Skip to content
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

Closed
wants to merge 4 commits into from

Conversation

beauby
Copy link
Contributor

@beauby beauby commented Oct 2, 2015

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:

class PostSerializer
  attributes ...
  has_many :comments do
    attributes ...
  end
end

@joaomdmoura
Copy link
Member

IMO no need for the changelog here, I think we can group it all under a nesting serializer feature entry. 😄

@beauby beauby force-pushed the inline-implicit-serializers branch from 54725ac to c0dc25c Compare October 9, 2015 14:41
@joaomdmoura
Copy link
Member

@beauby could we have a specific docs on how to do nested / namespaced serializers?
Or even some moe specific examples like, "how to verison API using namespaces"

@beauby
Copy link
Contributor Author

beauby commented Oct 12, 2015

@joaomdmoura Sure, will do!

@NullVoxPopuli
Copy link
Contributor

@beauby did you make the docs per @joaomdmoura 's suggestion? are they in a different pr that I haven't yet gotten to? :-)

@beauby
Copy link
Contributor Author

beauby commented Oct 18, 2015

@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 beauby added the S: Hold label Oct 19, 2015
@NullVoxPopuli
Copy link
Contributor

@beauby LGTM :-)

@beauby
Copy link
Contributor Author

beauby commented Oct 19, 2015

Closing this one for now as we're moving towards using the block syntax for overriding associations rather than specifying nested serializers.

@beauby beauby closed this Oct 19, 2015
@itsmechlark
Copy link

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} 

@beauby
Copy link
Contributor Author

beauby commented Oct 21, 2015

@itsmechlark Thanks for your interest. However this feature has not been merged, and will probably not be.
What you can do though is the following:

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

@itsmechlark
Copy link

@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.

@itsmechlark
Copy link

And also, does AMS supports option in associations like has_many to use like:

has_many :libraries, as: :products

@beauby
Copy link
Contributor Author

beauby commented Oct 25, 2015

@itsmechlark Yes, you can specify the key option, that will set the JSON key under which the association will be serialized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants