Guest User

Untitled

a guest
Apr 11th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. require 'rubygems'
  2. require 'active_record'
  3. require 'spec'
  4.  
  5. ActiveRecord::Base.establish_connection( {
  6. :adapter => 'mysql',
  7. :host => 'localhost',
  8. :database => 'testbed',
  9. :username => 'root',
  10. :password => ''
  11. })
  12.  
  13. describe 'association modeling' do
  14. before :all do
  15. ActiveRecord::Migration.instance_eval do
  16. create_table :users, :force => true do |t|
  17. t.string :name
  18. end
  19.  
  20. create_table :affiliations, :force => true do |t|
  21. t.integer :user_id
  22. t.references :affiliatable, :polymorphic => true
  23. t.string :relationship_type
  24. end
  25.  
  26. create_table :networks, :force => true do |t|
  27. t.string :name
  28. end
  29.  
  30. create_table :communities, :force => true do |t|
  31. t.string :name
  32. end
  33. end
  34. end
  35.  
  36. after :all do
  37. ActiveRecord::Migration.instance_eval do
  38. drop_table :users
  39. drop_table :affiliations
  40. drop_table :networks
  41. drop_table :communities
  42. end
  43. end
  44.  
  45. class User < ActiveRecord::Base
  46. has_many :affiliations
  47. has_many :communities, :through => :affiliations, :source => :community,
  48. :conditions => "affiliations.affiliatable_type = 'Community'"
  49.  
  50. has_many :networks, :through => :affiliations,
  51. :conditions => "affiliations.affiliatable_type = 'Network'"
  52.  
  53. named_scope :in_network, lambda{ |network| {:joins => :networks, :conditions => ['affiliations.affiliatable_id = ?', network]} }
  54.  
  55. def fellow_members
  56. self.class.scoped({:conditions => [
  57. "users.id IN
  58. (SELECT u.id from users u
  59. INNER JOIN affiliations ON (u.id = affiliations.user_id)
  60. INNER JOIN affiliations my_affiliations ON (affiliations.affiliatable_id = my_affiliations.affiliatable_id AND affiliations.affiliatable_type = 'Community')
  61. WHERE (my_affiliations.user_id = ?))", id]})
  62. end
  63. end
  64.  
  65. class Affiliation < ActiveRecord::Base
  66. belongs_to :user
  67. belongs_to :affiliatable, :polymorphic => true
  68.  
  69. belongs_to :community, :class_name => "Community", :foreign_key => "affiliatable_id"
  70. belongs_to :network, :class_name => "Network", :foreign_key => "affiliatable_id"
  71. end
  72.  
  73. class Network < ActiveRecord::Base
  74. has_many :affiliations, :as => :affiliatable
  75. has_many :users, :through => :affiliations
  76. end
  77.  
  78. class Community < ActiveRecord::Base
  79. has_many :affiliations, :as => :affiliatable
  80. has_many :users, :through => :affiliations
  81. end
  82.  
  83. it 'has_many all around' do
  84. me = User.create
  85. network = Network.create
  86. community = Community.create
  87.  
  88. me.affiliations.create(:affiliatable => network)
  89. me.affiliations.create(:affiliatable => community)
  90. me.should have(2).affiliations
  91.  
  92. network.should have(1).user
  93. community.should have(1).user
  94. me.should have(1).community
  95. me.should have(1).network
  96. end
  97.  
  98. it 'can do something special' do
  99. community1 = Community.create
  100. community2 = Community.create
  101. me = User.create(:name => 'ME')
  102. sally = User.create(:name => 'sally')
  103. harry = User.create(:name => 'harry')
  104.  
  105. me.affiliations.create(:affiliatable => community1)
  106. me.affiliations.create(:affiliatable => community2)
  107.  
  108. harry.affiliations.create(:affiliatable => community1)
  109. me.fellow_members.should include(harry)
  110. me.fellow_members.should_not include(sally)
  111.  
  112. sally.affiliations.create(:affiliatable => community2)
  113. me.fellow_members.should include(harry, sally)
  114. end
  115.  
  116. it 'can do something even better' do
  117. community = Community.create
  118. network = Network.create
  119. me = User.create(:name => 'ME')
  120. sally = User.create(:name => 'sally')
  121. harry = User.create(:name => 'harry')
  122. [me, sally, harry].each{|user| user.affiliations.create(:affiliatable => community) }
  123. [me, sally].each{|user| user.affiliations.create(:affiliatable => network) }
  124.  
  125.  
  126. me.networks.should_not be_empty
  127. me.fellow_members.in_network(network).should include(sally)
  128. me.fellow_members.in_network(network).should_not include(harry)
  129. end
  130.  
  131. end
Add Comment
Please, Sign In to add comment