diff --git a/lib/acts_as_follower.rb b/lib/acts_as_follower.rb index c9246b2..040db64 100644 --- a/lib/acts_as_follower.rb +++ b/lib/acts_as_follower.rb @@ -6,5 +6,28 @@ module ActsAsFollower autoload :FollowerLib, 'acts_as_follower/follower_lib' autoload :FollowScopes, 'acts_as_follower/follow_scopes' + def self.setup + @configuration ||= Configuration.new + yield @configuration if block_given? + end + + def self.method_missing(method_name, *args, &block) + if method_name == :custom_parent_classes= + ActiveSupport::Deprecation.warn("Setting custom parent classes is deprecated and will be removed in future versions.") + end + @configuration.respond_to?(method_name) ? + @configuration.send(method_name, *args, &block) : super + end + + class Configuration + attr_accessor :custom_parent_classes + + def initialize + @custom_parent_classes = [] + end + end + + setup + require 'acts_as_follower/railtie' if defined?(Rails) && Rails::VERSION::MAJOR >= 3 end diff --git a/lib/acts_as_follower/follower_lib.rb b/lib/acts_as_follower/follower_lib.rb index 57c1a90..fa40023 100644 --- a/lib/acts_as_follower/follower_lib.rb +++ b/lib/acts_as_follower/follower_lib.rb @@ -3,9 +3,14 @@ module FollowerLib private + DEFAULT_PARENTS = [ApplicationRecord, ActiveRecord::Base] + # Retrieves the parent class name if using STI. def parent_class_name(obj) - obj.class.base_class.name + unless parent_classes.include?(obj.class.superclass) + return obj.class.base_class.name + end + obj.class.name end def apply_options_to_scope(scope, options = {}) @@ -27,5 +32,11 @@ def apply_options_to_scope(scope, options = {}) scope end + def parent_classes + return DEFAULT_PARENTS unless ActsAsFollower.custom_parent_classes + + ActiveSupport::Deprecation.warn("Setting custom parent classes is deprecated and will be removed in future versions.") + ActsAsFollower.custom_parent_classes + DEFAULT_PARENTS + end end end diff --git a/test/follow_test.rb b/test/follow_test.rb index 2f99749..b11cfe7 100644 --- a/test/follow_test.rb +++ b/test/follow_test.rb @@ -7,4 +7,22 @@ def test_assert_true_should_be_true assert true end + context "configuration with setters" do + should "contain custom parents" do + ActsAsFollower.custom_parent_classes = [CustomRecord] + + assert_equal [CustomRecord], ActsAsFollower.custom_parent_classes + end + end + + context "#setup" do + should "contain custom parents via setup" do + ActsAsFollower.setup do |c| + c.custom_parent_classes = [CustomRecord] + end + + assert_equal [CustomRecord], ActsAsFollower.custom_parent_classes + end + end + end