Module: ActsAsVotable::Votable
- Includes:
- 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
- #count_votes_down(skip_cache = false) ⇒ Object
-
#count_votes_total(skip_cache = false) ⇒ Object
counting.
- #count_votes_up(skip_cache = false) ⇒ Object
- #default_conditions ⇒ Object
- #down_votes ⇒ Object
-
#find_votes(extra_conditions = {}) ⇒ Object
results.
- #unvote(args = {}) ⇒ Object
- #up_votes ⇒ Object
-
#update_cached_votes ⇒ Object
caching.
-
#vote(args = {}) ⇒ Object
voting.
- #vote_down(voter) ⇒ Object
- #vote_registered? ⇒ Boolean
- #vote_up(voter) ⇒ Object
-
#voted_on_by?(voter) ⇒ Boolean
voters.
Methods included from Helpers::Words
Instance Attribute Details
#vote_registered ⇒ Object
Returns the value of attribute vote_registered.
53 54 55 |
# File 'lib/acts_as_votable/votable.rb', line 53 def vote_registered @vote_registered end |
Class Method Details
.included(base) ⇒ Object
8 9 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 51 |
# File 'lib/acts_as_votable/votable.rb', line 8 def self.included base # allow the user to define these himself aliases = { :vote_up => [ :up_by, :upvote_by, :like_by, :liked_by, :vote_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 ], :up_votes => [ :true_votes, :ups, :upvotes, :likes, :positives, :for_votes, ], :down_votes => [ :false_votes, :downs, :downvotes, :dislikes, :negatives ], :unvote => [ :unliked_by, :undisliked_by ] } base.class_eval do belongs_to :votable, :polymorphic => true has_many :votes, :class_name => "ActsAsVotable::Vote", :as => :votable 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
#count_votes_down(skip_cache = false) ⇒ Object
181 182 183 184 185 186 |
# File 'lib/acts_as_votable/votable.rb', line 181 def count_votes_down skip_cache = false if !skip_cache && self.respond_to?(:cached_votes_down) return self.send(:cached_votes_down) end down_votes.count end |
#count_votes_total(skip_cache = false) ⇒ Object
counting
167 168 169 170 171 172 |
# File 'lib/acts_as_votable/votable.rb', line 167 def count_votes_total skip_cache = false if !skip_cache && self.respond_to?(:cached_votes_total) return self.send(:cached_votes_total) end find_votes.count end |
#count_votes_up(skip_cache = false) ⇒ Object
174 175 176 177 178 179 |
# File 'lib/acts_as_votable/votable.rb', line 174 def count_votes_up skip_cache = false if !skip_cache && self.respond_to?(:cached_votes_up) return self.send(:cached_votes_up) end up_votes.count end |
#default_conditions ⇒ Object
59 60 61 62 63 64 |
# File 'lib/acts_as_votable/votable.rb', line 59 def default_conditions { :votable_id => self.id, :votable_type => self.class.base_class.name.to_s } end |
#down_votes ⇒ Object
161 162 163 |
# File 'lib/acts_as_votable/votable.rb', line 161 def down_votes find_votes(:vote_flag => false) end |
#find_votes(extra_conditions = {}) ⇒ Object
results
153 154 155 |
# File 'lib/acts_as_votable/votable.rb', line 153 def find_votes extra_conditions = {} votes.where(extra_conditions) end |
#unvote(args = {}) ⇒ Object
111 112 113 114 115 116 117 118 119 120 |
# File 'lib/acts_as_votable/votable.rb', line 111 def unvote args = {} return false if args[:voter].nil? _votes_ = find_votes(:voter_id => args[:voter].id, :voter_type => args[:voter].class.name) return true if _votes_.size == 0 _votes_.each(&:destroy) update_cached_votes self.vote_registered = false if votes.count == 0 return true end |
#up_votes ⇒ Object
157 158 159 |
# File 'lib/acts_as_votable/votable.rb', line 157 def up_votes find_votes(:vote_flag => true) end |
#update_cached_votes ⇒ Object
caching
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/acts_as_votable/votable.rb', line 131 def update_cached_votes updates = {} if self.respond_to?(:cached_votes_total=) updates[:cached_votes_total] = count_votes_total(true) end if self.respond_to?(:cached_votes_up=) updates[:cached_votes_up] = count_votes_up(true) end if self.respond_to?(:cached_votes_down=) updates[:cached_votes_down] = count_votes_down(true) end self.update_attributes(updates, :without_protection => true) if updates.size > 0 end |
#vote(args = {}) ⇒ Object
voting
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 106 107 108 109 |
# File 'lib/acts_as_votable/votable.rb', line 67 def vote args = {} = { :vote => true, }.merge(args) self.vote_registered = false if [:voter].nil? return false end # find the vote _votes_ = find_votes({ :voter_id => [:voter].id, :voter_type => [:voter].class.name }) if _votes_.count == 0 # this voter has never voted vote = ActsAsVotable::Vote.new( :votable => self, :voter => [:voter] ) else # this voter is potentially changing his vote vote = _votes_.first end last_update = vote.updated_at vote.vote_flag = votable_words.meaning_of([:vote]) if vote.save self.vote_registered = true if last_update != vote.updated_at update_cached_votes return true else self.vote_registered = false return false end end |
#vote_down(voter) ⇒ Object
126 127 128 |
# File 'lib/acts_as_votable/votable.rb', line 126 def vote_down voter self.vote :voter => voter, :vote => false end |
#vote_registered? ⇒ Boolean
55 56 57 |
# File 'lib/acts_as_votable/votable.rb', line 55 def vote_registered? return self.vote_registered end |
#vote_up(voter) ⇒ Object
122 123 124 |
# File 'lib/acts_as_votable/votable.rb', line 122 def vote_up voter self.vote :voter => voter, :vote => true end |
#voted_on_by?(voter) ⇒ Boolean
voters
189 190 191 192 |
# File 'lib/acts_as_votable/votable.rb', line 189 def voted_on_by? voter votes = find_votes :voter_id => voter.id, :voter_type => voter.class.name votes.count > 0 end |