Changeset 109
- Timestamp:
- 12/10/04 16:46:41 (5 years ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
r108 r109 325 325 def quote(value, column = nil) 326 326 case value 327 when String then %('#{quote_string(value)}')# ' (for ruby-mode)327 when String then "'#{quote_string(value)}'" # ' (for ruby-mode) 328 328 when NilClass then "NULL" 329 329 when TrueClass then (column && column.type == :boolean ? "'t'" : "1") trunk/activerecord/lib/active_record/validations.rb
r108 r109 62 62 # class Person < ActiveRecord::Base 63 63 # validate_confirmation :user_name, :password 64 # validate_confirmation :email_address, "should match confirmation"64 # validate_confirmation :email_address, :message => "should match confirmation" 65 65 # end 66 66 # … … 73 73 # See validate_confirmation_on_create and validate_confirmation_on_update if you want to restrict the validation to just one of the two 74 74 # situations. 75 # 76 # Configuration options: 77 # ::message: Specifies a custom error message (default is: "doesn't match confirmation") 75 78 def validate_confirmation(*attr_names) 76 error_message = attr_names.last.is_a?(String) ? attr_names.pop : "doesn't match confirmation" 79 configuration = { :message => "doesn't match confirmation" } 80 configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) 81 77 82 validation_method = block_given? ? yield : "validate" 78 83 79 84 for attr_name in attr_names 80 85 attr_accessor "#{attr_name}_confirmation" 81 class_eval(%(#{validation_method} %{errors.add('#{attr_name}', "#{ error_message}") unless #{attr_name} == #{attr_name}_confirmation}))86 class_eval(%(#{validation_method} %{errors.add('#{attr_name}', "#{configuration[:message]}") unless #{attr_name} == #{attr_name}_confirmation})) 82 87 end 83 88 end … … 98 103 # class Person < ActiveRecord::Base 99 104 # validate_acceptance :terms_of_service 100 # validate_acceptance :eula, "must be abided"105 # validate_acceptance :eula, :message => "must be abided" 101 106 # end 102 107 # … … 108 113 # situations. 109 114 # 115 # Configuration options: 116 # ::message: Specifies a custom error message (default is: "must be accepted") 117 # 110 118 # 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. 111 119 def validate_acceptance(*attr_names) 112 error_message = attr_names.last.is_a?(String) ? attr_names.pop : "must be accepted" 120 configuration = { :message => "must be accepted" } 121 configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) 122 113 123 validation_method = block_given? ? yield : "validate" 114 124 115 125 for attr_name in attr_names 116 126 attr_accessor(attr_name) 117 class_eval(%(#{validation_method} %{errors.add('#{attr_name}', '#{ error_message}') unless #{attr_name} == "1"}))127 class_eval(%(#{validation_method} %{errors.add('#{attr_name}', '#{configuration[:message]}') unless #{attr_name} == "1"})) 118 128 end 119 129 end … … 130 140 131 141 def validate_presence(*attr_names) 132 error_message = attr_names.last.is_a?(String) ? attr_names.pop : "can't be empty" 142 configuration = { :message => "can't be empty" } 143 configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) 144 133 145 validation_method = block_given? ? yield : "validate" 134 146 135 147 for attr_name in attr_names 136 class_eval(%(#{validation_method} %{errors.add_on_empty('#{attr_name}', "#{ error_message}")}))148 class_eval(%(#{validation_method} %{errors.add_on_empty('#{attr_name}', "#{configuration[:message]}")})) 137 149 end 138 150 end … … 161 173 # 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 162 174 # attribute (that maps to a column). When the record is updated, the same check is made but disregarding the record itself. 175 # 176 # Configuration options: 177 # ::message: Specifies a custom error message (default is: "has already been taken") 163 178 def validate_uniqueness(*attr_names) 164 error_message = attr_names.last.is_a?(String) ? attr_names.pop : "has already been taken" 179 configuration = { :message => "has already been taken" } 180 configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) 165 181 166 182 for attr_name in attr_names 167 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])}))183 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])})) 168 184 end 169 185 end trunk/activerecord/test/validations_test.rb
r108 r109 150 150 151 151 def test_eula 152 Topic.validate_acceptance_on_create(:eula, "must be abided")152 Topic.validate_acceptance_on_create(:eula, :message => "must be abided") 153 153 154 154 t = Topic.create("title" => "We should be confirmed")