aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/validations.rb
diff options
context:
space:
mode:
authorRick Olson <technoweenie@gmail.com>2006-04-12 20:42:13 +0000
committerRick Olson <technoweenie@gmail.com>2006-04-12 20:42:13 +0000
commitbdb2a2f1cb645a5dfde2de3b03f0f22b54b8b5d0 (patch)
tree3cab5b2cd876df9de682416fe132c882aa5deeac /activerecord/lib/active_record/validations.rb
parent7e76740d2a5ee14b308c7e40f9c95354d19f6189 (diff)
downloadrails-bdb2a2f1cb645a5dfde2de3b03f0f22b54b8b5d0.tar.gz
rails-bdb2a2f1cb645a5dfde2de3b03f0f22b54b8b5d0.tar.bz2
rails-bdb2a2f1cb645a5dfde2de3b03f0f22b54b8b5d0.zip
Add :case_sensitive option to validates_uniqueness_of (closes #3090) [Rick]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4207 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record/validations.rb')
-rwxr-xr-xactiverecord/lib/active_record/validations.rb14
1 files changed, 11 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb
index fec2f59a1b..648b7f8f5a 100755
--- a/activerecord/lib/active_record/validations.rb
+++ b/activerecord/lib/active_record/validations.rb
@@ -503,17 +503,23 @@ module ActiveRecord
# Configuration options:
# * <tt>message</tt> - Specifies a custom error message (default is: "has already been taken")
# * <tt>scope</tt> - One or more columns by which to limit the scope of the uniquness constraint.
+ # * <tt>case_sensitive</tt> - Looks for an exact match. Ignored by non-text columns (true by default).
# * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should
# occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The
# method, proc or string should return or evaluate to a true or false value.
def validates_uniqueness_of(*attr_names)
- configuration = { :message => ActiveRecord::Errors.default_error_messages[:taken] }
+ configuration = { :message => ActiveRecord::Errors.default_error_messages[:taken], :case_sensitive => true }
configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
validates_each(attr_names,configuration) do |record, attr_name, value|
- condition_sql = "#{record.class.table_name}.#{attr_name} #{attribute_condition(value)}"
- condition_params = [value]
+ if value.nil? || (configuration[:case_sensitive] || !columns_hash[attr_name.to_s].text?)
+ condition_sql = "#{record.class.table_name}.#{attr_name} #{attribute_condition(value)}"
+ condition_params = [value]
+ else
+ condition_sql = "UPPER(#{record.class.table_name}.#{attr_name}) #{attribute_condition(value)}"
+ condition_params = [value.upcase]
+ end
if scope = configuration[:scope]
Array(scope).map do |scope_item|
scope_value = record.send(scope_item)
@@ -531,6 +537,8 @@ module ActiveRecord
end
end
+
+
# Validates whether the value of the specified attribute is of the correct form by matching it against the regular expression
# provided.
#