Skip to content

Commit

Permalink
Added acts_as_union method and simple test to acts_as_network plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
billymeltdown committed May 30, 2008
1 parent 4d2ce44 commit f5230cd
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 4 deletions.
3 changes: 2 additions & 1 deletion init.rb
@@ -1 +1,2 @@
ActiveRecord::Base.send :include, Zetetic::Acts::Network ActiveRecord::Base.send :include, Zetetic::Acts::Network
ActiveRecord::Base.send :include, Zetetic::Acts::Union
20 changes: 19 additions & 1 deletion lib/zetetic/acts/network.rb
Expand Up @@ -267,5 +267,23 @@ def #{relationship}
end end
end end
end end
end
module Union
def self.included(base)
base.extend ClassMethods
end

module ClassMethods
def acts_as_union(relationship, methods)
# define the accessor method for the reciprocal network relationship view itself.
# i.e. if People acts_as_network :contacts, this method is defind as def contacts
class_eval <<-EOV
def #{relationship}
UnionCollection.new(#{methods.collect{|m| "self.#{m.to_s}"}.join(',')})
end
EOV
end
end
end # module Union
end # module Acts
end end
6 changes: 5 additions & 1 deletion test/fixtures/channels.yml
Expand Up @@ -8,4 +8,8 @@ usa:


amc: amc:
id: 2 id: 2
name: AMC name: AMC

abc:
id: 3
name: ABC
15 changes: 14 additions & 1 deletion test/fixtures/shows.yml
Expand Up @@ -2,33 +2,46 @@ myth_busters:
id: 0 id: 0
name: Myth Busters name: Myth Busters
channel_id: 0 channel_id: 0
package: premium


deadliest_catch: deadliest_catch:
id: 1 id: 1
name: Deadliest Catch name: Deadliest Catch
channel_id: 0 channel_id: 0
package: mega


dirty_jobs: dirty_jobs:
id: 2 id: 2
name: Dirty Jobs name: Dirty Jobs
channel_id: 0 channel_id: 0
package: mega


monk: monk:
id: 3 id: 3
name: Monk name: Monk
channel_id: 1 channel_id: 1
package: basic


psych: psych:
id: 4 id: 4
name: Psych name: Psych
channel_id: 1 channel_id: 1
package: basic


burn_notice: burn_notice:
id: 5 id: 5
name: Burn Notice name: Burn Notice
channel_id: 1 channel_id: 1
package: premium


mad_men: mad_men:
id: 6 id: 6
name: Mad Men name: Mad Men
channel_id: 2 channel_id: 2
package: premium

action_news:
id: 7
name: Action News
channel_id: 3
package: basic
12 changes: 12 additions & 0 deletions test/network_test.rb
Expand Up @@ -2,6 +2,9 @@


class Channel < ActiveRecord::Base class Channel < ActiveRecord::Base
has_many :shows has_many :shows
has_many :premium_shows, :class_name => 'Show', :conditions => ['package = ?', 'premium']
has_many :mega_shows, :class_name => 'Show', :conditions => ['package = ?', 'mega']
acts_as_union :pay_shows, [ :premium_shows, :mega_shows ]
end end


class Show < ActiveRecord::Base class Show < ActiveRecord::Base
Expand Down Expand Up @@ -40,6 +43,15 @@ def ids
end end
end end


class ActsAsUntionTest < Test::Unit::TestCase
fixtures :shows, :channels

def test_union_method
assert_equal 0, channels(:abc).pay_shows.length
assert_equal 3, channels(:discovery).pay_shows.length
end
end

class UnionCollectionTest < Test::Unit::TestCase class UnionCollectionTest < Test::Unit::TestCase
fixtures :shows, :channels fixtures :shows, :channels


Expand Down
1 change: 1 addition & 0 deletions test/schema.rb
Expand Up @@ -7,6 +7,7 @@
create_table :shows, :force => true do |t| create_table :shows, :force => true do |t|
t.column :name, :string t.column :name, :string
t.column :channel_id, :integer t.column :channel_id, :integer
t.column :package, :string
end end


# people # people
Expand Down

0 comments on commit f5230cd

Please sign in to comment.