Re: [Rails-core] ActiveModel::Serializers::JSON support roles

33 views
Skip to first unread message

Steve Klabnik

unread,
Jun 21, 2012, 10:12:40 PM6/21/12
to rubyonra...@googlegroups.com
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.

Carlos Antonio da Silva

unread,
Jun 21, 2012, 10:49:38 PM6/21/12
to rubyonra...@googlegroups.com
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 :).
Reply all
Reply to author
Forward
0 new messages