From 57d06cf3d5226ed65c3a0f7dbf64686ad661fe21 Mon Sep 17 00:00:00 2001 From: Eloy Date: Sat, 21 Oct 2017 18:12:06 +0200 Subject: [PATCH] New method find_votes_by --- lib/acts_as_votable/votable.rb | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/acts_as_votable/votable.rb b/lib/acts_as_votable/votable.rb index b0e39bd..db6b8b7 100644 --- a/lib/acts_as_votable/votable.rb +++ b/lib/acts_as_votable/votable.rb @@ -77,9 +77,7 @@ def vote_by(args = {}) end # find the vote - votes = find_votes_for(voter_id: options[:voter].id, - vote_scope: options[:vote_scope], - voter_type: options[:voter].class.base_class.name) + votes = find_votes_by(options[:voter], options[:vote_scope]) if votes.count == (0) || options[:duplicate] # this voter has never voted @@ -112,7 +110,7 @@ def vote_by(args = {}) def unvote(args = {}) return false if args[:voter].nil? - votes = find_votes_for(voter_id: args[:voter].id, vote_scope: args[:vote_scope], voter_type: args[:voter].class.base_class.name) + votes = find_votes_by(args[:voter], args[:vote_scope]) return true if votes.size == 0 votes.each(&:destroy) @@ -138,6 +136,12 @@ def find_votes_for(extra_conditions = {}) votes_for.where(extra_conditions) end + def find_votes_by(voter, vote_scope) + find_votes_for(voter_id: voter.id, + vote_scope: vote_scope, + voter_type: voter.class.base_class.name) + end + def get_up_votes(options = {}) vote_scope_hash = scope_or_empty_hash(options[:vote_scope]) find_votes_for({ vote_flag: true }.merge(vote_scope_hash))