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

human_attribute_name overriding #2049

Closed
eugen0329 opened this issue Feb 9, 2017 · 1 comment
Closed

human_attribute_name overriding #2049

eugen0329 opened this issue Feb 9, 2017 · 1 comment

Comments

@eugen0329
Copy link

Expected behavior vs actual behavior

#human_attribute_name on class that inherited from ActiveModelSerializers::Model returns it's translation according to ActiveModel::Translation#human_attribute_name functionality.

Actual behaviour

#human_attribute_name on class that inherited from ActiveModelSerializers::Model returns an attribute passed to it without any translations.

Steps to reproduce

  1. Create a poro and inherit it from ActiveModelSerializers::Model:
class TestObject < ActiveModelSerializers::Model
  attr_accessor :test_attr
end
  1. Translate attribute with #human_attribute_name:
TestObject.human_attribute_name(:test_attr)

Environment

active_model_serializers (~> 0.10.2)

Additonal helpful information

I found the method implementation in ActiveModelSerializers::Model, and I have a question. Why do we need this method defined within ActiveModelSerializers::Model when it's already supplied within ActiveModel?

@bf4
Copy link
Member

bf4 commented Feb 9, 2017

@eugen0329 Sounds like you understand the problem well enough to issue a pull request. Would you be interested in doing that? I don't see it included in ActiveModel::Model... oh, n/m it has extend ActiveModel::Translation

Looks like you're right, based on the active model errors docs https://github.com/rails/rails/blob/4-2-stable/activemodel/lib/active_model/errors.rb

  #     # The following methods are needed to be minimally implemented
  #
  #     def read_attribute_for_validation(attr)
  #       send(attr)
  #     end
  #
  #     def self.human_attribute_name(attr, options = {})
  #       attr
  #     end
  #
  #     def self.lookup_ancestors
  #       [self]
  #     end
  #   end
  #
  # The last three methods are required in your object for +Errors+ to be
  # able to generate error messages correctly and also handle multiple
  # languages. Of course, if you extend your object with <tt>ActiveModel::Translation</tt>
  # you will not need to implement the last two. Likewise, using
  # <tt>ActiveModel::Validations</tt> will handle the validation related methods
  # for you.

bf4 added a commit to bf4/active_model_serializers that referenced this issue Feb 16, 2017
As pointed out in
rails-api#2049

ActiveModel::Model already extends ActiveModel::Translation which
implements human_attribute_name and lookup_ancestors
@bf4 bf4 closed this as completed in #2054 Feb 16, 2017
GregPK pushed a commit to GregPK/active_model_serializers that referenced this issue Apr 25, 2017
As pointed out in
rails-api#2049

ActiveModel::Model already extends ActiveModel::Translation which
implements human_attribute_name and lookup_ancestors
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