mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #15889 from carnesmedia/model-name
Use #model_name on instances instead of classes
This commit is contained in:
commit
d2d809868c
13 changed files with 28 additions and 24 deletions
|
@ -6,7 +6,7 @@ module ActionController
|
||||||
end
|
end
|
||||||
|
|
||||||
def model_name_from_record_or_class(record_or_class)
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -254,9 +254,9 @@ module ActionDispatch
|
||||||
model = record.to_model
|
model = record.to_model
|
||||||
name = if record.persisted?
|
name = if record.persisted?
|
||||||
args << model
|
args << model
|
||||||
model.class.model_name.singular_route_key
|
model.model_name.singular_route_key
|
||||||
else
|
else
|
||||||
@key_strategy.call model.class.model_name
|
@key_strategy.call model.model_name
|
||||||
end
|
end
|
||||||
|
|
||||||
named_route = prefix + "#{name}_#{suffix}"
|
named_route = prefix + "#{name}_#{suffix}"
|
||||||
|
@ -284,7 +284,7 @@ module ActionDispatch
|
||||||
parent.model_name.singular_route_key
|
parent.model_name.singular_route_key
|
||||||
else
|
else
|
||||||
args << parent.to_model
|
args << parent.to_model
|
||||||
parent.to_model.class.model_name.singular_route_key
|
parent.to_model.model_name.singular_route_key
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -297,9 +297,9 @@ module ActionDispatch
|
||||||
else
|
else
|
||||||
if record.persisted?
|
if record.persisted?
|
||||||
args << record.to_model
|
args << record.to_model
|
||||||
record.to_model.class.model_name.singular_route_key
|
record.to_model.model_name.singular_route_key
|
||||||
else
|
else
|
||||||
@key_strategy.call record.to_model.class.model_name
|
@key_strategy.call record.to_model.model_name
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1863,8 +1863,8 @@ module ActionView
|
||||||
object = convert_to_model(@object)
|
object = convert_to_model(@object)
|
||||||
key = object ? (object.persisted? ? :update : :create) : :submit
|
key = object ? (object.persisted? ? :update : :create) : :submit
|
||||||
|
|
||||||
model = if object.class.respond_to?(:model_name)
|
model = if object.respond_to?(:model_name)
|
||||||
object.class.model_name.human
|
object.model_name.human
|
||||||
else
|
else
|
||||||
@object_name.to_s.humanize
|
@object_name.to_s.humanize
|
||||||
end
|
end
|
||||||
|
|
|
@ -40,7 +40,7 @@ module ActionView
|
||||||
@object_name.gsub!(/\[(.*)_attributes\]\[\d+\]/, '.\1')
|
@object_name.gsub!(/\[(.*)_attributes\]\[\d+\]/, '.\1')
|
||||||
|
|
||||||
if object.respond_to?(:to_model)
|
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, ""]
|
i18n_default = ["#{key}.#{method_and_value}".to_sym, ""]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,7 @@ module ActionView
|
||||||
end
|
end
|
||||||
|
|
||||||
def model_name_from_record_or_class(record_or_class)
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -34,8 +34,8 @@ class ModelDelegator < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
class ModelDelegate
|
class ModelDelegate
|
||||||
def self.model_name
|
def model_name
|
||||||
ActiveModel::Name.new(self)
|
ActiveModel::Name.new(self.class)
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_param
|
def to_param
|
||||||
|
|
|
@ -434,7 +434,7 @@ module ActiveModel
|
||||||
|
|
||||||
options = {
|
options = {
|
||||||
default: defaults,
|
default: defaults,
|
||||||
model: @base.class.model_name.human,
|
model: @base.model_name.human,
|
||||||
attribute: @base.class.human_attribute_name(attribute),
|
attribute: @base.class.human_attribute_name(attribute),
|
||||||
value: value
|
value: value
|
||||||
}.merge!(options)
|
}.merge!(options)
|
||||||
|
|
|
@ -73,16 +73,19 @@ module ActiveModel
|
||||||
|
|
||||||
# == \Naming
|
# == \Naming
|
||||||
#
|
#
|
||||||
# Model.model_name must return a string with some convenience methods:
|
# Model.model_name and Model#model_name must return a string with some
|
||||||
# <tt>:human</tt>, <tt>:singular</tt> and <tt>:plural</tt>. Check
|
# convenience methods: # <tt>:human</tt>, <tt>:singular</tt> and
|
||||||
# ActiveModel::Naming for more information.
|
# <tt>:plural</tt>. Check ActiveModel::Naming for more information.
|
||||||
def test_model_naming
|
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
|
model_name = model.class.model_name
|
||||||
assert model_name.respond_to?(:to_str)
|
assert model_name.respond_to?(:to_str)
|
||||||
assert model_name.human.respond_to?(:to_str)
|
assert model_name.human.respond_to?(:to_str)
|
||||||
assert model_name.singular.respond_to?(:to_str)
|
assert model_name.singular.respond_to?(:to_str)
|
||||||
assert model_name.plural.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
|
end
|
||||||
|
|
||||||
# == \Errors Testing
|
# == \Errors Testing
|
||||||
|
|
|
@ -307,7 +307,7 @@ module ActiveModel
|
||||||
if record_or_class.respond_to?(:model_name)
|
if record_or_class.respond_to?(:model_name)
|
||||||
record_or_class.model_name
|
record_or_class.model_name
|
||||||
elsif record_or_class.respond_to?(:to_model)
|
elsif record_or_class.respond_to?(:to_model)
|
||||||
record_or_class.to_model.class.model_name
|
record_or_class.to_model.model_name
|
||||||
else
|
else
|
||||||
record_or_class.class.model_name
|
record_or_class.class.model_name
|
||||||
end
|
end
|
||||||
|
|
|
@ -93,7 +93,7 @@ module ActiveModel
|
||||||
end
|
end
|
||||||
|
|
||||||
if root
|
if root
|
||||||
root = self.class.model_name.element if root == true
|
root = model_name.element if root == true
|
||||||
{ root => serializable_hash(options) }
|
{ root => serializable_hash(options) }
|
||||||
else
|
else
|
||||||
serializable_hash(options)
|
serializable_hash(options)
|
||||||
|
|
|
@ -84,7 +84,7 @@ module ActiveModel
|
||||||
@builder = options[:builder]
|
@builder = options[:builder]
|
||||||
@builder.instruct! unless options[:skip_instruct]
|
@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)
|
root = ActiveSupport::XmlMini.rename_key(root, options)
|
||||||
|
|
||||||
args = [root]
|
args = [root]
|
||||||
|
|
|
@ -39,6 +39,7 @@ module ActiveModel
|
||||||
extend ActiveSupport::Concern
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
included do
|
included do
|
||||||
|
extend ActiveModel::Naming
|
||||||
extend ActiveModel::Callbacks
|
extend ActiveModel::Callbacks
|
||||||
extend ActiveModel::Translation
|
extend ActiveModel::Translation
|
||||||
|
|
||||||
|
|
|
@ -55,16 +55,16 @@ module ActiveRecord
|
||||||
def cache_key(*timestamp_names)
|
def cache_key(*timestamp_names)
|
||||||
case
|
case
|
||||||
when new_record?
|
when new_record?
|
||||||
"#{self.class.model_name.cache_key}/new"
|
"#{model_name.cache_key}/new"
|
||||||
when timestamp_names.any?
|
when timestamp_names.any?
|
||||||
timestamp = max_updated_column_timestamp(timestamp_names)
|
timestamp = max_updated_column_timestamp(timestamp_names)
|
||||||
timestamp = timestamp.utc.to_s(cache_timestamp_format)
|
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
|
when timestamp = max_updated_column_timestamp
|
||||||
timestamp = timestamp.utc.to_s(cache_timestamp_format)
|
timestamp = timestamp.utc.to_s(cache_timestamp_format)
|
||||||
"#{self.class.model_name.cache_key}/#{id}-#{timestamp}"
|
"#{model_name.cache_key}/#{id}-#{timestamp}"
|
||||||
else
|
else
|
||||||
"#{self.class.model_name.cache_key}/#{id}"
|
"#{model_name.cache_key}/#{id}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue