diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index 1431228..4ee8ed2 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -289,8 +289,8 @@ module ActiveModel end defaults = @base.class.lookup_ancestors.map do |klass| - [ :"#{@base.class.i18n_scope}.errors.models.#{klass.model_name.underscore}.attributes.#{attribute}.#{type}", - :"#{@base.class.i18n_scope}.errors.models.#{klass.model_name.underscore}.#{type}" ] + [ :"#{@base.class.i18n_scope}.errors.models.#{klass.i18n_key}.attributes.#{attribute}.#{type}", + :"#{@base.class.i18n_scope}.errors.models.#{klass.i18n_key}.#{type}" ] end defaults << options.delete(:message) diff --git a/activemodel/lib/active_model/naming.rb b/activemodel/lib/active_model/naming.rb index d79635c..2fa0655 100644 --- a/activemodel/lib/active_model/naming.rb +++ b/activemodel/lib/active_model/naming.rb @@ -27,7 +27,7 @@ module ActiveModel @klass.respond_to?(:i18n_scope) defaults = @klass.lookup_ancestors.map do |klass| - klass.model_name.underscore.to_sym + klass.i18n_key end defaults << options.delete(:default) if options[:default] @@ -40,7 +40,7 @@ module ActiveModel # == Active Model Naming # - # Creates a +model_name+ method on your object. + # Creates +model_name+ and +i18n_key+ methods on your object. # # To implement, just extend ActiveModel::Naming in your object: # @@ -51,6 +51,9 @@ module ActiveModel # BookCover.model_name # => "BookCover" # BookCover.model_name.human # => "Book cover" # + # BookCover.i18n_key # => "book_cover" + # BookModule::BookCover.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.. @@ -61,6 +64,11 @@ module ActiveModel @_model_name ||= ActiveModel::Name.new(self) end + # Returns an i18n key symbol for module. + def i18n_key + @_i18n_key ||= model_name.underscore.tr('/', '.').to_sym + end + # Returns the plural class name of a record or class. Examples: # # ActiveModel::Naming.plural(post) # => "posts" diff --git a/activemodel/lib/active_model/translation.rb b/activemodel/lib/active_model/translation.rb index 6c1cecd..a03c6f6 100644 --- a/activemodel/lib/active_model/translation.rb +++ b/activemodel/lib/active_model/translation.rb @@ -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.underscore}.#{attribute}" + :"#{self.i18n_scope}.attributes.#{klass.i18n_key}.#{attribute}" end defaults << :"attributes.#{attribute}" diff --git a/activemodel/test/cases/validations/i18n_validation_test.rb b/activemodel/test/cases/validations/i18n_validation_test.rb index e9f0e43..a193364 100644 --- a/activemodel/test/cases/validations/i18n_validation_test.rb +++ b/activemodel/test/cases/validations/i18n_validation_test.rb @@ -55,6 +55,14 @@ 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') @@ -363,4 +371,17 @@ class I18nValidationTest < ActiveModel::TestCase assert_equal ["I am a custom error"], @person.errors[:title] end + # test class i18n scope with Module + + 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 :title => nil + 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 diff --git a/activemodel/test/models/person.rb b/activemodel/test/models/person.rb index cf16a38..eb84f7a 100644 --- a/activemodel/test/models/person.rb +++ b/activemodel/test/models/person.rb @@ -11,3 +11,8 @@ end class Child < Person end + +module PersonModule + class Person < ::Person + end +end