Skip to content

Commit

Permalink
Support i18n attributes for confirmation
Browse files Browse the repository at this point in the history
  • Loading branch information
bcardarella committed Apr 24, 2012
1 parent 3a749a9 commit 4433b1a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
3 changes: 2 additions & 1 deletion activemodel/lib/active_model/validations/confirmation.rb
Expand Up @@ -5,7 +5,8 @@ module Validations
class ConfirmationValidator < EachValidator
def validate_each(record, attribute, value)
if (confirmed = record.send("#{attribute}_confirmation")) && (value != confirmed)
record.errors.add(:"#{attribute}_confirmation", :confirmation, options.merge(:attribute => attribute))
human_attribute_name = record.class.human_attribute_name(attribute)
record.errors.add(:"#{attribute}_confirmation", :confirmation, options.merge(:attribute => human_attribute_name))
end
end

Expand Down
Expand Up @@ -44,12 +44,31 @@ def test_validates_confirmation_of_for_ruby_class
p.karma_confirmation = "None"
assert p.invalid?

assert_equal ["doesn't match karma"], p.errors[:karma_confirmation]
assert_equal ["doesn't match Karma"], p.errors[:karma_confirmation]

p.karma = "None"
assert p.valid?
ensure
Person.reset_callbacks(:validate)
end

def test_title_confirmation_with_i18n_attribute
@old_load_path, @old_backend = I18n.load_path.dup, I18n.backend
I18n.load_path.clear
I18n.backend = I18n::Backend::Simple.new
I18n.backend.store_translations('en', {
:errors => {:messages => {:confirmation => "doesn't match %{attribute}"}},
:activemodel => {:attributes => {:topic => {:title => 'Test Title'}}}
})

Topic.validates_confirmation_of(:title)

t = Topic.new("title" => "We should be confirmed","title_confirmation" => "")
assert t.invalid?
assert_equal ["doesn't match Test Title"], t.errors[:title_confirmation]

I18n.load_path.replace @old_load_path
I18n.backend = @old_backend
end

end
2 changes: 1 addition & 1 deletion activemodel/test/cases/validations/i18n_validation_test.rb
Expand Up @@ -81,7 +81,7 @@ def test_errors_full_messages_uses_format
test "validates_confirmation_of on generated message #{name}" do
Person.validates_confirmation_of :title, validation_options
@person.title_confirmation = 'foo'
@person.errors.expects(:generate_message).with(:title_confirmation, :confirmation, generate_message_options.merge(:attribute => :title))
@person.errors.expects(:generate_message).with(:title_confirmation, :confirmation, generate_message_options.merge(:attribute => 'Title'))
@person.valid?
end
end
Expand Down

0 comments on commit 4433b1a

Please sign in to comment.