mirror of https://github.com/rails/rails
Revert "Properly interpolate i18n keys in modules [#5572 state:resolved]"
This breaks #6448, you should use :"module/class" as key for namespacing
[#6448 state:committed]
This reverts commit 8d30193b08
.
This commit is contained in:
parent
acf0688fdd
commit
a00bed0c48
|
@ -295,8 +295,8 @@ module ActiveModel
|
|||
type = options.delete(:message) if options[:message].is_a?(Symbol)
|
||||
|
||||
defaults = @base.class.lookup_ancestors.map do |klass|
|
||||
[ :"#{@base.class.i18n_scope}.errors.models.#{klass.model_name.i18n_key}.attributes.#{attribute}.#{type}",
|
||||
:"#{@base.class.i18n_scope}.errors.models.#{klass.model_name.i18n_key}.#{type}" ]
|
||||
[ :"#{@base.class.i18n_scope}.errors.models.#{klass.model_name.underscore}.attributes.#{attribute}.#{type}",
|
||||
:"#{@base.class.i18n_scope}.errors.models.#{klass.model_name.underscore}.#{type}" ]
|
||||
end
|
||||
|
||||
defaults << options.delete(:message)
|
||||
|
|
|
@ -4,7 +4,7 @@ require 'active_support/core_ext/module/introspection'
|
|||
|
||||
module ActiveModel
|
||||
class Name < String
|
||||
attr_reader :singular, :plural, :element, :collection, :partial_path, :route_key, :param_key, :i18n_key
|
||||
attr_reader :singular, :plural, :element, :collection, :partial_path, :route_key, :param_key
|
||||
alias_method :cache_key, :collection
|
||||
|
||||
def initialize(klass, namespace = nil)
|
||||
|
@ -20,7 +20,6 @@ module ActiveModel
|
|||
@partial_path = "#{@collection}/#{@element}".freeze
|
||||
@param_key = (namespace ? _singularize(@unnamespaced) : @singular).freeze
|
||||
@route_key = (namespace ? ActiveSupport::Inflector.pluralize(@param_key) : @plural).freeze
|
||||
@i18n_key = _singularize(self, '.').to_sym
|
||||
end
|
||||
|
||||
# Transform the model name into a more humane format, using I18n. By default,
|
||||
|
@ -34,7 +33,7 @@ module ActiveModel
|
|||
@klass.respond_to?(:i18n_scope)
|
||||
|
||||
defaults = @klass.lookup_ancestors.map do |klass|
|
||||
klass.model_name.i18n_key
|
||||
klass.model_name.underscore.to_sym
|
||||
end
|
||||
|
||||
defaults << options[:default] if options[:default]
|
||||
|
@ -45,10 +44,9 @@ module ActiveModel
|
|||
end
|
||||
|
||||
private
|
||||
|
||||
def _singularize(string, replacement='_')
|
||||
ActiveSupport::Inflector.underscore(string).tr('/', replacement)
|
||||
end
|
||||
def _singularize(str)
|
||||
ActiveSupport::Inflector.underscore(str).tr('/', '_')
|
||||
end
|
||||
end
|
||||
|
||||
# == Active Model Naming
|
||||
|
@ -64,9 +62,6 @@ module ActiveModel
|
|||
# BookCover.model_name # => "BookCover"
|
||||
# BookCover.model_name.human # => "Book cover"
|
||||
#
|
||||
# BookCover.model_name.i18n_key # => "book_cover"
|
||||
# BookModule::BookCover.model_name.i18n_key # => "book_module.book_cover"
|
||||
#
|
||||
# Providing the functionality that ActiveModel::Naming provides in your object
|
||||
# is required to pass the Active Model Lint test. So either extending the provided
|
||||
# method below, or rolling your own is required.
|
||||
|
|
|
@ -44,7 +44,7 @@ module ActiveModel
|
|||
# Specify +options+ with additional translating options.
|
||||
def human_attribute_name(attribute, options = {})
|
||||
defaults = lookup_ancestors.map do |klass|
|
||||
:"#{self.i18n_scope}.attributes.#{klass.model_name.i18n_key}.#{attribute}"
|
||||
:"#{self.i18n_scope}.attributes.#{klass.model_name.underscore}.#{attribute}"
|
||||
end
|
||||
|
||||
defaults << :"attributes.#{attribute}"
|
||||
|
|
|
@ -55,14 +55,6 @@ class I18nValidationTest < ActiveModel::TestCase
|
|||
assert_equal ["Person's name not found"], @person.errors.full_messages
|
||||
end
|
||||
|
||||
def test_errors_full_messages_translates_human_attribute_name_for_model_in_module_attributes
|
||||
I18n.backend.store_translations('en', :activemodel => {:attributes => {:person_module => {:person => {:name => "Person in Module's name"}}}})
|
||||
person = PersonModule::Person.new
|
||||
person.errors.add(:name, 'not found')
|
||||
PersonModule::Person.expects(:human_attribute_name).with(:name, :default => 'Name').returns("Person in Module's name")
|
||||
assert_equal ["Person in Module's name not found"], person.errors.full_messages
|
||||
end
|
||||
|
||||
def test_errors_full_messages_uses_format
|
||||
I18n.backend.store_translations('en', :errors => {:format => "Field %{attribute} %{message}"})
|
||||
@person.errors.add('name', 'empty')
|
||||
|
@ -371,15 +363,4 @@ class I18nValidationTest < ActiveModel::TestCase
|
|||
assert_equal ["I am a custom error"], @person.errors[:title]
|
||||
end
|
||||
|
||||
def test_model_with_module_i18n_scope
|
||||
I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person_module => {:person => {:blank => 'generic blank'}}}}}
|
||||
PersonModule::Person.validates_presence_of :title
|
||||
person = PersonModule::Person.new
|
||||
person.valid?
|
||||
assert_equal ['generic blank'], person.errors[:title]
|
||||
|
||||
I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person_module => {:person => {:attributes => {:title => {:blank => 'title cannot be blank'}}}}}}}
|
||||
person.valid?
|
||||
assert_equal ['title cannot be blank'], person.errors[:title]
|
||||
end
|
||||
end
|
||||
|
|
|
@ -15,8 +15,3 @@ end
|
|||
|
||||
class Child < Person
|
||||
end
|
||||
|
||||
module PersonModule
|
||||
class Person < ::Person
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue