Basically, I would like to be able to create different serialization_hash depending on "roles". Here''s an example of what I think could be helpful: controller: render :json @clients, as: :search OR render :json @clients, as: :contract_won ActiveModel::Serializers::JSON def attributes //default end def contract_won // as: :contract_won end def search // as: :search end -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-core/-/gH6j1zxEtcgJ. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
Basically, I would like to be able to create different serialization_hash depending on "roles". Here''s an example of what I think could be helpful: controller: render :json @clients, as: :search OR render :json @clients, as: :contract_won ActiveModel::Serializers::JSON def attributes //default end def contract_won // as: :contract_won end def search // as: :search end -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-core/-/oyBsDOl1qaQJ. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
I think this would be useful, but I''m not sure that this just means that there shouldn''t be two serailizers instead of putting both in one class. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
Carlos Antonio da Silva
2012-Jun-22 02:49 UTC
Re: ActiveModel::Serializers::JSON support roles
Steve, are you thinking about something like this in terms of code? render json: SearchClient.new(@clients) #or render json: ContractWon.new(@clients) class SearchClient def initialize(clients) @clients = clients end def to_json(options=nil) @clients.to_json(only: %w(foo bar) end end class ContractWon def initialize(clients) @clients = clients end def to_json(options=nil) @clients.to_json(only: %w(baz other) end end That''s basically the approach I''ve been using lately, and it works quite fine for my needs :). On Thursday, June 21, 2012 11:12:40 PM UTC-3, Steve Klabnik wrote:> > I think this would be useful, but I''m not sure that this just means > that there shouldn''t be two serailizers instead of putting both in one > class. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-core/-/5gm_TzguGugJ. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.