aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord')
-rwxr-xr-xactiverecord/lib/active_record/validations.rb8
-rw-r--r--activerecord/test/cases/validations_i18n_test.rb62
2 files changed, 63 insertions, 7 deletions
diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb
index 49d3c59ca7..5bbd10394c 100755
--- a/activerecord/lib/active_record/validations.rb
+++ b/activerecord/lib/active_record/validations.rb
@@ -21,8 +21,8 @@ module ActiveRecord
class << self
def default_error_messages
- # ActiveSupport::Deprecation.warn("ActiveRecord::Errors.default_error_messages has been deprecated. Please use :'active_record.error_messages'.t.")
- :'active_record.error_messages'.t
+ ActiveSupport::Deprecation.warn("ActiveRecord::Errors.default_error_messages has been deprecated. Please use 'active_record.error_messages'.t.")
+ 'active_record.error_messages'.t
end
end
@@ -163,7 +163,7 @@ module ActiveRecord
@errors.each_key do |attr|
@errors[attr].each do |message|
next unless message
-
+
if attr == "base"
full_messages << message
else
@@ -872,7 +872,7 @@ module ActiveRecord
end
raw_value = raw_value.to_i
else
- begin
+ begin
raw_value = Kernel.Float(raw_value.to_s)
rescue ArgumentError, TypeError
message = record.errors.generate_message(attr_name, :not_a_number, :value => raw_value, :default => configuration[:message])
diff --git a/activerecord/test/cases/validations_i18n_test.rb b/activerecord/test/cases/validations_i18n_test.rb
index 37a7c1ce49..158ff69e57 100644
--- a/activerecord/test/cases/validations_i18n_test.rb
+++ b/activerecord/test/cases/validations_i18n_test.rb
@@ -34,6 +34,12 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase
end
end
+ def test_default_error_messages_is_deprecated
+ assert_deprecated('ActiveRecord::Errors.default_error_messages') do
+ ActiveRecord::Errors.default_error_messages
+ end
+ end
+
# ActiveRecord::Errors
def test_errors_generate_message_translates_custom_model_attribute_key
@@ -182,18 +188,32 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase
# validates_length_of :within
- def test_validates_length_of_within_generates_message
+ def test_validates_length_of_within_generates_message_with_title_too_short
Topic.validates_length_of :title, :within => 3..5
@topic.errors.expects(:generate_message).with(:title, :too_short, {:count => 3, :default => nil})
@topic.valid?
end
- def test_validates_length_of_within_generates_message_with_custom_default_message
+ def test_validates_length_of_within_generates_message_with_title_too_short_and_custom_default_message
Topic.validates_length_of :title, :within => 3..5, :too_short => 'custom'
@topic.errors.expects(:generate_message).with(:title, :too_short, {:count => 3, :default => 'custom'})
@topic.valid?
end
+ def test_validates_length_of_within_generates_message_with_title_too_long
+ Topic.validates_length_of :title, :within => 3..5
+ @topic.title = 'this title is too long'
+ @topic.errors.expects(:generate_message).with(:title, :too_long, {:count => 5, :default => nil})
+ @topic.valid?
+ end
+
+ def test_validates_length_of_within_generates_message_with_title_too_long_and_custom_default_message
+ Topic.validates_length_of :title, :within => 3..5, :too_long => 'custom'
+ @topic.title = 'this title is too long'
+ @topic.errors.expects(:generate_message).with(:title, :too_long, {:count => 5, :default => 'custom'})
+ @topic.valid?
+ end
+
def test_validates_length_of_within_finds_custom_model_key_translation
I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:custom => {:topic => {:title => {:too_short => 'custom message'}}}}}
I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:too_short => 'global message'}}
@@ -382,7 +402,43 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase
end
- # validates_numericality_of :only_integer
+ # validates_numericality_of without :only_integer
+
+ def test_validates_numericality_of_generates_message
+ Topic.validates_numericality_of :title
+ @topic.title = 'a'
+ @topic.errors.expects(:generate_message).with(:title, :not_a_number, {:value => 'a', :default => nil})
+ @topic.valid?
+ end
+
+ def test_validates_numericality_of_generates_message_with_custom_default_message
+ Topic.validates_numericality_of :title, :message => 'custom'
+ @topic.title = 'a'
+ @topic.errors.expects(:generate_message).with(:title, :not_a_number, {:value => 'a', :default => 'custom'})
+ @topic.valid?
+ end
+
+ def test_validates_numericality_of_finds_custom_model_key_translation
+ I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:custom => {:topic => {:title => {:not_a_number => 'custom message'}}}}}
+ I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:not_a_number => 'global message'}}
+
+ Topic.validates_numericality_of :title
+ @topic.title = 'a'
+ @topic.valid?
+ assert_equal 'custom message', @topic.errors.on(:title)
+ end
+
+ def test_validates_numericality_of_finds_global_default_translation
+ I18n.backend.store_translations 'en-US', :active_record => {:error_messages => {:not_a_number => 'global message'}}
+
+ Topic.validates_numericality_of :title, :only_integer => true
+ @topic.title = 'a'
+ @topic.valid?
+ assert_equal 'global message', @topic.errors.on(:title)
+ end
+
+
+ # validates_numericality_of with :only_integer
def test_validates_numericality_of_only_integer_generates_message
Topic.validates_numericality_of :title, :only_integer => true