Skip to content

Commit

Permalink
restore and deprecate custom_parent_classes
Browse files Browse the repository at this point in the history
  • Loading branch information
brchristian committed Jan 20, 2017
1 parent 48b22a6 commit d84ac43
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
23 changes: 23 additions & 0 deletions lib/acts_as_follower.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 12 additions & 1 deletion lib/acts_as_follower/follower_lib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {})
Expand All @@ -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
18 changes: 18 additions & 0 deletions test/follow_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit d84ac43

Please sign in to comment.