Permalink
Browse files

Regression with how base errors messages are added to a model. Works …

…correctly for both string error messages and symbol translated messages.

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information...
1 parent 7008911 commit e8c064bbe0fb5e07c7ceaa45d0cafa3c4ef01ab0 @joshk joshk committed with josevalim Jun 23, 2010
@@ -16,7 +16,7 @@ def on_base
end
def add_to_base(msg)
- ActiveSupport::Deprecation.warn "Errors#add_to_base(msg) has been deprecated, use Errors#[:base] << msg instead"
+ ActiveSupport::Deprecation.warn "Errors#add_to_base(msg) has been deprecated, use Errors#add(:base, msg) instead"
self[:base] << msg
end
@@ -308,7 +308,7 @@ def generate_message(attribute, type = :invalid, options = {})
defaults.flatten!
key = defaults.shift
- value = @base.send(:read_attribute_for_validation, attribute)
+ value = (attribute != :base ? @base.send(:read_attribute_for_validation, attribute) : nil)
options = {
:default => defaults,
@@ -83,10 +83,9 @@ def test_errors_on_base
r = Reply.new
r.content = "Mismatch"
r.valid?
- r.errors[:base] << "Reply is not dignifying"
+ r.errors.add(:base, "Reply is not dignifying")
- errors = []
- r.errors.to_a.each { |error| errors << error }
+ errors = r.errors.to_a.inject([]) { |result, error| result + [error] }
assert_equal ["Reply is not dignifying"], r.errors[:base]
@@ -95,6 +94,22 @@ def test_errors_on_base
assert_equal 2, r.errors.count
end
+ def test_errors_on_base_with_symbol_message
+ r = Reply.new
+ r.content = "Mismatch"
+ r.valid?
+ r.errors.add(:base, :invalid)
+
+ errors = r.errors.to_a.inject([]) { |result, error| result + [error] }
+
+ assert_equal ["is invalid"], r.errors[:base]
+
+ assert errors.include?("Title is Empty")
+ assert errors.include?("is invalid")
+
+ assert_equal 2, r.errors.count
+ end
+
def test_errors_empty_after_errors_on_check
t = Topic.new
assert t.errors[:id].empty?

0 comments on commit e8c064b

Please sign in to comment.