Throughout the Rails code there are a few references to object.class.model_name. Instead of always pushing this up to the class, it makes sense to me that we ask the model for its model_name.
module ActionController
module ModelNaming
# Converts the given object to an ActiveModel compliant one.
def convert_to_model(object)
object.respond_to?(:to_model) ? object.to_model : object
end
def model_name_from_record_or_class(record_or_class)
(record_or_class.is_a?(Class) ? record_or_class : convert_to_model(record_or_class).class).model_name
end
end
end
Example:
class Foo
extend ActiveModel::Naming
def model_name
class.model_name
end
end
I wouldn’t be in favour of this - purely because I’m generally against adding instance methods to ActiveRecord::Base in case they override attributes defined on the table. For example, a photography studio may have a photos table which imports from somewhere and has a “model_name” column.
I don’t see the issue with having it on the class myself.
I’m very much in favor of this as well. Please make it so. To address Andy’s concerns, we should have proper error messages if this is overwriting an attribute of the same name. But it’s too convenient not to have.
I know that I can fix it by creating a module that I can use on my class with this method using the ActiveModel::Naming. Am I wrong in use ActiveModel::Error without an activemodel? And the older way is not right in use the method from class?
Thanks so much, and sorry for opening an old topic again =/