1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Added Base.validate_confirmation that encapsulates the pattern of wanting to validate the acceptance of a terms of service check box (or similar agreement)

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@97 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
David Heinemeier Hansson 2004-12-09 14:37:37 +00:00
parent d33f814db5
commit 27ea0b527e
3 changed files with 55 additions and 13 deletions

View file

@ -17,6 +17,22 @@
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 Base.validate_confirmation that encapsulates the pattern of wanting to validate the acceptance of a terms of service check box (or similar agreement). Example:
Model:
class Person < ActiveRecord::Base
validate_acceptance :terms_of_service
end
View:
<%= check_box "person", "terms_of_service" %>
The terms_of_service attribute is entirely virtual. It's only used for validation at the time of creation. No database column is needed.
NOTE: The agreement is considered valid if it's set to the string "1". This makes it easy to relate it to an HTML checkbox.
* Added validation macros to make the stackable just like the lifecycle callbacks. Examples:
class Person < ActiveRecord::Base

View file

@ -80,6 +80,29 @@ module ActiveRecord
EOM
end
end
# Encapsulates the pattern of wanting to validate the acceptance of a terms of service check box (or similar agreement). Example:
#
# Model:
# class Person < ActiveRecord::Base
# validate_acceptance :terms_of_service
# end
#
# View:
# <%= check_box "person", "terms_of_service" %>
#
# The terms_of_service attribute is entirely virtual. It's only used for validation at the time of creation. No database column is needed.
#
# NOTE: The agreement is considered valid if it's set to the string "1". This makes it easy to relate it to an HTML checkbox.
def validate_acceptance(*attr_names)
for attr_name in attr_names
attr_accessor(attr_name)
class_eval <<-EOM
validate_on_create %{errors.add('#{attr_name}', "must be accepted") unless #{attr_name} == "1"}
EOM
end
end
end
# The validation process on save can be skipped by passing false. The regular Base#save method is

View file

@ -120,25 +120,28 @@ class ValidationsTest < Test::Unit::TestCase
developer.name = "Just right"
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
Topic.validate_confirmation(:title)
t = Topic.create("title" => "We should be confirmed")
assert !t.save
t.title_confirmation = "We should be confirmed"
assert t.save
Topic.write_inheritable_attribute("validate_on_create", [])
end
def test_terms_of_service_agreement
Topic.validate_acceptance(:terms_of_service)
t = Topic.create("title" => "We should be confirmed")
assert !t.save
t.terms_of_service = "1"
assert t.save
Topic.write_inheritable_attribute("validate_on_create", [])
end
end