Skip to content

Kernel methods get priority over delegate methods #215

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

Closed
maltoe opened this issue Apr 20, 2016 · 4 comments
Closed

Kernel methods get priority over delegate methods #215

maltoe opened this issue Apr 20, 2016 · 4 comments
Labels

Comments

@maltoe
Copy link

maltoe commented Apr 20, 2016

Hi,

Question: Due to the way exposures are looked up in the entity class first (calling respond_to attribute, true), attributes defined on the delegate class that collide with Kernel methods can not be exposed. We today noticed that with a AR model that has a belongs_to :system association. Probably poor wording on our side, but still: What would be the preferred way to deal with this? Currently we need to monkey-patch Grape::Entity#delegate_attribute.

class Foo < ActiveRecord::Base
  belongs_to :system

  class Entity < Grape::Entity
    expose :system
  end
end

Thanks,
Malte

@Linell
Copy link

Linell commented Apr 21, 2016

I've ran into the same problem before, and the way that I handled it was

class Foo < ActiveRecord::Base
  belongs_to :system

  class Entity < Grape::Entity
    expose :system do |foo, options|
      foo.system
    end
  end
end

It's not great, but it works.

@dblock
Copy link
Member

dblock commented Apr 21, 2016

Is there something we can do about this in grape-entity?

@dblock dblock added the bug? label Apr 21, 2016
@maltoe
Copy link
Author

maltoe commented Apr 25, 2016

Sure, setting this to false. But there's a spec testing specifically for this, so I guess you guys were presented with a use case for allowing private methods of the Entity to take priority over the delegator's attribute.

You could also just exclude methods not owned by the entity class, á la

def delegate_attribute attribute
  if respond_to?(attribute, true) && method(attribute).owner == self.class
   ...

@maltoe
Copy link
Author

maltoe commented Apr 25, 2016

@Linell Thanks, that would work. Unfortunately, we actually do not have the Entity classes in the model classes themselves (gave that example for simplicity), so we usually have something like

module Entities
  class Foo < Grape::Entity
    expose :system, using: Entities::System
  end
end

maltoe pushed a commit to plugintheworld/grape-entity that referenced this issue Apr 25, 2016
This patch prevents Entity from delegating attributes
to Kernel methods. This is accomplished by testing the
methods the Entity class responds to for ownership
by the Entity class itself.

Fixes ruby-grape#215.
sbatykov pushed a commit to sbatykov/grape-entity that referenced this issue Jun 28, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants