diff --git a/actionpack/lib/action_controller/model_naming.rb b/actionpack/lib/action_controller/model_naming.rb
index 785221dc3dca644866d1f40c7d3b09f250f70f3b..2b33f6726315e7703ac6e4d49651ed9cc101000c 100644
--- a/actionpack/lib/action_controller/model_naming.rb
+++ b/actionpack/lib/action_controller/model_naming.rb
@@ -6,7 +6,7 @@ def convert_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
+ 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 bd3696cda16d07636b1b427e7f1e6d12450817c5..746e4cd2458728cc733735c3e10bf69c1797526d 100644
--- a/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb
+++ b/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb
@@ -249,9 +249,9 @@ def handle_model(record)
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}"
@@ -279,7 +279,7 @@ def handle_list(list)
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
}
@@ -292,9 +292,9 @@ def handle_list(list)
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 789a413c8d2501882733623ae93fcebeb6e9de97..bd8e3b19735aae87e415e0cec67dc0444d29841b 100644
--- a/actionview/lib/action_view/helpers/form_helper.rb
+++ b/actionview/lib/action_view/helpers/form_helper.rb
@@ -1815,8 +1815,8 @@ def submit_default_value
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 a5bcaf81537249c6d92ed4ca345e57621610a1bb..39b2f48c390b1000ce9e94581a428b6ae37ba900 100644
--- a/actionview/lib/action_view/helpers/tags/label.rb
+++ b/actionview/lib/action_view/helpers/tags/label.rb
@@ -40,7 +40,7 @@ def render(&block)
@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 e09ebd60df95bc77cc3a9e9020078d257ea674e3..d42e436b1706b415a7907decffab7c8d180af8e2 100644
--- a/actionview/lib/action_view/model_naming.rb
+++ b/actionview/lib/action_view/model_naming.rb
@@ -6,7 +6,7 @@ def convert_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
+ 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 fef27ef492af7fec012ce883b8f50ae3d1090ab9..b03fe66e2363f36d0edc79ca3d9339b5c3c3b7f3 100644
--- a/actionview/test/activerecord/polymorphic_routes_test.rb
+++ b/actionview/test/activerecord/polymorphic_routes_test.rb
@@ -34,8 +34,8 @@ def to_model
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 917d3b9142f4ddeb6fffc963775a288daf733ded..95029fd5383f209ce082c97ca3a8b2dc7cea7185 100644
--- a/activemodel/lib/active_model/errors.rb
+++ b/activemodel/lib/active_model/errors.rb
@@ -434,7 +434,7 @@ def generate_message(attribute, type = :invalid, options = {})
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 c6bc18b00864607f0f6f0301314d083d3281a22f..38087521a2f5f54022a23853d5bfff2014b4743f 100644
--- a/activemodel/lib/active_model/lint.rb
+++ b/activemodel/lib/active_model/lint.rb
@@ -73,16 +73,19 @@ def test_persisted?
# == \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 8cf1a191f4b5aa4b58c37b57d7745063af1b8b5e..ef3449b7cb1b55d56b59a052fcee11428fa0e33d 100644
--- a/activemodel/lib/active_model/naming.rb
+++ b/activemodel/lib/active_model/naming.rb
@@ -308,7 +308,7 @@ def self.model_name_from_record_or_class(record_or_class) #:nodoc:
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 c58e73f6a78e980699587d19826fbc7ca77d3eb4..77f2a64b11cc16f4cc8bcf804a82f5a09043f167 100644
--- a/activemodel/lib/active_model/serializers/json.rb
+++ b/activemodel/lib/active_model/serializers/json.rb
@@ -93,7 +93,7 @@ def as_json(options = nil)
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 7f99536dbbc56cc545da1fe24666784654220917..3ad3bf30ad2eb2a55ec6e0e6c86a14551f2e720f 100644
--- a/activemodel/lib/active_model/serializers/xml.rb
+++ b/activemodel/lib/active_model/serializers/xml.rb
@@ -84,7 +84,7 @@ def serialize
@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 cf97f45dba94ab1127891dc55ebbefaa17dfc3fb..e5468633051c323cea463768c40a70e3aae4526d 100644
--- a/activemodel/lib/active_model/validations.rb
+++ b/activemodel/lib/active_model/validations.rb
@@ -39,6 +39,7 @@ module Validations
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 31e2518540d6af3550a459af2e3310905fa84570..15b2f65dcb2f49b5ee46f1dbe08a4f72372f7f0f 100644
--- a/activerecord/lib/active_record/integration.rb
+++ b/activerecord/lib/active_record/integration.rb
@@ -55,16 +55,16 @@ def to_param
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