Browse files

Added acts_as_union method and simple test to acts_as_network plugin

  • Loading branch information...
1 parent 4d2ce44 commit f5230cdfca5191f82e4f50269b6ebb7ba33a193c @billymeltdown billymeltdown committed May 30, 2008
Showing with 53 additions and 4 deletions.
  1. +2 −1 init.rb
  2. +19 −1 lib/zetetic/acts/network.rb
  3. +5 −1 test/fixtures/channels.yml
  4. +14 −1 test/fixtures/shows.yml
  5. +12 −0 test/network_test.rb
  6. +1 −0 test/schema.rb
View
3 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
View
20 lib/zetetic/acts/network.rb
@@ -267,5 +267,23 @@ def #{relationship}
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
View
6 test/fixtures/channels.yml
@@ -8,4 +8,8 @@ usa:
amc:
id: 2
- name: AMC
+ name: AMC
+
+abc:
+ id: 3
+ name: ABC
View
15 test/fixtures/shows.yml
@@ -2,33 +2,46 @@ myth_busters:
id: 0
name: Myth Busters
channel_id: 0
+ package: premium
deadliest_catch:
id: 1
name: Deadliest Catch
channel_id: 0
+ package: mega
dirty_jobs:
id: 2
name: Dirty Jobs
channel_id: 0
+ package: mega
monk:
id: 3
name: Monk
channel_id: 1
+ package: basic
psych:
id: 4
name: Psych
channel_id: 1
+ package: basic
burn_notice:
id: 5
name: Burn Notice
channel_id: 1
+ package: premium
mad_men:
id: 6
name: Mad Men
- channel_id: 2
+ channel_id: 2
+ package: premium
+
+action_news:
+ id: 7
+ name: Action News
+ channel_id: 3
+ package: basic
View
12 test/network_test.rb
@@ -2,6 +2,9 @@
class Channel < ActiveRecord::Base
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
class Show < ActiveRecord::Base
@@ -40,6 +43,15 @@ def ids
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
fixtures :shows, :channels
View
1 test/schema.rb
@@ -7,6 +7,7 @@
create_table :shows, :force => true do |t|
t.column :name, :string
t.column :channel_id, :integer
+ t.column :package, :string
end
# people

0 comments on commit f5230cd

Please sign in to comment.