提交 0c72e6d6 编写于 作者: D David Heinemeier Hansson

Options for the new validation methods are now given as a hash

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@109 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
上级 aaf9a45c
......@@ -324,7 +324,7 @@ def rollback_db_transaction() end
def quote(value, column = nil)
case value
when String then %('#{quote_string(value)}') # ' (for ruby-mode)
when String then "'#{quote_string(value)}'" # ' (for ruby-mode)
when NilClass then "NULL"
when TrueClass then (column && column.type == :boolean ? "'t'" : "1")
when FalseClass then (column && column.type == :boolean ? "'f'" : "0")
......
......@@ -61,7 +61,7 @@ module ClassMethods
# Model:
# class Person < ActiveRecord::Base
# validate_confirmation :user_name, :password
# validate_confirmation :email_address, "should match confirmation"
# validate_confirmation :email_address, :message => "should match confirmation"
# end
#
# View:
......@@ -72,13 +72,18 @@ module ClassMethods
# It exists only as an in-memory variable for validating the password. This check is performed both on create and update.
# See validate_confirmation_on_create and validate_confirmation_on_update if you want to restrict the validation to just one of the two
# situations.
#
# Configuration options:
# ::message: Specifies a custom error message (default is: "doesn't match confirmation")
def validate_confirmation(*attr_names)
error_message = attr_names.last.is_a?(String) ? attr_names.pop : "doesn't match confirmation"
configuration = { :message => "doesn't match confirmation" }
configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
validation_method = block_given? ? yield : "validate"
for attr_name in attr_names
attr_accessor "#{attr_name}_confirmation"
class_eval(%(#{validation_method} %{errors.add('#{attr_name}', "#{error_message}") unless #{attr_name} == #{attr_name}_confirmation}))
class_eval(%(#{validation_method} %{errors.add('#{attr_name}', "#{configuration[:message]}") unless #{attr_name} == #{attr_name}_confirmation}))
end
end
......@@ -97,7 +102,7 @@ def validate_confirmation_on_update(*attr_names)
# Model:
# class Person < ActiveRecord::Base
# validate_acceptance :terms_of_service
# validate_acceptance :eula, "must be abided"
# validate_acceptance :eula, :message => "must be abided"
# end
#
# View:
......@@ -107,14 +112,19 @@ def validate_confirmation_on_update(*attr_names)
# See validate_acceptance_on_create and validate_acceptance_on_update if you want to restrict the validation to just one of the two
# situations.
#
# Configuration options:
# ::message: Specifies a custom error message (default is: "must be accepted")
#
# 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)
error_message = attr_names.last.is_a?(String) ? attr_names.pop : "must be accepted"
configuration = { :message => "must be accepted" }
configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
validation_method = block_given? ? yield : "validate"
for attr_name in attr_names
attr_accessor(attr_name)
class_eval(%(#{validation_method} %{errors.add('#{attr_name}', '#{error_message}') unless #{attr_name} == "1"}))
class_eval(%(#{validation_method} %{errors.add('#{attr_name}', '#{configuration[:message]}') unless #{attr_name} == "1"}))
end
end
......@@ -129,11 +139,13 @@ def validate_acceptance_on_update(*attr_names)
end
def validate_presence(*attr_names)
error_message = attr_names.last.is_a?(String) ? attr_names.pop : "can't be empty"
configuration = { :message => "can't be empty" }
configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
validation_method = block_given? ? yield : "validate"
for attr_name in attr_names
class_eval(%(#{validation_method} %{errors.add_on_empty('#{attr_name}', "#{error_message}")}))
class_eval(%(#{validation_method} %{errors.add_on_empty('#{attr_name}', "#{configuration[:message]}")}))
end
end
......@@ -160,11 +172,15 @@ def validate_presence_on_update(*attr_names)
#
# When the record is created, a check is performed to make sure that no record exist in the database with the given value for the specified
# attribute (that maps to a column). When the record is updated, the same check is made but disregarding the record itself.
#
# Configuration options:
# ::message: Specifies a custom error message (default is: "has already been taken")
def validate_uniqueness(*attr_names)
error_message = attr_names.last.is_a?(String) ? attr_names.pop : "has already been taken"
configuration = { :message => "has already been taken" }
configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
for attr_name in attr_names
class_eval(%(validate %{errors.add("#{attr_name}", "#{error_message}") if self.class.find_first(new_record? ? ["#{attr_name} = ?", #{attr_name}] : ["#{attr_name} = ? AND id <> ?", #{attr_name}, id])}))
class_eval(%(validate %{errors.add("#{attr_name}", "#{configuration[:message]}") if self.class.find_first(new_record? ? ["#{attr_name} = ?", #{attr_name}] : ["#{attr_name} = ? AND id <> ?", #{attr_name}, id])}))
end
end
end
......
......@@ -149,7 +149,7 @@ def test_terms_of_service_agreement
def test_eula
Topic.validate_acceptance_on_create(:eula, "must be abided")
Topic.validate_acceptance_on_create(:eula, :message => "must be abided")
t = Topic.create("title" => "We should be confirmed")
assert !t.save
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册