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

Inherited methods are hidden in serializer #1653

Closed
dpogue opened this issue Apr 1, 2016 · 3 comments
Closed

Inherited methods are hidden in serializer #1653

dpogue opened this issue Apr 1, 2016 · 3 comments

Comments

@dpogue
Copy link

dpogue commented Apr 1, 2016

Expected behavior vs actual behavior

When subclassing a serializer, the parent class's instance methods are not populated in the child class's instance method list, so _serializer_instance_method_defined? returns false.

This started breaking with commit a065bc2.

Steps to reproduce

class ParentSerializer < ActiveModel::Serializer
  attributes :$id

  define_method(:$id) do
    object.id
  end
end
class ChildSerializer < ParentSerializer
  attributes :name
end

Throws the following error:
undefined method '$id' for <Child:0x00...>

Environment

ActiveModelSerializers Version (commit ref if not on tag): 7acbb76

Output of ruby -e "puts RUBY_DESCRIPTION": ruby 2.1.5p273 (2014-11-13 revision 48405) [x86_64-darwin14.0]

OS Type & Version: OSX 10.11.4

Integrated application and version (e.g., Rails, Grape, etc): Rails

@bf4
Copy link
Member

bf4 commented Apr 3, 2016

Oh interesting.

I can fix that, unless you're interested in helping out with a pr.

Also, curious, what are you using the define_method for? Could use an attribute with the key: option or just alias the id method

@dpogue
Copy link
Author

dpogue commented Apr 3, 2016

In practice, I'm using URIs for identifiers, so define_method(:$id) returns scope.url_for(self). Ruby won't let you use def for a method starting with a $.

@bf4
Copy link
Member

bf4 commented Apr 4, 2016

Should be fixed by #1661

bf4 added a commit to bf4/active_model_serializers that referenced this issue Apr 4, 2016
…hods

Fixes rails-api#1653, rails-api#1658, rails-api#1660

Define "scope_name" on instance singleton, not all instances
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants