Skip to content

Commit

Permalink
Added Base.validate_confirmation that encapsulates the pattern of wan…
Browse files Browse the repository at this point in the history
…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
dhh committed Dec 9, 2004
1 parent fc817ef commit a65f791
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
17 changes: 17 additions & 0 deletions activerecord/CHANGELOG
@@ -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
Expand Down
31 changes: 31 additions & 0 deletions activerecord/lib/active_record/validations.rb
Expand Up @@ -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
Expand Down
26 changes: 22 additions & 4 deletions activerecord/test/validations_test.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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.