Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

`validates_confirmation_of` does not override writer methods.

  • Loading branch information...
commit b501ee47fa3f877f8b8028e732f8ef8a22cc75fb 1 parent b359c5d
@senny senny authored
View
15 activemodel/CHANGELOG.md
@@ -1,5 +1,20 @@
## Rails 4.0.0 (unreleased) ##
+* `validates_confirmation_of` does not override writer methods for
+ the confirmation attribute if no reader is defined.
+
+ Example:
+
+ class Blog
+ def title=(new_title)
@josevalim Owner

Isn't this the wrong indentation?

@senny Owner
senny added a note

it is but looks like it was fixed on 4-0-stable and it's not on master anymore:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ @title = new_title.downcase
+ end
+
+ # previously this would override the setter above.
+ validates_confirmation_of :title
+ end
+
+ *Yves Senn*
## Rails 4.0.0.beta1 (February 25, 2013) ##
View
6 activemodel/lib/active_model/validations/confirmation.rb
@@ -10,9 +10,13 @@ def validate_each(record, attribute, value)
end
def setup(klass)
- klass.send(:attr_accessor, *attributes.map do |attribute|
+ klass.send(:attr_reader, *attributes.map do |attribute|
:"#{attribute}_confirmation" unless klass.method_defined?(:"#{attribute}_confirmation")
end.compact)
+
+ klass.send(:attr_writer, *attributes.map do |attribute|
+ :"#{attribute}_confirmation" unless klass.method_defined?(:"#{attribute}_confirmation=")
+ end.compact)
end
end
View
31 activemodel/test/cases/validations/confirmation_validation_test.rb
@@ -71,4 +71,35 @@ def test_title_confirmation_with_i18n_attribute
I18n.backend = @old_backend
end
+ test "does not override confirmation reader if present" do
+ klass = Class.new do
+ include ActiveModel::Validations
+
+ def title_confirmation
+ "expected title"
+ end
+
+ validates_confirmation_of :title
+ end
+
+ assert_equal "expected title", klass.new.title_confirmation,
+ "confirmation validation should not override the reader"
+ end
+
+ test "does not override confirmation writer if present" do
+ klass = Class.new do
+ include ActiveModel::Validations
+
+ def title_confirmation=(value)
+ @title_confirmation = "expected title"
+ end
+
+ validates_confirmation_of :title
+ end
+
+ model = klass.new
+ model.title_confirmation = "new title"
+ assert_equal "expected title", model.title_confirmation,
+ "confirmation validation should not override the writer"
+ end
end
@josevalim

Isn't this the wrong indentation?

@senny

it is but looks like it was fixed on 4-0-stable and it's not on master anymore:

Please sign in to comment.
Something went wrong with that request. Please try again.