Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #5896: validates_uniqueness.diff

File validates_uniqueness.diff, 4.1 kB (added by wildchild, 1 year ago)

Tested with trunk, updated db_definitions/schema.rb according to bitsweat.

  • activerecord/test/fixtures/developer.rb

    old new  
    1010  end 
    1111end 
    1212 
     13require 'ipaddr' 
     14 
     15# Convert the integer form of an IP to string 
     16def inet_ntoa(n) 
     17  n.nil? ? nil : ([n].pack("N").unpack("C*").join ".") 
     18end 
     19 
    1320class Developer < ActiveRecord::Base 
    1421  has_and_belongs_to_many :projects do 
    1522    def find_most_recent 
     
    3340 
    3441  validates_inclusion_of :salary, :in => 50000..200000 
    3542  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 
    3651end 
    3752 
    3853DeveloperSalary = Struct.new(:amount) 
  • activerecord/test/fixtures/developers.yml

    old new  
    22  id: 1 
    33  name: David 
    44  salary: 80000 
     5  machine_ip: 1179946852 
    56 
    67jamis: 
    78  id: 2 
    89  name: Jamis 
    910  salary: 150000 
     11  machine_ip: 1179946854 
    1012 
    1113<% for digit in 3..10 %> 
    1214dev_<%= digit %>: 
  • activerecord/test/fixtures/db_definitions/schema.rb

    old new  
    7575      t.column :real_number, :real 
    7676    end 
    7777  end 
     78 
     79  add_column(:developers, :machine_ip, :integer) 
    7880end 
  • activerecord/test/validations_test.rb

    old new  
    369369    assert t2.save, "should save with nil" 
    370370  end 
    371371 
     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 
    372385  def test_validate_format 
    373386    Topic.validates_format_of(:title, :content, :with => /^Validation\smacros \w+!$/, :message => "is bad data") 
    374387 
     
    385398 
    386399    assert_raise(ArgumentError) { Topic.validates_format_of(:title, :content) } 
    387400  end 
    388    
     401 
    389402  # testing ticket #3142 
    390403  def test_validate_format_numeric 
    391404    Topic.validates_format_of(:title, :content, :with => /^[1-9][0-9]*$/, :message => "is bad data") 
  • activerecord/lib/active_record/validations.rb

    old new  
    621621        configuration = { :message => ActiveRecord::Errors.default_error_messages[:taken], :case_sensitive => true } 
    622622        configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) 
    623623 
    624         validates_each(attr_names,configuration) do |record, attr_name, value| 
     624        validates_each(attr_names, configuration) do |record, attr_name, value| 
    625625          if value.nil? || (configuration[:case_sensitive] || !columns_hash[attr_name.to_s].text?) 
    626626            condition_sql = "#{record.class.table_name}.#{attr_name} #{attribute_condition(value)}" 
    627             condition_params = [value
     627            condition_params = [record.send(:read_attribute, attr_name)
    628628          else 
    629629            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] 
    631631          end 
    632632          if scope = configuration[:scope] 
    633633            Array(scope).map do |scope_item|