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

Ticket #5896: uniq_with_adapters.diff

File uniq_with_adapters.diff, 7.5 kB (added by me@julik.nl, 2 years ago)

now with gazillion adapters (untested)

  • 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) 
  • 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 %>: 
  • test/fixtures/db_definitions/frontbase.sql

    old new  
    4949    salary integer DEFAULT 70000, 
    5050    created_at timestamp, 
    5151    updated_at timestamp, 
     52    machine_ip integer default 0, 
    5253    PRIMARY KEY (id) 
    5354); 
    5455SET UNIQUE FOR developers(id); 
  • test/fixtures/db_definitions/openbase.sql

    old new  
    4141    name char(100), 
    4242    salary integer DEFAULT 70000, 
    4343    created_at datetime, 
    44     updated_at datetime 
     44    updated_at datetime, 
     45    machine_ip integer default 0 
    4546)  
    4647go 
    4748CREATE PRIMARY KEY developers (id)  
  • test/fixtures/db_definitions/sqlite.sql

    old new  
    4040  'name' TEXT DEFAULT NULL, 
    4141  'salary' INTEGER DEFAULT 70000, 
    4242  'created_at' DATETIME DEFAULT NULL, 
    43   'updated_at' DATETIME DEFAULT NULL 
     43  'updated_at' DATETIME DEFAULT NULL, 
     44  `machine_ip` INTEGER DEFAULT NULL 
    4445); 
    4546 
    4647CREATE TABLE 'projects' ( 
  • test/fixtures/db_definitions/mysql.sql

    old new  
    4545  `salary` int(11) default 70000, 
    4646  `created_at` datetime default NULL, 
    4747  `updated_at` datetime default NULL, 
     48  `machine_ip` int(10) default NULL, 
    4849  PRIMARY KEY  (`id`) 
    4950) TYPE=InnoDB; 
    5051 
  • test/fixtures/db_definitions/oracle.sql

    old new  
    6969    salary integer default 70000, 
    7070    created_at timestamp default null, 
    7171    updated_at timestamp default null, 
     72    machine_ip integer default 0, 
    7273    primary key (id) 
    7374); 
    7475create sequence developers_seq minvalue 10000; 
  • test/fixtures/db_definitions/db2.sql

    old new  
    5858  developer_id INT NOT NULL, 
    5959  project_id INT NOT NULL, 
    6060  joined_on DATE DEFAULT NULL, 
    61   access_level SMALLINT DEFAULT 1 
     61  access_level SMALLINT DEFAULT 1, 
     62  machine_ip INT DEFAULT NULL 
    6263); 
    6364 
    6465CREATE TABLE orders ( 
  • test/fixtures/db_definitions/sqlserver.sql

    old new  
    3939  name varchar(100) default NULL, 
    4040  salary int default 70000, 
    4141  created_at datetime default NULL, 
    42   updated_at datetime default NULL 
     42  updated_at datetime default NULL, 
     43  machine_ip int default 0 
    4344); 
    4445 
    4546CREATE TABLE projects ( 
  • test/fixtures/db_definitions/firebird.sql

    old new  
    5454  salary INTEGER DEFAULT 70000, 
    5555  created_at TIMESTAMP, 
    5656  updated_at TIMESTAMP, 
     57  machine_ip INTEGER DEFAULT 0 
    5758  PRIMARY KEY (id) 
    5859); 
    5960CREATE GENERATOR developers_seq; 
  • test/fixtures/db_definitions/sybase.sql

    old new  
    4040  name varchar(100) NULL, 
    4141  salary int default 70000, 
    4242  created_at datetime NULL, 
    43   updated_at datetime NULL 
     43  updated_at datetime NULL, 
     44  machine_ip int default 0 
    4445) 
    4546 
    4647CREATE TABLE projects ( 
  • test/fixtures/db_definitions/postgresql.sql

    old new  
    3838    salary integer DEFAULT 70000, 
    3939    created_at timestamp, 
    4040    updated_at timestamp, 
     41    machine_ip integer default 0, 
    4142    PRIMARY KEY (id) 
    4243); 
    4344SELECT setval('developers_id_seq', 100); 
  • test/validations_test.rb

    old new  
    266266    t2.title = "Now Im really also unique" 
    267267    assert t2.save, "Should now save t2 as unique" 
    268268  end 
     269   
     270  def test_validate_uniqueness_with_custom_accessors 
     271    david = Developer.find(1) 
     272    jamis = Developer.find(2) 
     273     
     274    assert_equal "70.84.143.100", david.machine_ip 
     275    assert_equal "70.84.143.102", jamis.machine_ip 
     276     
     277    jamis.machine_ip = "70.84.143.100" 
     278    assert jamis.valid? 
     279    Developer.validates_uniqueness_of(:machine_ip) 
     280    assert !jamis.valid?, "The integer value should be used for validation" 
     281  end 
    269282 
    270283  def test_validate_uniqueness_with_scope 
    271284    Reply.validates_uniqueness_of(:content, :scope => "parent_id") 
  • lib/active_record/validations.rb

    old new  
    526526        validates_each(attr_names,configuration) do |record, attr_name, value| 
    527527          if value.nil? || (configuration[:case_sensitive] || !columns_hash[attr_name.to_s].text?) 
    528528            condition_sql = "#{record.class.table_name}.#{attr_name} #{attribute_condition(value)}" 
    529             condition_params = [value
     529            condition_params = [record.send(:read_attribute, attr_name)
    530530          else 
    531531            condition_sql = "UPPER(#{record.class.table_name}.#{attr_name}) #{attribute_condition(value)}" 
    532             condition_params = [value.upcase] 
     532            condition_params = [record.send(:read_attribute, attr_name).upcase] 
    533533          end 
    534534          if scope = configuration[:scope] 
    535535            Array(scope).map do |scope_item|