1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Use #model_name on instances instead of classes

This allows rails code to be more confdent when asking for a model name, instead of having to ask for the class.

Rails core discussion here: https://groups.google.com/forum/#!topic/rubyonrails-core/ThSaXw9y1F8
This commit is contained in:
Amiel Martin 2014-06-20 15:49:27 -07:00
parent 5b23e31771
commit 6b0e834a19
13 changed files with 28 additions and 24 deletions

View file

@ -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

View file

@ -249,9 +249,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}"
@ -279,7 +279,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
}
@ -292,9 +292,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

View file

@ -1815,8 +1815,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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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)

View file

@ -73,16 +73,19 @@ module ActiveModel
# == \Naming
#
# Model.model_name must return a string with some convenience methods:
# <tt>:human</tt>, <tt>:singular</tt> and <tt>:plural</tt>. Check
# ActiveModel::Naming for more information.
# Model.model_name and Model#model_name must return a string with some
# convenience methods: # <tt>:human</tt>, <tt>:singular</tt> and
# <tt>:plural</tt>. 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

View file

@ -308,7 +308,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

View file

@ -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)

View file

@ -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]

View file

@ -39,6 +39,7 @@ module ActiveModel
extend ActiveSupport::Concern
included do
extend ActiveModel::Naming
extend ActiveModel::Callbacks
extend ActiveModel::Translation

View file

@ -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