Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make dependent strategy configurable #219

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/acts_as_votable/extenders/votable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module ActsAsVotable
module Extenders
module Votable
ALLOWED_CACHEABLE_STRATEGIES = %i[update update_columns]
ALLOWED_DEPENDENT_STRATEGIES = %i[destroy_all delete_all]

def votable?
false
Expand All @@ -16,14 +17,19 @@ def acts_as_votable(args = {})
if args.key?(:cacheable_strategy) && !ALLOWED_CACHEABLE_STRATEGIES.include?(args[:cacheable_strategy])
raise ArgumentError, args[:cacheable_strategy]
end

if args.key?(:dependent_strategy) && !ALLOWED_DEPENDENT_STRATEGIES.include?(args[:dependent_strategy])
raise ArgumentError, args[:dependent_strategy]
end

define_method :acts_as_votable_options do
self.class.instance_variable_get("@acts_as_votable_options")
end

class_eval do
@acts_as_votable_options = {
cacheable_strategy: :update
cacheable_strategy: :update,
dependent_strategy: :delete_all
}.merge(args)

def self.votable?
Expand Down
4 changes: 2 additions & 2 deletions lib/acts_as_votable/votable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def self.included(base)
}

base.class_eval do
has_many :votes_for, class_name: "ActsAsVotable::Vote", as: :votable, dependent: :delete_all do
has_many :votes_for, class_name: "ActsAsVotable::Vote", as: :votable, dependent: acts_as_votable_options[:dependent_strategy] do
def voters
includes(:voter).map(&:voter)
end
Expand Down Expand Up @@ -110,7 +110,7 @@ def unvote(args = {})
votes = find_votes_by(args[:voter], args[:vote_scope])

ActiveRecord::Base.transaction do
deleted_count = votes.delete_all
deleted_count = votes.send(acts_as_votable_options[:dependent_strategy])
update_cached_votes(args[:vote_scope]) if deleted_count > 0
end
self.vote_registered = false
Expand Down
2 changes: 1 addition & 1 deletion lib/acts_as_votable/voter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def self.included(base)

base.class_eval do

has_many :votes, class_name: "ActsAsVotable::Vote", as: :voter, dependent: :delete_all do
has_many :votes, class_name: "ActsAsVotable::Vote", as: :voter, dependent: acts_as_votable_options[:dependent_strategy] do
def votables
includes(:votable).map(&:votable)
end
Expand Down