Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect entity representation on attribute values #70

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mbleigh
Copy link
Contributor

@mbleigh mbleigh commented May 2, 2014

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?

@dblock
Copy link
Member

dblock commented May 2, 2014

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

@dblock
Copy link
Member

dblock commented May 13, 2014

Bump. Want this in?

@mbleigh
Copy link
Contributor Author

mbleigh commented May 13, 2014

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants