GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Description: Exclusive Conditions for ActiveRecord’s has_many
Homepage: http://ambethia.com/2007/11/28/exclusive-conditions-for-activerecords-has_many/
Clone URL: git://github.com/ambethia/has_many_exclusive_conditions.git
has_many_exclusive_conditions / test / has_many_exclusive_conditions_test.rb
100644 53 lines (40 sloc) 1.494 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
$:.unshift(File.dirname(__FILE__) + '/../lib')
$:.unshift(File.dirname(__FILE__) + '/..')
 
require 'test/unit'
require 'rubygems'
require 'active_record'
require 'init'
 
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => "has_many_exclusive_conditions.sqlite3.db")
 
ActiveRecord::Schema.define(:version => 1) do
  create_table :users, :force => true do |t|
    t.string :name
    t.boolean :is_admin, :default => false
  end
 
  create_table :things, :force => true do |t|
    t.string :name
    t.integer :user_id
  end
end
 
class User < ActiveRecord::Base
  has_many :things, :exclusive_conditions => %q(`things`.user_id = #{id} OR #{is_admin ? 1 : 0})
end
 
class Thing < ActiveRecord::Base
  belongs_to :user
end
 
class HasManyExclusiveConditionsTest < Test::Unit::TestCase
 
  def setup
    @alice ||= User.create :name => "Alice", :is_admin => true
    @bob ||= User.create :name => "Bob"
    
    @alices_thing ||= Thing.create :name => "Alice's Thing", :user => @alice
    @bobs_thing ||= Thing.create :name => "Bob's Thing", :user => @bob
  end
 
  def test_admin_should_have_all_things
    assert_block { @alice.things.include?(@bobs_thing) }
    assert_block { @alice.things.include?(@alices_thing) }
  end
 
  def test_user_should_have_its_things
    assert_block { @bob.things.include?(@bobs_thing) }
    assert_block { !@bob.things.include?(@alices_thing) }
  end
  
end