Permalink
Browse files

Added Base.validate_confirmation that encapsulates the pattern of wan…

…ting to validate a password or email address field with a confirmation.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@95 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent fc817ef commit a65f791fbd8cf16d17a963f32c7b85a5659c04e0 @dhh dhh committed Dec 9, 2004
Showing with 70 additions and 4 deletions.
  1. +17 −0 activerecord/CHANGELOG
  2. +31 −0 activerecord/lib/active_record/validations.rb
  3. +22 −4 activerecord/test/validations_test.rb
@@ -1,5 +1,22 @@
*CVS*
+* Added Base.validate_confirmation that encapsulates the pattern of wanting to validate a password or email address field with a confirmation. Example:
+
+ Model:
+ class Person < ActiveRecord::Base
+ validate_confirmation :password
+ end
+
+ View:
+ <%= password_field "person", "password" %>
+ <%= password_field "person", "password_confirmation" %>
+
+ The person has to already have a password attribute (a column in the people table), but the password_confirmation is virtual.
+ It exists only as an in-memory variable for validating the password.
+
+ NOTE: This validation is only happening on create. When you want to update the record, you'll have to decide and pursue your
+ own course of action.
+
* Added validation macros to make the stackable just like the lifecycle callbacks. Examples:
class Person < ActiveRecord::Base
@@ -51,6 +51,37 @@ def self.append_features(base) # :nodoc:
VALIDATIONS.each { |vd| base.class_eval("def self.#{vd}(*methods) write_inheritable_array(\"#{vd}\", methods - (read_inheritable_attribute(\"#{vd}\") || [])) end") }
end
+
+ base.extend(ClassMethods)
+ end
+
+ module ClassMethods
+ # Encapsulates the pattern of wanting to validate a password or email address field with a confirmation. Example:
+ #
+ # Model:
+ # class Person < ActiveRecord::Base
+ # validate_confirmation :password
+ # end
+ #
+ # View:
+ # <%= password_field "person", "password" %>
+ # <%= password_field "person", "password_confirmation" %>
+ #
+ # The person has to already have a password attribute (a column in the people table), but the password_confirmation is virtual.
+ # It exists only as an in-memory variable for validating the password.
+ #
+ # NOTE: This validation is only happening on create. When you want to update the record, you'll have to decide and pursue your
+ # own course of action.
+ def validate_confirmation(*attr_names)
+ for attr_name in attr_names
+ attr_accessor "#{attr_name}_confirmation"
+ class_eval <<-EOC
+ validate_on_create(Proc.new { |record|
+ record.errors.add("#{attr_name}", "doesn't match confirmation") unless record.#{attr_name} == record.#{attr_name}_confirmation
+ })
+EOC
+ end
+ end
end
# The validation process on save can be skipped by passing false. The regular Base#save method is
@@ -5,10 +5,7 @@
class ValidationsTest < Test::Unit::TestCase
- def setup
- @topic_fixtures = create_fixtures("topics")
- @developers = create_fixtures("developers")
- end
+ fixtures :topics, :developers
def test_single_field_validation
r = Reply.new
@@ -124,3 +121,24 @@ def test_errors_on_boundary_breaking
assert developer.save
end
end
+
+
+class MacroValidationsTest < Test::Unit::TestCase
+ fixtures :topics, :developers
+
+ def setup
+ Topic.validate_confirmation(:title)
+ end
+
+ def teardown
+ Topic.write_inheritable_attribute("validate_on_create", [])
+ end
+
+ def test_title_confirmation
+ t = Topic.create("title" => "We should be confirmed")
+ assert !t.save
+
+ t.title_confirmation = "We should be confirmed"
+ assert t.save
+ end
+end

0 comments on commit a65f791

Please sign in to comment.