Commit d84ac4313a818addf0326ba5b850986f08955500
1 parent
48b22a63
Exists in
master
restore and deprecate custom_parent_classes
Showing
3 changed files
with
53 additions
and
1 deletions
Show diff stats
lib/acts_as_follower.rb
... | ... | @@ -6,5 +6,28 @@ 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 | + 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 | |
18 | + @configuration.respond_to?(method_name) ? | |
19 | + @configuration.send(method_name, *args, &block) : super | |
20 | + end | |
21 | + | |
22 | + class Configuration | |
23 | + attr_accessor :custom_parent_classes | |
24 | + | |
25 | + def initialize | |
26 | + @custom_parent_classes = [] | |
27 | + end | |
28 | + end | |
29 | + | |
30 | + setup | |
31 | + | |
9 | 32 | require 'acts_as_follower/railtie' if defined?(Rails) && Rails::VERSION::MAJOR >= 3 |
10 | 33 | end | ... | ... |
lib/acts_as_follower/follower_lib.rb
... | ... | @@ -3,9 +3,14 @@ module ActsAsFollower |
3 | 3 | |
4 | 4 | private |
5 | 5 | |
6 | + DEFAULT_PARENTS = [ApplicationRecord, ActiveRecord::Base] | |
7 | + | |
6 | 8 | # Retrieves the parent class name if using STI. |
7 | 9 | def parent_class_name(obj) |
8 | - obj.class.base_class.name | |
10 | + unless parent_classes.include?(obj.class.superclass) | |
11 | + return obj.class.base_class.name | |
12 | + end | |
13 | + obj.class.name | |
9 | 14 | end |
10 | 15 | |
11 | 16 | def apply_options_to_scope(scope, options = {}) |
... | ... | @@ -27,5 +32,11 @@ module ActsAsFollower |
27 | 32 | scope |
28 | 33 | end |
29 | 34 | |
35 | + def parent_classes | |
36 | + return DEFAULT_PARENTS unless ActsAsFollower.custom_parent_classes | |
37 | + | |
38 | + ActiveSupport::Deprecation.warn("Setting custom parent classes is deprecated and will be removed in future versions.") | |
39 | + ActsAsFollower.custom_parent_classes + DEFAULT_PARENTS | |
40 | + end | |
30 | 41 | end |
31 | 42 | end | ... | ... |
test/follow_test.rb
... | ... | @@ -7,4 +7,22 @@ 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 | + | |
10 | 28 | end | ... | ... |