Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions activemodel/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Rails 4.0.0 (unreleased) ##

* `ConfirmationValidator` error messages will attach to `:#{attribute}_confirmation` instead of `attribute` *Brian Cardarella*

* Added ActiveModel::Model, a mixin to make Ruby objects work with AP out of box *Guillermo Iguaran*

* `AM::Errors#to_json`: support `:full_messages` parameter *Bogdan Gusiev*
Expand Down
2 changes: 1 addition & 1 deletion activemodel/lib/active_model/locale/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ en:
inclusion: "is not included in the list"
exclusion: "is reserved"
invalid: "is invalid"
confirmation: "doesn't match confirmation"
confirmation: "doesn't match %{attribute}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this right? I mean the %{}. Should it be #{}?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it is, the pattern is %{} :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay. Just checking :D

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem, always good to have code review ;)

accepted: "must be accepted"
empty: "can't be empty"
blank: "can't be blank"
Expand Down
3 changes: 2 additions & 1 deletion activemodel/lib/active_model/validations/confirmation.rb
Original file line number Diff line number Diff line change
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, options)
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
Original file line number Diff line number Diff line change
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 confirmation"], p.errors[:karma]
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
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_generate_message_invalid_with_custom_message

# validates_confirmation_of: generate_message(attr_name, :confirmation, :message => custom_message)
def test_generate_message_confirmation_with_default_message
assert_equal "doesn't match confirmation", @person.errors.generate_message(:title, :confirmation)
assert_equal "doesn't match Title", @person.errors.generate_message(:title, :confirmation)
end

def test_generate_message_confirmation_with_custom_message
Expand Down
17 changes: 11 additions & 6 deletions activemodel/test/cases/validations/i18n_validation_test.rb
Original file line number Diff line number Diff line change
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, generate_message_options)
@person.errors.expects(:generate_message).with(:title_confirmation, :confirmation, generate_message_options.merge(:attribute => 'Title'))
@person.valid?
end
end
Expand Down Expand Up @@ -217,24 +217,29 @@ def test_errors_full_messages_uses_format

# To make things DRY this macro is defined to define 3 tests for every validation case.
def self.set_expectations_for_validation(validation, error_type, &block_that_sets_validation)
if error_type == :confirmation
attribute = :title_confirmation
else
attribute = :title
end
# test "validates_confirmation_of finds custom model key translation when blank"
test "#{validation} finds custom model key translation when #{error_type}" do
I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {error_type => 'custom message'}}}}}}
I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {attribute => {error_type => 'custom message'}}}}}}
I18n.backend.store_translations 'en', :errors => {:messages => {error_type => 'global message'}}

yield(@person, {})
@person.valid?
assert_equal ['custom message'], @person.errors[:title]
assert_equal ['custom message'], @person.errors[attribute]
end

# test "validates_confirmation_of finds custom model key translation with interpolation when blank"
test "#{validation} finds custom model key translation with interpolation when #{error_type}" do
I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {error_type => 'custom message with %{extra}'}}}}}}
I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {attribute => {error_type => 'custom message with %{extra}'}}}}}}
I18n.backend.store_translations 'en', :errors => {:messages => {error_type => 'global message'}}

yield(@person, {:extra => "extra information"})
@person.valid?
assert_equal ['custom message with extra information'], @person.errors[:title]
assert_equal ['custom message with extra information'], @person.errors[attribute]
end

# test "validates_confirmation_of finds global default key translation when blank"
Expand All @@ -243,7 +248,7 @@ def self.set_expectations_for_validation(validation, error_type, &block_that_set

yield(@person, {})
@person.valid?
assert_equal ['global message'], @person.errors[:title]
assert_equal ['global message'], @person.errors[attribute]
end
end

Expand Down
2 changes: 1 addition & 1 deletion activemodel/test/cases/validations/validates_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,6 @@ def test_defining_extra_default_keys_for_validates
topic.title = "What's happening"
topic.title_confirmation = "Not this"
assert !topic.valid?
assert_equal ['Y U NO CONFIRM'], topic.errors[:title]
assert_equal ['Y U NO CONFIRM'], topic.errors[:title_confirmation]
end
end