I would expect to get json with both the name and note fields. But I only get the note field. What am I doing wrong?
class OrderPage
module Entities
class AsSeller < OrderPage::Entities::Order
expose :note
def note
object.note
end
end
end
end
class OrderPage
module Entities
class Order < Grape::Entity
expose :name
end
end
end
json = OrderPage::Entities::AsSeller.represent(order).as_json
I think you should take a step back and remember that an entity is supposed to represent an object, because it doesn't really seem like that's what you're doing here. So you should have one entity representing your codebase's concept of an Order object (AsSeller sounds like a visibility restriction, not an object), and use
with_options
to restrict what is or is not exposed.Alternatively, if it's just a matter of conditionally exposing an attribute that only exists on certain descendants of the order model, you should be able to do something like this:
If you really need to include a subset of attributes defined elsewhere, you could write a Concern and include it in another entity, but from the context available to me in your question and the naming you've used, it doesn't seem like that's a good solution here.