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 nil #1658

Closed
pareeohnos opened this issue Apr 4, 2016 · 7 comments
Closed

Inherited methods are nil #1658

pareeohnos opened this issue Apr 4, 2016 · 7 comments

Comments

@pareeohnos
Copy link

Expected behavior vs actual behavior

I've created an ApplicationSerializer which I want to contain the property success which is in all child serializers. My understanding is that by declaring the attribute within the superclass, all child classes will inherit this. I am finding that this is partially true when the attribute declared is a method, rather than a property on the underlying resource.

When I render using an instance of the child serializer, the attribute key is present, however the value is always null. If I override the attribute method and simply call super within it, I get the value I expect, however this defeats the purpose of inheritance.

Steps to reproduce

I have the following structure

class ApplicationSerializer < ActiveModel::Serializer
  attributes :status

  def status
    object.try(:errors).blank? && object.try(:error).blank?
  end

end
class ErrorResponse < ActiveModelSerializers::Model
  attr_accessor :error
end
class ErrorResponseSerializer < ApplicationSerializer
  attributes :error
end

In my controller, I am rendering the response using:

render json: ErrorResponse.new('Error message here'), status: :unauthorized

I am expecting to get the JSON response of

{
    "success": false,
    "error": "Error message here"
}

However, I instead get the response

{
    "success": null,
    "error": "Error message here"
}

To remedy this, I can change the definition of the ErrorResponseSerializer class to the following:

class ErrorResponseSerializer < ApplicationSerializer
  attributes :error

  def success
    super
  end
end

And this then works as expected.

Environment

ActiveModelSerializers Version: 0.10.0.rc4 d30aa4c
Output of ruby -e "puts RUBY_DESCRIPTION": ruby 2.1.3p242 (2014-09-19 revision 47630) [x86_64-darwin13.0]
OS Type & Version: Mac OS X 10.11.3
Integrated application and version: Rails 4.2.1

@NullVoxPopuli
Copy link
Contributor

@pareeohnos hey, can you try this commit: 5be33af

@pareeohnos
Copy link
Author

hmm that's strange @NullVoxPopuli that's worked. Something after this commit must be breaking something though. I have just installed from master before trying this, and that commit appears to have been added to master 3 days ago?

@pareeohnos
Copy link
Author

have just tested - your commit 5be33af works, but the commit straight after dba85f2 causes it to break once again, even though the commit is 100% documentation. How is that possible!?

@NullVoxPopuli
Copy link
Contributor

I have no idea, I'm confused about this, too.

@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
@NullVoxPopuli
Copy link
Contributor

Closing for now, please re-open if this issue still exists

@pareeohnos
Copy link
Author

Seems to be working for me. Cheers guys :)

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

3 participants