-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for inline serializers #1226
Conversation
IMO no need for the changelog here, I think we can group it all under a nesting serializer feature entry. 😄 |
54725ac
to
c0dc25c
Compare
@beauby could we have a specific docs on how to do nested / namespaced serializers? |
@joaomdmoura Sure, will do! |
@beauby did you make the docs per @joaomdmoura 's suggestion? are they in a different pr that I haven't yet gotten to? :-) |
@joaomdmoura @NullVoxPopuli So there is an explicit example in the docs about nested serializers. I will eventually write a How To about API versioning with AMS but it is unrelated to this PR. As far as I'm concerned, this PR is ready for merging (squashing apart). Any remarks? |
@beauby LGTM :-) |
Closing this one for now as we're moving towards using the block syntax for overriding associations rather than specifying nested serializers. |
Hi, I tried to use this feature but association returns all attributes: module Api
module V1
class SectionSerializer < ActiveModel::Serializer
attributes :id, :name, :short_name
has_many :libraries do
attributes :id, :name, :short_name, :code
end
belongs_to :parent do
attributes :id, :name, :short_name
end
# TODO: Remove this in the future
def libraries
object.products
end
end
end
end section = Section.first
opts = { serializer: Api::V1::SectionSerializer }
serializable_resource = ActiveModel::SerializableResource.new(section, opts)
serializable_resource.as_json # => {:id=>46, :name=>"Test1", :short_name=>"Test1", :libraries=>[{"id"=>41, "name"=>"test", "short_name"=>"test", "created_at"=>Tue, 20 Oct 2015 11:20:14 UTC +00:00, "updated_at"=>Tue, 20 Oct 2015 11:20:14 UTC +00:00, "config"=>{}, "banner_file_name"=>"12k.png", "banner_content_type"=>"image/png", "banner_file_size"=>12093, "banner_updated_at"=>Tue, 20 Oct 2015 11:19:57 UTC +00:00, "logo_file_name"=>"12k.png", "logo_content_type"=>"image/png", "logo_file_size"=>12093, "logo_updated_at"=>Tue, 20 Oct 2015 11:20:03 UTC +00:00, "rank"=>0, "code"=>"test"}], :parent=>nil} |
@itsmechlark Thanks for your interest. However this feature has not been merged, and will probably not be. module Api
module V1
class SectionSerializer < ActiveModel::Serializer
attributes :id, :name, :short_name
has_many :libraries
class LibrarySerializer < ActiveModel::Serializer
attributes :id, :name, :short_name, :code
end
belongs_to :parent # Here I'm guessing parent is a Section
class SectionSerializer < ActiveModel::Serializer
attributes :id, :name, :short_name
end
# TODO: Remove this in the future
def libraries
object.products
end
end
end
end |
@beauby Thanks for immediate respond. Hope this feature will be added in future, it will help alot. Thanks. BTW, I merge it with my fork repo just to test. |
And also, does AMS supports option in associations like has_many :libraries, as: :products |
@itsmechlark Yes, you can specify the |
This is the second part of #1193, and is based on top of #1225. This PR allows for defining nested serializers with a block syntax as follows: