Permalink
Browse files

Updating errors to friendly use [] like ActiveRecord errors, playing …

…nicely with Formtastic.
  • Loading branch information...
1 parent 5669973 commit 89813b17f17d1e3474c9e83b66ff3b0704fa5515 @carlosantoniodasilva carlosantoniodasilva committed with josevalim Oct 3, 2009
Showing with 22 additions and 2 deletions.
  1. +4 −2 lib/simple_form/errors.rb
  2. +18 −0 test/errors_test.rb
@@ -31,13 +31,15 @@ def initialize(base, *args)
alias :add :store
alias :count :size
+ alias :get :[]
def on(attribute)
attribute = attribute.to_sym
- return nil unless self[attribute]
+ return nil unless get(attribute)
- generate_message_for(attribute, self[attribute])
+ generate_message_for(attribute, get(attribute))
end
+ alias :[] :on
def full_messages
map do |attribute, message|
View
@@ -43,6 +43,24 @@ def test_on_returns_an_attribute_localized_message_in_the_given_attribute
assert_equal nil, form.errors.on(:message)
end
+ def test_array_like_option_acts_as_an_alias_for_on
+ form = ContactForm.new(:email => 'not_valid')
+ form.valid?
+ assert_equal "can't be blank", form.errors[:name]
+ assert_equal form.errors.on(:name), form.errors[:name]
+ assert_equal "is invalid", form.errors[:email]
+ assert_equal form.errors.on(:email), form.errors[:email]
+ assert_equal nil, form.errors[:message]
+ end
+
+ def test_get_returns_the_real_value_in_the_given_attribute
+ form = ContactForm.new(:email => 'not_valid')
+ form.valid?
+ assert_equal :blank, form.errors.get(:name)
+ assert_equal :invalid, form.errors.get(:email)
+ assert_equal nil, form.errors.get(:message)
+ end
+
def test_full_messages
form = ContactForm.new(:email => 'not_valid')
form.valid?

0 comments on commit 89813b1

Please sign in to comment.