Skip to content

Commit

Permalink
Add custom parent classes as setup option.
Browse files Browse the repository at this point in the history
  • Loading branch information
merqlove authored and LeFnord committed Jan 21, 2021
1 parent 82a9484 commit 4fb64f3
Show file tree
Hide file tree
Showing 11 changed files with 119 additions and 4 deletions.
12 changes: 12 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ 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: 20 additions & 0 deletions lib/acts_as_follower.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,25 @@ 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: 10 additions & 2 deletions lib/acts_as_follower/follower_lib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ module FollowerLib

private

DEFAULT_PARENTS = [ApplicationRecord, ActiveRecord::Base]

# Retrieves the parent class name if using STI.
def parent_class_name(obj)
if obj.class.superclass != ActiveRecord::Base
unless parent_classes.include?(obj.class.superclass)
return obj.class.superclass.name
end
return obj.class.name
obj.class.name
end

def apply_options_to_scope(scope, options = {})
Expand All @@ -29,5 +31,11 @@ def apply_options_to_scope(scope, options = {})
end
scope
end

def parent_classes
return DEFAULT_PARENTS unless ActsAsFollower.custom_parent_classes

ActsAsFollower.custom_parent_classes + DEFAULT_PARENTS
end
end
end
3 changes: 3 additions & 0 deletions test/dummy30/app/models/application_record.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
2 changes: 1 addition & 1 deletion test/dummy30/app/models/band.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Band < ActiveRecord::Base
class Band < ApplicationRecord
validates_presence_of :name
acts_as_followable
end
3 changes: 3 additions & 0 deletions test/dummy30/app/models/custom_record.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class CustomRecord < ActiveRecord::Base
self.abstract_class = true
end
5 changes: 5 additions & 0 deletions test/dummy30/app/models/some.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Some < CustomRecord
validates_presence_of :name
acts_as_follower
acts_as_followable
end
2 changes: 1 addition & 1 deletion test/dummy30/app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class User < ActiveRecord::Base
class User < ApplicationRecord
validates_presence_of :name
acts_as_follower
acts_as_followable
Expand Down
9 changes: 9 additions & 0 deletions test/factories/somes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FactoryGirl.define do
factory :daddy, :class => Some do |b|
b.name 'Daddy'
end

factory :mommy, :class => Some do |b|
b.name 'Mommy'
end
end
51 changes: 51 additions & 0 deletions test/follow_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,55 @@ 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
4 changes: 4 additions & 0 deletions test/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@
t.column :name, :string
end

create_table :somes, :force => true do |t|
t.column :name, :string
end

end

0 comments on commit 4fb64f3

Please sign in to comment.