diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2005-04-17 10:04:07 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2005-04-17 10:04:07 +0000 |
commit | d5b67ed8d33dbde44d24d7d93135261062e550d1 (patch) | |
tree | a034208841067902096469abedb12cc8e304cfdd | |
parent | 154668898aadef2d98093cd3c2b7c895fff8b7a6 (diff) | |
download | rails-d5b67ed8d33dbde44d24d7d93135261062e550d1.tar.gz rails-d5b67ed8d33dbde44d24d7d93135261062e550d1.tar.bz2 rails-d5b67ed8d33dbde44d24d7d93135261062e550d1.zip |
Added the option to specify the acceptance string in validates_acceptance_of #1106 [caleb@aei-tech.com]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1188 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r-- | activerecord/CHANGELOG | 2 | ||||
-rwxr-xr-x | activerecord/lib/active_record/validations.rb | 8 | ||||
-rwxr-xr-x | activerecord/test/validations_test.rb | 11 |
3 files changed, 18 insertions, 3 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 3c54b9cd2e..6229de9224 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Added the option to specify the acceptance string in validates_acceptance_of #1106 [caleb@aei-tech.com] + * Added acts_as_nested_set #1000 [wschenk]. Introduction: This acts provides Nested Set functionality. Nested Set is similiar to Tree, but with diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index ad951b9c25..af92ac6db8 100755 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -291,16 +291,18 @@ module ActiveRecord # Configuration options: # * <tt>message</tt> - A custom error message (default is: "can't be empty") # * <tt>on</tt> - Specifies when this validation is active (default is :save, other options :create, :update) + # * <tt>accept</tt> - Specifies value that is considered accepted. The default value is a string "1", which + # makes it easy to relate to an HTML checkbox. # - # 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 validates_acceptance_of(*attr_names) - configuration = { :message => ActiveRecord::Errors.default_error_messages[:accepted], :on => :save, :allow_nil => true } + configuration = { :message => ActiveRecord::Errors.default_error_messages[:accepted], :on => :save, :allow_nil => true, :accept => "1" } configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) attr_accessor *attr_names validates_each(attr_names,configuration) do |record, attr_name, value| - record.errors.add(attr_name, configuration[:message]) unless value == "1" + record.errors.add(attr_name, configuration[:message]) unless value == configuration[:accept] end end diff --git a/activerecord/test/validations_test.rb b/activerecord/test/validations_test.rb index 30de66c94b..736cad140a 100755 --- a/activerecord/test/validations_test.rb +++ b/activerecord/test/validations_test.rb @@ -189,6 +189,17 @@ class ValidationsTest < Test::Unit::TestCase assert t.save end + def test_terms_of_service_agreement_with_accept_value + Topic.validates_acceptance_of(:terms_of_service, :on => :create, :accept => "I agree.") + + t = Topic.create("title" => "We should be confirmed", "terms_of_service" => "") + assert !t.save + assert_equal "must be accepted", t.errors.on(:terms_of_service) + + t.terms_of_service = "I agree." + assert t.save + end + def test_validate_presences Topic.validates_presence_of(:title, :content) |