grape-entity icon indicating copy to clipboard operation
grape-entity copied to clipboard

Detect entity representation on attribute values

Open mbleigh opened this issue 11 years ago • 3 comments

This is something I've run into a number of times and (I think) this solves the issue. I'd like feedback in case there are dangers of which I'm not aware here. The problem is this:

class User < ActiveRecord::Base
  include Grape::Entity::DSL

  entity do
    expose(:name){ "Bob" }
  end
end

class Project < ActiveRecord::Base
  include Grape::Entity::DSL
  def user; User.new; end

  entity do
    expose :user
  end
end

With the above code, what I would expect is the JSON representation to look like:

{
  "user": {
    "name": "Bob"
  }
}

Instead the native JSON representation for the object is coming through. This pull request alters the default behavior when no :using or :proc or block are specified on an exposure and tries to detect an entity for it, first by value.entity then by value.class.entity_class.represent(value).

I believe this is making the behavior more what someone would expect. Any thoughts @dblock or others?

mbleigh avatar May 02 '14 01:05 mbleigh

This looks good. I would just document this behavior, add to CHANGELOG and it's good to merge.

dblock avatar May 02 '14 11:05 dblock

Bump. Want this in?

dblock avatar May 13 '14 19:05 dblock

Yeah, I'll take care of this later today. Thanks!

mbleigh avatar May 13 '14 19:05 mbleigh