Detect entity representation on attribute values
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?
This looks good. I would just document this behavior, add to CHANGELOG and it's good to merge.
Bump. Want this in?
Yeah, I'll take care of this later today. Thanks!