Commit 48b22a63cf41b98e0182a3696d1e7ab9b65944b1

Authored by Brian Christian
1 parent 81833d03
Exists in master

refactor (custom parent config not needed for rails5 or sti compatibility)

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
... ... @@ -6,25 +6,5 @@ module ActsAsFollower
6 6 autoload :FollowerLib, 'acts_as_follower/follower_lib'
7 7 autoload :FollowScopes, 'acts_as_follower/follow_scopes'
8 8  
9   - def self.setup
10   - @configuration ||= Configuration.new
11   - yield @configuration if block_given?
12   - end
13   -
14   - def self.method_missing(method_name, *args, &block)
15   - @configuration.respond_to?(method_name) ?
16   - @configuration.send(method_name, *args, &block) : super
17   - end
18   -
19   - class Configuration
20   - attr_accessor :custom_parent_classes
21   -
22   - def initialize
23   - @custom_parent_classes = []
24   - end
25   - end
26   -
27   - setup
28   -
29 9 require 'acts_as_follower/railtie' if defined?(Rails) && Rails::VERSION::MAJOR >= 3
30 10 end
... ...
lib/acts_as_follower/follower_lib.rb
... ... @@ -3,14 +3,9 @@ module ActsAsFollower
3 3  
4 4 private
5 5  
6   - DEFAULT_PARENTS = [ApplicationRecord, ActiveRecord::Base]
7   -
8 6 # Retrieves the parent class name if using STI.
9 7 def parent_class_name(obj)
10   - unless parent_classes.include?(obj.class.superclass)
11   - return obj.class.base_class.name
12   - end
13   - obj.class.name
  8 + obj.class.base_class.name
14 9 end
15 10  
16 11 def apply_options_to_scope(scope, options = {})
... ... @@ -32,10 +27,5 @@ module ActsAsFollower
32 27 scope
33 28 end
34 29  
35   - def parent_classes
36   - return DEFAULT_PARENTS unless ActsAsFollower.custom_parent_classes
37   -
38   - ActsAsFollower.custom_parent_classes + DEFAULT_PARENTS
39   - end
40 30 end
41 31 end
... ...
test/follow_test.rb
... ... @@ -7,55 +7,4 @@ class FollowTest < ActiveSupport::TestCase
7 7 assert true
8 8 end
9 9  
10   - context "configuration with setters" do
11   - should "contain custom parents" do
12   - ActsAsFollower.custom_parent_classes = [CustomRecord]
13   -
14   - assert_equal [CustomRecord], ActsAsFollower.custom_parent_classes
15   - end
16   - end
17   -
18   - context "#setup" do
19   - should "contain custom parents via setup" do
20   - ActsAsFollower.setup do |c|
21   - c.custom_parent_classes = [CustomRecord]
22   - end
23   -
24   - assert_equal [CustomRecord], ActsAsFollower.custom_parent_classes
25   - end
26   - end
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 10 end
... ...