Ticket #5896: validates_uniqueness.diff
| File validates_uniqueness.diff, 4.1 kB (added by wildchild, 1 year ago) |
|---|
-
activerecord/test/fixtures/developer.rb
old new 10 10 end 11 11 end 12 12 13 require 'ipaddr' 14 15 # Convert the integer form of an IP to string 16 def inet_ntoa(n) 17 n.nil? ? nil : ([n].pack("N").unpack("C*").join ".") 18 end 19 13 20 class Developer < ActiveRecord::Base 14 21 has_and_belongs_to_many :projects do 15 22 def find_most_recent … … 33 40 34 41 validates_inclusion_of :salary, :in => 50000..200000 35 42 validates_length_of :name, :within => 3..20 43 44 def machine_ip 45 inet_ntoa(read_attribute(:machine_ip)) 46 end 47 48 def machine_ip=(n) 49 write_attribute(:machine_ip, IPAddr.new(n).to_i) 50 end 36 51 end 37 52 38 53 DeveloperSalary = Struct.new(:amount) -
activerecord/test/fixtures/developers.yml
old new 2 2 id: 1 3 3 name: David 4 4 salary: 80000 5 machine_ip: 1179946852 5 6 6 7 jamis: 7 8 id: 2 8 9 name: Jamis 9 10 salary: 150000 11 machine_ip: 1179946854 10 12 11 13 <% for digit in 3..10 %> 12 14 dev_<%= digit %>: -
activerecord/test/fixtures/db_definitions/schema.rb
old new 75 75 t.column :real_number, :real 76 76 end 77 77 end 78 79 add_column(:developers, :machine_ip, :integer) 78 80 end -
activerecord/test/validations_test.rb
old new 369 369 assert t2.save, "should save with nil" 370 370 end 371 371 372 def test_validate_uniqueness_with_custom_accessors 373 david = Developer.find(1) 374 jamis = Developer.find(2) 375 376 assert_equal "70.84.143.100", david.machine_ip 377 assert_equal "70.84.143.102", jamis.machine_ip 378 379 jamis.machine_ip = "70.84.143.100" 380 assert jamis.valid? 381 Developer.validates_uniqueness_of(:machine_ip) 382 assert !jamis.valid?, "The integer value should be used for validation" 383 end 384 372 385 def test_validate_format 373 386 Topic.validates_format_of(:title, :content, :with => /^Validation\smacros \w+!$/, :message => "is bad data") 374 387 … … 385 398 386 399 assert_raise(ArgumentError) { Topic.validates_format_of(:title, :content) } 387 400 end 388 401 389 402 # testing ticket #3142 390 403 def test_validate_format_numeric 391 404 Topic.validates_format_of(:title, :content, :with => /^[1-9][0-9]*$/, :message => "is bad data") -
activerecord/lib/active_record/validations.rb
old new 621 621 configuration = { :message => ActiveRecord::Errors.default_error_messages[:taken], :case_sensitive => true } 622 622 configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) 623 623 624 validates_each(attr_names, configuration) do |record, attr_name, value|624 validates_each(attr_names, configuration) do |record, attr_name, value| 625 625 if value.nil? || (configuration[:case_sensitive] || !columns_hash[attr_name.to_s].text?) 626 626 condition_sql = "#{record.class.table_name}.#{attr_name} #{attribute_condition(value)}" 627 condition_params = [ value]627 condition_params = [record.send(:read_attribute, attr_name)] 628 628 else 629 629 condition_sql = "LOWER(#{record.class.table_name}.#{attr_name}) #{attribute_condition(value)}" 630 condition_params = [ value.downcase]630 condition_params = [record.send(:read_attribute, attr_name).downcase] 631 631 end 632 632 if scope = configuration[:scope] 633 633 Array(scope).map do |scope_item|