Commit 72dfdffbddb13a15fb2462a72acb6093c9420400

Authored by Justin Coyne
Committed by GitHub
2 parents c395dd2f d84ac431
Exists in master

Merge pull request #56 from brchristian/patch-1

Use base_class instead of superclass in parent_class_name
README.rdoc
... ... @@ -76,18 +76,6 @@ Make your model(s) that can follow other models acts_as_follower
76 76 ...
77 77 end
78 78  
79   -Extended setup:
80   - # config/initializers/acts_as_follower.rb
81   -
82   - # By default list of parent classes includes only `[ApplicationRecord, ActiveRecord::Base]`.
83   - ActsAsFollower.custom_parent_classes = [CustomRecord]
84   -
85   - # OR
86   -
87   - ActsAsFollower.setup do |c|
88   - c.custom_parent_classes = [...]
89   - end
90   -
91 79 ---
92 80  
93 81 === acts_as_follower methods
... ...
lib/acts_as_follower.rb
... ... @@ -12,6 +12,9 @@ module ActsAsFollower
12 12 end
13 13  
14 14 def self.method_missing(method_name, *args, &block)
  15 + if method_name == :custom_parent_classes=
  16 + ActiveSupport::Deprecation.warn("Setting custom parent classes is deprecated and will be removed in future versions.")
  17 + end
15 18 @configuration.respond_to?(method_name) ?
16 19 @configuration.send(method_name, *args, &block) : super
17 20 end
... ...
lib/acts_as_follower/follower_lib.rb
... ... @@ -8,7 +8,7 @@ module ActsAsFollower
8 8 # Retrieves the parent class name if using STI.
9 9 def parent_class_name(obj)
10 10 unless parent_classes.include?(obj.class.superclass)
11   - return obj.class.superclass.name
  11 + return obj.class.base_class.name
12 12 end
13 13 obj.class.name
14 14 end
... ... @@ -35,6 +35,7 @@ module ActsAsFollower
35 35 def parent_classes
36 36 return DEFAULT_PARENTS unless ActsAsFollower.custom_parent_classes
37 37  
  38 + ActiveSupport::Deprecation.warn("Setting custom parent classes is deprecated and will be removed in future versions.")
38 39 ActsAsFollower.custom_parent_classes + DEFAULT_PARENTS
39 40 end
40 41 end
... ...
test/acts_as_followable_test.rb
... ... @@ -20,6 +20,8 @@ class ActsAsFollowableTest < ActiveSupport::TestCase
20 20 @jon = FactoryGirl.create(:jon)
21 21 @oasis = FactoryGirl.create(:oasis)
22 22 @metallica = FactoryGirl.create(:metallica)
  23 + @green_day = FactoryGirl.create(:green_day)
  24 + @blink_182 = FactoryGirl.create(:blink_182)
23 25 @sam.follow(@jon)
24 26 end
25 27  
... ... @@ -225,6 +227,20 @@ class ActsAsFollowableTest < ActiveSupport::TestCase
225 227 end
226 228 end
227 229  
  230 + context "followers_with_sti" do
  231 + setup do
  232 + @sam.follow(@green_day)
  233 + @sam.follow(@blink_182)
  234 + end
  235 +
  236 + should "return the followers for given type" do
  237 + assert_equal @sam.follows_by_type('Band').first.followable, @green_day.becomes(Band)
  238 + assert_equal @sam.follows_by_type('Band').second.followable, @blink_182.becomes(Band)
  239 + assert @green_day.followers_by_type('User').include?(@sam)
  240 + assert @blink_182.followers_by_type('User').include?(@sam)
  241 + end
  242 + end
  243 +
228 244 context "method_missing" do
229 245 setup do
230 246 @sam.follow(@oasis)
... ...
test/dummy30/app/models/band/punk.rb 0 → 100644
... ... @@ -0,0 +1,4 @@
  1 +class Band::Punk < Band
  2 + validates_presence_of :name
  3 + acts_as_followable
  4 +end
... ...
test/dummy30/app/models/band/punk/pop_punk.rb 0 → 100644
... ... @@ -0,0 +1,4 @@
  1 +class Band::Punk::PopPunk < Band::Punk
  2 + validates_presence_of :name
  3 + acts_as_followable
  4 +end
... ...
test/factories/bands.rb
... ... @@ -6,4 +6,12 @@ FactoryGirl.define do
6 6 factory :metallica, :class => Band do |b|
7 7 b.name 'Metallica'
8 8 end
  9 +
  10 + factory :green_day, :class => Band::Punk do |b|
  11 + b.name 'Green Day'
  12 + end
  13 +
  14 + factory :blink_182, :class => Band::Punk::PopPunk do |b|
  15 + b.name 'Blink 182'
  16 + end
9 17 end
... ...
test/follow_test.rb
... ... @@ -25,37 +25,4 @@ class FollowTest &lt; ActiveSupport::TestCase
25 25 end
26 26 end
27 27  
28   - context "with custom parents" do
29   - setup do
30   - @daddy = FactoryGirl.create(:daddy)
31   - @mommy = FactoryGirl.create(:mommy)
32   - @oasis = FactoryGirl.create(:oasis)
33   - @metallica = FactoryGirl.create(:metallica)
34   - end
35   -
36   - should "be followed" do
37   - ActsAsFollower.custom_parent_classes = [CustomRecord]
38   -
39   - @daddy.follow(@mommy)
40   - @daddy.follow(@metallica)
41   - @mommy.follow(@oasis)
42   - assert_equal true, @daddy.following?(@mommy)
43   - assert_equal false, @mommy.following?(@daddy)
44   - assert_equal true, @mommy.followed_by?(@daddy)
45   - assert_equal false, @daddy.followed_by?(@mommy)
46   - assert_equal true, @metallica.followed_by?(@daddy)
47   - assert_equal true, @oasis.followed_by?(@mommy)
48   - assert_equal true, @daddy.following?(@metallica)
49   - assert_equal true, @mommy.following?(@oasis)
50   - end
51   -
52   - should "be not followed" do
53   - ActsAsFollower.custom_parent_classes = []
54   -
55   - @daddy.follow(@mommy)
56   - @mommy.follow(@oasis)
57   - assert_equal false, @daddy.following?(@mommy)
58   - assert_equal false, @mommy.following?(@oasis)
59   - end
60   - end
61 28 end
... ...