Skip to content
This repository
Browse code

ActiveModel::Errors#generate_message without i18n_scope, and more tes…

…t cases for #add
  • Loading branch information...
commit 180d4137ca8222cb90a285bfd60265ae93c56968 1 parent 8919a68
Martin Svalin authored
12  activemodel/lib/active_model/errors.rb
@@ -300,13 +300,17 @@ def full_message(attribute, message)
300 300
     def generate_message(attribute, type = :invalid, options = {})
301 301
       type = options.delete(:message) if options[:message].is_a?(Symbol)
302 302
 
303  
-      defaults = @base.class.lookup_ancestors.map do |klass|
304  
-        [ :"#{@base.class.i18n_scope}.errors.models.#{klass.model_name.i18n_key}.attributes.#{attribute}.#{type}",
305  
-          :"#{@base.class.i18n_scope}.errors.models.#{klass.model_name.i18n_key}.#{type}" ]
  303
+      if @base.class.respond_to?(:i18n_scope)
  304
+        defaults = @base.class.lookup_ancestors.map do |klass|
  305
+          [ :"#{@base.class.i18n_scope}.errors.models.#{klass.model_name.i18n_key}.attributes.#{attribute}.#{type}",
  306
+            :"#{@base.class.i18n_scope}.errors.models.#{klass.model_name.i18n_key}.#{type}" ]
  307
+        end
  308
+      else
  309
+        defaults = []
306 310
       end
307 311
 
308 312
       defaults << options.delete(:message)
309  
-      defaults << :"#{@base.class.i18n_scope}.errors.messages.#{type}"
  313
+      defaults << :"#{@base.class.i18n_scope}.errors.messages.#{type}" if @base.class.respond_to?(:i18n_scope)
310 314
       defaults << :"errors.attributes.#{attribute}.#{type}"
311 315
       defaults << :"errors.messages.#{type}"
312 316
 
21  activemodel/test/cases/errors_test.rb
@@ -66,6 +66,20 @@ def test_has_key?
66 66
     assert_equal ["can not be blank"], person.errors[:name]
67 67
   end
68 68
 
  69
+  test "should be able to add an error with a symbol" do
  70
+    person = Person.new
  71
+    person.errors.add(:name, :blank)
  72
+    message = person.errors.generate_message(:name, :blank)
  73
+    assert_equal [message], person.errors[:name]
  74
+  end
  75
+
  76
+  test "should be able to add an error with a proc" do
  77
+    person = Person.new
  78
+    message = Proc.new { "can not be blank" }
  79
+    person.errors.add(:name, message)
  80
+    assert_equal ["can not be blank"], person.errors[:name]
  81
+  end
  82
+
69 83
   test 'should respond to size' do
70 84
     person = Person.new
71 85
     person.errors.add(:name, "can not be blank")
@@ -112,5 +126,12 @@ def test_has_key?
112 126
     assert_equal ["is invalid"], hash[:email]
113 127
   end
114 128
 
  129
+  test "generate_message should work without i18n_scope" do
  130
+    person = Person.new
  131
+    assert !Person.respond_to?(:i18n_scope)
  132
+    assert_nothing_raised {
  133
+      person.errors.generate_message(:name, :blank)
  134
+    }
  135
+  end
115 136
 end
116 137
 

0 notes on commit 180d413

Please sign in to comment.
Something went wrong with that request. Please try again.