diff --git a/actionpack/lib/action_controller/model_naming.rb b/actionpack/lib/action_controller/model_naming.rb index 785221dc3d..2b33f67263 100644 --- a/actionpack/lib/action_controller/model_naming.rb +++ b/actionpack/lib/action_controller/model_naming.rb @@ -6,7 +6,7 @@ module ActionController 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 + convert_to_model(record_or_class).model_name end end end diff --git a/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb b/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb index cd8b1ab066..427a5674bd 100644 --- a/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb +++ b/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb @@ -254,9 +254,9 @@ module ActionDispatch model = record.to_model name = if record.persisted? args << model - model.class.model_name.singular_route_key + model.model_name.singular_route_key else - @key_strategy.call model.class.model_name + @key_strategy.call model.model_name end named_route = prefix + "#{name}_#{suffix}" @@ -284,7 +284,7 @@ module ActionDispatch parent.model_name.singular_route_key else args << parent.to_model - parent.to_model.class.model_name.singular_route_key + parent.to_model.model_name.singular_route_key end } @@ -297,9 +297,9 @@ module ActionDispatch else if record.persisted? args << record.to_model - record.to_model.class.model_name.singular_route_key + record.to_model.model_name.singular_route_key else - @key_strategy.call record.to_model.class.model_name + @key_strategy.call record.to_model.model_name end end diff --git a/actionview/lib/action_view/helpers/form_helper.rb b/actionview/lib/action_view/helpers/form_helper.rb index c6bc0c9e38..0582cb3e12 100644 --- a/actionview/lib/action_view/helpers/form_helper.rb +++ b/actionview/lib/action_view/helpers/form_helper.rb @@ -1863,8 +1863,8 @@ module ActionView object = convert_to_model(@object) key = object ? (object.persisted? ? :update : :create) : :submit - model = if object.class.respond_to?(:model_name) - object.class.model_name.human + model = if object.respond_to?(:model_name) + object.model_name.human else @object_name.to_s.humanize end diff --git a/actionview/lib/action_view/helpers/tags/label.rb b/actionview/lib/action_view/helpers/tags/label.rb index a5bcaf8153..39b2f48c39 100644 --- a/actionview/lib/action_view/helpers/tags/label.rb +++ b/actionview/lib/action_view/helpers/tags/label.rb @@ -40,7 +40,7 @@ module ActionView @object_name.gsub!(/\[(.*)_attributes\]\[\d+\]/, '.\1') if object.respond_to?(:to_model) - key = object.class.model_name.i18n_key + key = object.model_name.i18n_key i18n_default = ["#{key}.#{method_and_value}".to_sym, ""] end diff --git a/actionview/lib/action_view/model_naming.rb b/actionview/lib/action_view/model_naming.rb index e09ebd60df..d42e436b17 100644 --- a/actionview/lib/action_view/model_naming.rb +++ b/actionview/lib/action_view/model_naming.rb @@ -6,7 +6,7 @@ module ActionView 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 + convert_to_model(record_or_class).model_name end end end diff --git a/actionview/test/activerecord/polymorphic_routes_test.rb b/actionview/test/activerecord/polymorphic_routes_test.rb index e220dcb8cb..0d97ddb2f6 100644 --- a/actionview/test/activerecord/polymorphic_routes_test.rb +++ b/actionview/test/activerecord/polymorphic_routes_test.rb @@ -34,8 +34,8 @@ class ModelDelegator < ActiveRecord::Base end class ModelDelegate - def self.model_name - ActiveModel::Name.new(self) + def model_name + ActiveModel::Name.new(self.class) end def to_param diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index 1d025beeef..fc8034f9c7 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -434,7 +434,7 @@ module ActiveModel options = { default: defaults, - model: @base.class.model_name.human, + model: @base.model_name.human, attribute: @base.class.human_attribute_name(attribute), value: value }.merge!(options) diff --git a/activemodel/lib/active_model/lint.rb b/activemodel/lib/active_model/lint.rb index c6bc18b008..38087521a2 100644 --- a/activemodel/lib/active_model/lint.rb +++ b/activemodel/lib/active_model/lint.rb @@ -73,16 +73,19 @@ module ActiveModel # == \Naming # - # Model.model_name must return a string with some convenience methods: - # :human, :singular and :plural. Check - # ActiveModel::Naming for more information. + # Model.model_name and Model#model_name must return a string with some + # convenience methods: # :human, :singular and + # :plural. Check ActiveModel::Naming for more information. def test_model_naming - assert model.class.respond_to?(:model_name), "The model should respond to model_name" + assert model.class.respond_to?(:model_name), "The model class should respond to model_name" model_name = model.class.model_name assert model_name.respond_to?(:to_str) assert model_name.human.respond_to?(:to_str) assert model_name.singular.respond_to?(:to_str) assert model_name.plural.respond_to?(:to_str) + + assert model.respond_to?(:model_name), "The model instance should respond to model_name" + assert_equal model.model_name, model.class.model_name end # == \Errors Testing diff --git a/activemodel/lib/active_model/naming.rb b/activemodel/lib/active_model/naming.rb index 241e88deeb..d9ec502f88 100644 --- a/activemodel/lib/active_model/naming.rb +++ b/activemodel/lib/active_model/naming.rb @@ -307,7 +307,7 @@ module ActiveModel if record_or_class.respond_to?(:model_name) record_or_class.model_name elsif record_or_class.respond_to?(:to_model) - record_or_class.to_model.class.model_name + record_or_class.to_model.model_name else record_or_class.class.model_name end diff --git a/activemodel/lib/active_model/serializers/json.rb b/activemodel/lib/active_model/serializers/json.rb index c58e73f6a7..77f2a64b11 100644 --- a/activemodel/lib/active_model/serializers/json.rb +++ b/activemodel/lib/active_model/serializers/json.rb @@ -93,7 +93,7 @@ module ActiveModel end if root - root = self.class.model_name.element if root == true + root = model_name.element if root == true { root => serializable_hash(options) } else serializable_hash(options) diff --git a/activemodel/lib/active_model/serializers/xml.rb b/activemodel/lib/active_model/serializers/xml.rb index 7f99536dbb..3ad3bf30ad 100644 --- a/activemodel/lib/active_model/serializers/xml.rb +++ b/activemodel/lib/active_model/serializers/xml.rb @@ -84,7 +84,7 @@ module ActiveModel @builder = options[:builder] @builder.instruct! unless options[:skip_instruct] - root = (options[:root] || @serializable.class.model_name.element).to_s + root = (options[:root] || @serializable.model_name.element).to_s root = ActiveSupport::XmlMini.rename_key(root, options) args = [root] diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb index f67a3be5c1..7ee033ba5f 100644 --- a/activemodel/lib/active_model/validations.rb +++ b/activemodel/lib/active_model/validations.rb @@ -39,6 +39,7 @@ module ActiveModel extend ActiveSupport::Concern included do + extend ActiveModel::Naming extend ActiveModel::Callbacks extend ActiveModel::Translation diff --git a/activerecord/lib/active_record/integration.rb b/activerecord/lib/active_record/integration.rb index 31e2518540..15b2f65dcb 100644 --- a/activerecord/lib/active_record/integration.rb +++ b/activerecord/lib/active_record/integration.rb @@ -55,16 +55,16 @@ module ActiveRecord def cache_key(*timestamp_names) case when new_record? - "#{self.class.model_name.cache_key}/new" + "#{model_name.cache_key}/new" when timestamp_names.any? timestamp = max_updated_column_timestamp(timestamp_names) timestamp = timestamp.utc.to_s(cache_timestamp_format) - "#{self.class.model_name.cache_key}/#{id}-#{timestamp}" + "#{model_name.cache_key}/#{id}-#{timestamp}" when timestamp = max_updated_column_timestamp timestamp = timestamp.utc.to_s(cache_timestamp_format) - "#{self.class.model_name.cache_key}/#{id}-#{timestamp}" + "#{model_name.cache_key}/#{id}-#{timestamp}" else - "#{self.class.model_name.cache_key}/#{id}" + "#{model_name.cache_key}/#{id}" end end