Как отфильтровать свойства / коллекции от представителей?

Например, у меня есть представитель, похожий на:

module AccountRepresenter

  include Roar::Representer::JSON
  include Roar::Representer::Feature::Hypermedia

  property :id
  property :name

  collection :assets, :extend => AssetRepresenter, :class => Asset
end

В контроллере я использую его как:

respond_with @accounts, represent_with: AccountsRepresenter

Для другого действия я не хочу collection :assets быть представленным. Я старался

respond_with @accounts, represent_with: AccountsRepresenter, exclude: :assets

Но это не работает. Как мне передать аргумент представителям?

1 ответ

Решение

Жемчужина Roar использует представимое внизу ( https://github.com/apotonick/representable). Для условной функциональности вы можете сделать следующее:

respond_with @accounts, represent_with: AccountsRepresenter, exclude: [:assets]

затем

module AccountRepresenter

  include Roar::Representer::JSON
  include Roar::Representer::Feature::Hypermedia

  property :id
  property :name

  collection :assets, extend: AssetRepresenter, if: lambda { |opts| !opts[:exclude].includes?(:assets)] }
end
Другие вопросы по тегам