Module: ActsAsVotable::Votable
- Includes:
- Cacheable, Helpers::Words
- Defined in:
- lib/acts_as_votable/votable.rb
Instance Attribute Summary collapse
-
#vote_registered ⇒ Object
Returns the value of attribute vote_registered.
Class Method Summary collapse
Instance Method Summary collapse
- #default_conditions ⇒ Object
- #find_votes_by(voter, vote_scope) ⇒ Object
-
#find_votes_for(extra_conditions = {}) ⇒ Object
results.
- #get_down_votes(options = {}) ⇒ Object
- #get_up_votes(options = {}) ⇒ Object
- #unvote(args = {}) ⇒ Object
- #unvote_by(voter, options = {}) ⇒ Object
-
#vote_by(args = {}) ⇒ Object
voting.
- #vote_down(voter, options = {}) ⇒ Object
- #vote_registered? ⇒ Boolean
- #vote_up(voter, options = {}) ⇒ Object
- #voted_down_by?(voter) ⇒ Boolean
-
#voted_on_by?(voter) ⇒ Boolean
voters.
- #voted_up_by?(voter) ⇒ Boolean
Methods included from Cacheable
#count_votes_down, #count_votes_score, #count_votes_total, #count_votes_up, #scope_cache_field, #update_cached_votes, #weighted_average, #weighted_score, #weighted_total
Methods included from Helpers::Words
Instance Attribute Details
#vote_registered ⇒ Object
Returns the value of attribute vote_registered.
52 53 54 |
# File 'lib/acts_as_votable/votable.rb', line 52 def vote_registered @vote_registered end |
Class Method Details
.included(base) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/acts_as_votable/votable.rb', line 10 def self.included(base) # allow the user to define these himself aliases = { vote_up: [ :up_by, :upvote_by, :like_by, :liked_by, :up_from, :upvote_from, :upvote_by, :like_from, :liked_from, :vote_from ], vote_down: [ :down_by, :downvote_by, :dislike_by, :disliked_by, :down_from, :downvote_from, :downvote_by, :dislike_by, :disliked_by ], get_up_votes: [ :get_true_votes, :get_ups, :get_upvotes, :get_likes, :get_positives, :get_for_votes, ], get_down_votes: [ :get_false_votes, :get_downs, :get_downvotes, :get_dislikes, :get_negatives ], unvote_by: [ :unvote_up, :unvote_down, :unliked_by, :undisliked_by ] } base.class_eval do has_many :votes_for, class_name: "ActsAsVotable::Vote", as: :votable, dependent: :delete_all do def voters includes(:voter).map(&:voter) end end aliases.each do |method, links| links.each do |new_method| alias_method(new_method, method) end end end end |
Instance Method Details
#default_conditions ⇒ Object
58 59 60 61 62 63 |
# File 'lib/acts_as_votable/votable.rb', line 58 def default_conditions { votable_id: self.id, votable_type: self.class.base_class.name.to_s } end |
#find_votes_by(voter, vote_scope) ⇒ Object
136 137 138 139 140 |
# File 'lib/acts_as_votable/votable.rb', line 136 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 |
#find_votes_for(extra_conditions = {}) ⇒ Object
results
132 133 134 |
# File 'lib/acts_as_votable/votable.rb', line 132 def find_votes_for(extra_conditions = {}) votes_for.where(extra_conditions) end |
#get_down_votes(options = {}) ⇒ Object
147 148 149 150 |
# File 'lib/acts_as_votable/votable.rb', line 147 def get_down_votes( = {}) vote_scope_hash = scope_or_empty_hash([:vote_scope]) find_votes_for({ vote_flag: false }.merge(vote_scope_hash)) end |
#get_up_votes(options = {}) ⇒ Object
142 143 144 145 |
# File 'lib/acts_as_votable/votable.rb', line 142 def get_up_votes( = {}) vote_scope_hash = scope_or_empty_hash([:vote_scope]) find_votes_for({ vote_flag: true }.merge(vote_scope_hash)) end |
#unvote(args = {}) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/acts_as_votable/votable.rb', line 107 def unvote(args = {}) return false if args[:voter].nil? votes = find_votes_by(args[:voter], args[:vote_scope]) ActiveRecord::Base.transaction do deleted_count = votes.delete_all update_cached_votes(args[:vote_scope]) if deleted_count > 0 end self.vote_registered = false return true end |
#unvote_by(voter, options = {}) ⇒ Object
127 128 129 |
# File 'lib/acts_as_votable/votable.rb', line 127 def unvote_by(voter, = {}) self.unvote voter: voter, vote_scope: [:vote_scope] #Does not need vote_weight since the votes_for are anyway getting destroyed end |
#vote_by(args = {}) ⇒ Object
voting
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/acts_as_votable/votable.rb', line 66 def vote_by(args = {}) return false if args[:voter].nil? = { vote: true, vote_scope: nil }.merge(args) self.vote_registered = false # find the vote votes = find_votes_by([:voter], [:vote_scope]) if [:duplicate] || !votes.exists? # this voter has never voted vote = ActsAsVotable::Vote.new( votable: self, voter: [:voter], vote_scope: [:vote_scope] ) else # this voter is potentially changing his vote vote = votes.last end last_update = vote.updated_at vote.vote_flag = votable_words.meaning_of([:vote]) #Allowing for a vote_weight to be associated with every vote. Could change with every voter object vote.vote_weight = ([:vote_weight].to_i if [:vote_weight].present?) || 1 vote_saved = false ActiveRecord::Base.transaction do self.vote_registered = false vote_saved = vote.save if vote_saved self.vote_registered = true if last_update != vote.updated_at update_cached_votes([:vote_scope]) end end vote_saved end |
#vote_down(voter, options = {}) ⇒ Object
123 124 125 |
# File 'lib/acts_as_votable/votable.rb', line 123 def vote_down(voter, = {}) self.vote_by voter: voter, vote: false, vote_scope: [:vote_scope], vote_weight: [:vote_weight] end |
#vote_registered? ⇒ Boolean
54 55 56 |
# File 'lib/acts_as_votable/votable.rb', line 54 def vote_registered? return self.vote_registered end |
#vote_up(voter, options = {}) ⇒ Object
119 120 121 |
# File 'lib/acts_as_votable/votable.rb', line 119 def vote_up(voter, = {}) self.vote_by voter: voter, vote: true, vote_scope: [:vote_scope], vote_weight: [:vote_weight] end |
#voted_down_by?(voter) ⇒ Boolean
165 166 167 168 169 170 |
# File 'lib/acts_as_votable/votable.rb', line 165 def voted_down_by?(voter) votes = find_votes_for(voter_id: voter.id, vote_flag: false, voter_type: voter.class.base_class.name) votes.exists? end |
#voted_on_by?(voter) ⇒ Boolean
voters
153 154 155 156 |
# File 'lib/acts_as_votable/votable.rb', line 153 def voted_on_by?(voter) votes = find_votes_for voter_id: voter.id, voter_type: voter.class.base_class.name votes.exists? end |
#voted_up_by?(voter) ⇒ Boolean
158 159 160 161 162 163 |
# File 'lib/acts_as_votable/votable.rb', line 158 def voted_up_by?(voter) votes = find_votes_for(voter_id: voter.id, vote_flag: true, voter_type: voter.class.base_class.name) votes.exists? end |