From bdb2a2f1cb645a5dfde2de3b03f0f22b54b8b5d0 Mon Sep 17 00:00:00 2001 From: Rick Olson Date: Wed, 12 Apr 2006 20:42:13 +0000 Subject: 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 --- activerecord/lib/active_record/validations.rb | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'activerecord/lib/active_record') 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: # * message - Specifies a custom error message (default is: "has already been taken") # * scope - One or more columns by which to limit the scope of the uniquness constraint. + # * case_sensitive - Looks for an exact match. Ignored by non-text columns (true by default). # * if - 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. # -- cgit v1.2.3