Skip to content

Commit

Permalink
refactor (custom parent config not needed for rails5 or sti compatibi…
Browse files Browse the repository at this point in the history
…lity)
  • Loading branch information
brchristian authored and LeFnord committed Jan 21, 2021
1 parent 33a297d commit efad5e5
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 94 deletions.
12 changes: 0 additions & 12 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,6 @@ Make your model(s) that can follow other models acts_as_follower
...
end

Extended setup:
# config/initializers/acts_as_follower.rb

# By default list of parent classes includes only `[ApplicationRecord, ActiveRecord::Base]`.
ActsAsFollower.custom_parent_classes = [CustomRecord]

# OR

ActsAsFollower.setup do |c|
c.custom_parent_classes = [...]
end

---

=== acts_as_follower methods
Expand Down
20 changes: 0 additions & 20 deletions lib/acts_as_follower.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,5 @@ 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)
@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
12 changes: 1 addition & 11 deletions lib/acts_as_follower/follower_lib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,9 @@ module FollowerLib

private

DEFAULT_PARENTS = [ApplicationRecord, ActiveRecord::Base]

# Retrieves the parent class name if using STI.
def parent_class_name(obj)
unless parent_classes.include?(obj.class.superclass)
return obj.class.base_class.name
end
obj.class.name
obj.class.base_class.name
end

def apply_options_to_scope(scope, options = {})
Expand All @@ -32,10 +27,5 @@ def apply_options_to_scope(scope, options = {})
scope
end

def parent_classes
return DEFAULT_PARENTS unless ActsAsFollower.custom_parent_classes

ActsAsFollower.custom_parent_classes + DEFAULT_PARENTS
end
end
end
51 changes: 0 additions & 51 deletions test/follow_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,55 +7,4 @@ 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

context "with custom parents" do
setup do
@daddy = FactoryGirl.create(:daddy)
@mommy = FactoryGirl.create(:mommy)
@oasis = FactoryGirl.create(:oasis)
@metallica = FactoryGirl.create(:metallica)
end

should "be followed" do
ActsAsFollower.custom_parent_classes = [CustomRecord]

@daddy.follow(@mommy)
@daddy.follow(@metallica)
@mommy.follow(@oasis)
assert_equal true, @daddy.following?(@mommy)
assert_equal false, @mommy.following?(@daddy)
assert_equal true, @mommy.followed_by?(@daddy)
assert_equal false, @daddy.followed_by?(@mommy)
assert_equal true, @metallica.followed_by?(@daddy)
assert_equal true, @oasis.followed_by?(@mommy)
assert_equal true, @daddy.following?(@metallica)
assert_equal true, @mommy.following?(@oasis)
end

should "be not followed" do
ActsAsFollower.custom_parent_classes = []

@daddy.follow(@mommy)
@mommy.follow(@oasis)
assert_equal false, @daddy.following?(@mommy)
assert_equal false, @mommy.following?(@oasis)
end
end
end

0 comments on commit efad5e5

Please sign in to comment.