mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
validates_confirmation_of
does not override writer methods.
This commit is contained in:
parent
b359c5db9f
commit
b501ee47fa
3 changed files with 51 additions and 1 deletions
|
@ -1,5 +1,20 @@
|
||||||
## Rails 4.0.0 (unreleased) ##
|
## 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)
|
||||||
|
@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) ##
|
## Rails 4.0.0.beta1 (February 25, 2013) ##
|
||||||
|
|
||||||
|
|
|
@ -10,9 +10,13 @@ module ActiveModel
|
||||||
end
|
end
|
||||||
|
|
||||||
def setup(klass)
|
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")
|
:"#{attribute}_confirmation" unless klass.method_defined?(:"#{attribute}_confirmation")
|
||||||
end.compact)
|
end.compact)
|
||||||
|
|
||||||
|
klass.send(:attr_writer, *attributes.map do |attribute|
|
||||||
|
:"#{attribute}_confirmation" unless klass.method_defined?(:"#{attribute}_confirmation=")
|
||||||
|
end.compact)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -71,4 +71,35 @@ class ConfirmationValidationTest < ActiveModel::TestCase
|
||||||
I18n.backend = @old_backend
|
I18n.backend = @old_backend
|
||||||
end
|
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
|
end
|
||||||
|
|
Loading…
Reference in a new issue