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

Ticket #9235: vuo_scoping_with_magic_scope.diff

File vuo_scoping_with_magic_scope.diff, 3.1 kB (added by nik.wakelin, 1 year ago)
  • activerecord/test/fixtures/customer.rb

    old new  
    22  composed_of :address, :mapping => [ %w(address_street street), %w(address_city city), %w(address_country country) ], :allow_nil => true 
    33  composed_of :balance, :class_name => "Money", :mapping => %w(balance amount) 
    44  composed_of :gps_location, :allow_nil => true 
     5   
     6  has_many    :orders, :foreign_key => "shipping_customer_id" 
    57end 
    68 
    79class Address 
  • activerecord/test/fixtures/order.rb

    old new  
    11class Order < ActiveRecord::Base 
    22  belongs_to :billing, :class_name => 'Customer', :foreign_key => 'billing_customer_id'  
    33  belongs_to :shipping, :class_name => 'Customer', :foreign_key => 'shipping_customer_id'  
     4   
     5  validates_uniqueness_of :name 
     6   
     7  def self.create_with_magic_scoping(args) 
     8    Order.create!(args) 
     9  end 
     10   
    411end 
  • activerecord/test/validations_test.rb

    old new  
    33require 'fixtures/reply' 
    44require 'fixtures/person' 
    55require 'fixtures/developer' 
     6require 'fixtures/customer' 
     7require 'fixtures/order' 
    68 
    79# The following methods in Topic are used in test_conditional_validation_* 
    810class Topic 
     
    368370    assert t2.valid?, "should validate with nil" 
    369371    assert t2.save, "should save with nil" 
    370372  end 
     373   
     374  def test_validates_uniqueness_breaks_out_of_implicit_scoping 
     375     
     376    c1 = Customer.find(1) 
     377    c2 = Customer.find(2) 
     378     
     379    o1 = c1.orders.create_with_magic_scoping(:name => "Should be unique") 
     380     
     381    #now create & validate with implicit scoping 
     382    assert_raises ActiveRecord::RecordInvalid do 
     383      c2.orders.create_with_magic_scoping(:name => "Should be unique") 
     384    end 
     385  end 
    371386 
    372387  def test_validate_format 
    373388    Topic.validates_format_of(:title, :content, :with => /^Validation\smacros \w+!$/, :message => "is bad data") 
  • activerecord/lib/active_record/validations.rb

    old new  
    640640            condition_sql << " AND #{record.class.table_name}.#{record.class.primary_key} <> ?" 
    641641            condition_params << record.send(:id) 
    642642          end 
    643           if record.class.find(:first, :conditions => [condition_sql, *condition_params]) 
    644             record.errors.add(attr_name, configuration[:message]) 
     643          record.class.send(:with_exclusive_scope) do  
     644            if record.class.find(:first, :conditions => [condition_sql, *condition_params]) 
     645              record.errors.add(attr_name, configuration[:message]) 
     646            end 
    645647          end 
    646648        end 
    647649      end