Module: Redmineup::ActsAsVotable::Votable
- Includes:
- Helpers::Words
- Defined in:
- lib/redmineup/acts_as_votable/up_acts_as_votable.rb,
lib/redmineup/acts_as_votable/votable.rb
Overview
:nodoc:
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, vote_scope = nil) ⇒ Object
-
#count_votes_total(skip_cache = false, vote_scope = nil) ⇒ Object
counting.
- #count_votes_up(skip_cache = false, vote_scope = nil) ⇒ Object
- #create_index(table_name, column_name) ⇒ Object
- #create_votable_table(options = {}) ⇒ Object
- #default_conditions ⇒ Object
- #drop_votable_table(options = {}) ⇒ Object
-
#find_votes_for(extra_conditions = {}) ⇒ Object
results.
- #get_down_votes(options = {}) ⇒ Object
- #get_up_votes(options = {}) ⇒ Object
- #scope_cache_field(field, vote_scope) ⇒ Object
- #unvote(args = {}) ⇒ Object
- #unvote_by(voter, options = {}) ⇒ Object
- #up_acts_as_votable ⇒ Object
-
#update_cached_votes(vote_scope = nil) ⇒ Object
caching.
- #votable? ⇒ Boolean
-
#vote_by(args = {}) ⇒ Object
voting.
- #vote_down(voter, options = {}) ⇒ Object
- #vote_registered? ⇒ Boolean
- #vote_up(voter, options = {}) ⇒ Object
-
#voted_on_by?(voter) ⇒ Boolean
voters.
- #weighted_average(skip_cache = false, vote_scope = nil) ⇒ Object
- #weighted_score(skip_cache = false, vote_scope = nil) ⇒ Object
- #weighted_total(skip_cache = false, vote_scope = nil) ⇒ Object
Methods included from Helpers::Words
Instance Attribute Details
#vote_registered ⇒ Object
Returns the value of attribute vote_registered.
49 50 51 |
# File 'lib/redmineup/acts_as_votable/votable.rb', line 49 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 |
# File 'lib/redmineup/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, :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 => 'Redmineup::ActsAsVotable::Vote', :as => :votable, :dependent => :destroy 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, vote_scope = nil) ⇒ Object
269 270 271 272 273 274 |
# File 'lib/redmineup/acts_as_votable/votable.rb', line 269 def count_votes_down(skip_cache = false, vote_scope = nil) if !skip_cache && self.respond_to?(scope_cache_field :cached_votes_down, vote_scope) return self.send(scope_cache_field :cached_votes_down, vote_scope) end get_down_votes(:vote_scope => vote_scope).count end |
#count_votes_total(skip_cache = false, vote_scope = nil) ⇒ Object
counting
255 256 257 258 259 260 |
# File 'lib/redmineup/acts_as_votable/votable.rb', line 255 def count_votes_total(skip_cache = false, vote_scope = nil) if !skip_cache && self.respond_to?(scope_cache_field :cached_votes_total, vote_scope) return self.send(scope_cache_field :cached_votes_total, vote_scope) end find_votes_for(scope_or_empty_hash(vote_scope)).count end |
#count_votes_up(skip_cache = false, vote_scope = nil) ⇒ Object
262 263 264 265 266 267 |
# File 'lib/redmineup/acts_as_votable/votable.rb', line 262 def count_votes_up(skip_cache = false, vote_scope = nil) if !skip_cache && self.respond_to?(scope_cache_field :cached_votes_up, vote_scope) return self.send(scope_cache_field :cached_votes_up, vote_scope) end get_up_votes(:vote_scope => vote_scope).count end |
#create_index(table_name, column_name) ⇒ Object
21 22 23 24 25 |
# File 'lib/redmineup/acts_as_votable/up_acts_as_votable.rb', line 21 def create_index(table_name, column_name) return if self.connection.index_exists?(table_name, column_name) self.connection.add_index table_name, column_name end |
#create_votable_table(options = {}) ⇒ Object
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/redmineup/acts_as_votable/up_acts_as_votable.rb', line 27 def create_votable_table( = {}) votes_name_table = [:votes] || :votes if !self.connection.table_exists?(votes_name_table) self.connection.create_table(votes_name_table) do |t| t.references :votable, :polymorphic => true t.references :voter, :polymorphic => true t.column :vote_flag, :boolean t.column :vote_scope, :string t.column :vote_weight, :integer t.column :vote_ip, :string t. end else #if table exists - check existence of separate columns fields = { :votable_id => :integer, :votable_type => :string, :voter_id => :integer, :voter_type => :string, :vote_flag => :boolean, :vote_scope => :string, :vote_weight => :integer, :vote_ip => :string } fields.each do |name, type| if !self.connection.column_exists?(votes_name_table, name) self.connection.add_column(votes_name_table, name, type) end end end if ::ActiveRecord::VERSION::MAJOR < 4 create_index votes_name_table, [:votable_id, :votable_type, :vote_ip] create_index votes_name_table, [:voter_id, :voter_type, :vote_ip] end create_index votes_name_table, [:voter_id, :voter_type, :vote_scope] create_index votes_name_table, [:votable_id, :votable_type, :vote_scope] create_index votes_name_table, [:voter_type, :vote_scope, :vote_ip] create_index votes_name_table, [:votable_type, :vote_scope, :vote_ip] end |
#default_conditions ⇒ Object
55 56 57 58 59 60 |
# File 'lib/redmineup/acts_as_votable/votable.rb', line 55 def default_conditions { :votable_id => self.id, :votable_type => self.class.base_class.name.to_s } end |
#drop_votable_table(options = {}) ⇒ Object
72 73 74 75 76 77 |
# File 'lib/redmineup/acts_as_votable/up_acts_as_votable.rb', line 72 def drop_votable_table( = {}) votes_name_table = [:votes] || :votes if self.connection.table_exists?(votes_name_table) self.connection.drop_table votes_name_table end end |
#find_votes_for(extra_conditions = {}) ⇒ Object
results
240 241 242 |
# File 'lib/redmineup/acts_as_votable/votable.rb', line 240 def find_votes_for(extra_conditions = {}) votes_for.where(extra_conditions) end |
#get_down_votes(options = {}) ⇒ Object
249 250 251 252 |
# File 'lib/redmineup/acts_as_votable/votable.rb', line 249 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
244 245 246 247 |
# File 'lib/redmineup/acts_as_votable/votable.rb', line 244 def get_up_votes( = {}) vote_scope_hash = scope_or_empty_hash([:vote_scope]) find_votes_for({:vote_flag => true}.merge(vote_scope_hash)) end |
#scope_cache_field(field, vote_scope) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/redmineup/acts_as_votable/votable.rb', line 132 def scope_cache_field(field, vote_scope) return field if vote_scope.nil? case field when :cached_votes_total= "cached_scoped_#{vote_scope}_votes_total=" when :cached_votes_total "cached_scoped_#{vote_scope}_votes_total" when :cached_votes_up= "cached_scoped_#{vote_scope}_votes_up=" when :cached_votes_up "cached_scoped_#{vote_scope}_votes_up" when :cached_votes_down= "cached_scoped_#{vote_scope}_votes_down=" when :cached_votes_down "cached_scoped_#{vote_scope}_votes_down" when :cached_votes_score= "cached_scoped_#{vote_scope}_votes_score=" when :cached_votes_score "cached_scoped_#{vote_scope}_votes_score" when :cached_weighted_total "cached_weighted_#{vote_scope}_total" when :cached_weighted_total= "cached_weighted_#{vote_scope}_total=" when :cached_weighted_score "cached_weighted_#{vote_scope}_score" when :cached_weighted_score= "cached_weighted_#{vote_scope}_score=" when :cached_weighted_average "cached_weighted_#{vote_scope}_average" when :cached_weighted_average= "cached_weighted_#{vote_scope}_average=" end end |
#unvote(args = {}) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/redmineup/acts_as_votable/votable.rb', line 101 def unvote(args = {}) return false if (!args[:vote_by_ip] && args[:voter].nil?) || (args[:vote_by_ip] && args[:vote_ip].nil?) vote_conditions = { :vote_scope => args[:vote_scope], :voter_type => args[:voter].class.base_class.name } vote_conditions.merge!(args[:vote_by_ip] ? { :vote_ip => args[:vote_ip] } : { :voter_id => args[:voter].id}) votes_for = find_votes_for(vote_conditions) return true if votes_for.empty? votes_for.each(&:destroy) update_cached_votes args[:vote_scope] self.vote_registered = false if votes_for.count == 0 true end |
#unvote_by(voter, options = {}) ⇒ Object
126 127 128 129 130 |
# File 'lib/redmineup/acts_as_votable/votable.rb', line 126 def unvote_by(voter, = {}) # Does not need vote_weight since the votes_for are anyway getting destroyed self.unvote :voter => voter, :vote_scope => [:vote_scope], :vote_ip => [:vote_ip], :vote_by_ip => [:vote_by_ip] end |
#up_acts_as_votable ⇒ Object
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/redmineup/acts_as_votable/up_acts_as_votable.rb', line 10 def up_acts_as_votable require 'redmineup/acts_as_votable/votable' include Redmineup::ActsAsVotable::Votable class_eval do def self.votable? true end end end |
#update_cached_votes(vote_scope = nil) ⇒ Object
caching
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/redmineup/acts_as_votable/votable.rb', line 168 def update_cached_votes(vote_scope = nil) 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 if self.respond_to?(:cached_votes_score=) updates[:cached_votes_score] = ( (updates[:cached_votes_up] || count_votes_up(true)) - (updates[:cached_votes_down] || count_votes_down(true)) ) end if self.respond_to?(:cached_weighted_total=) updates[:cached_weighted_total] = weighted_total(true) end if self.respond_to?(:cached_weighted_score=) updates[:cached_weighted_score] = weighted_score(true) end if self.respond_to?(:cached_weighted_average=) updates[:cached_weighted_average] = weighted_average(true) end if vote_scope if self.respond_to?(scope_cache_field :cached_votes_total=, vote_scope) updates[scope_cache_field :cached_votes_total, vote_scope] = count_votes_total(true, vote_scope) end if self.respond_to?(scope_cache_field :cached_votes_up=, vote_scope) updates[scope_cache_field :cached_votes_up, vote_scope] = count_votes_up(true, vote_scope) end if self.respond_to?(scope_cache_field :cached_votes_down=, vote_scope) updates[scope_cache_field :cached_votes_down, vote_scope] = count_votes_down(true, vote_scope) end if self.respond_to?(scope_cache_field :cached_weighted_total=, vote_scope) updates[scope_cache_field :cached_weighted_total, vote_scope] = weighted_total(true, vote_scope) end if self.respond_to?(scope_cache_field :cached_weighted_score=, vote_scope) updates[scope_cache_field :cached_weighted_score, vote_scope] = weighted_score(true, vote_scope) end if self.respond_to?(scope_cache_field :cached_votes_score=, vote_scope) updates[scope_cache_field :cached_votes_score, vote_scope] = ( (updates[scope_cache_field :cached_votes_up, vote_scope] || count_votes_up(true, vote_scope)) - (updates[scope_cache_field :cached_votes_down, vote_scope] || count_votes_down(true, vote_scope)) ) end if self.respond_to?(scope_cache_field :cached_weighted_average=, vote_scope) updates[scope_cache_field :cached_weighted_average, vote_scope] = weighted_average(true, vote_scope) end end self. = false self.update(updates) if !updates.empty? end |
#votable? ⇒ Boolean
6 7 8 |
# File 'lib/redmineup/acts_as_votable/up_acts_as_votable.rb', line 6 def votable? false end |
#vote_by(args = {}) ⇒ Object
voting
63 64 65 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 |
# File 'lib/redmineup/acts_as_votable/votable.rb', line 63 def vote_by(args = {}) = { :vote => true, :vote_scope => nil }.merge(args) self.vote_registered = false return false if [:voter].nil? # find the vote vote_conditions = { :vote_scope => [:vote_scope], :voter_type => [:voter].class.base_class.name } vote_conditions.merge!([:vote_by_ip] ? { :vote_ip => [:vote_ip] } : { :voter_id => [:voter].id} ) votes_for = find_votes_for(vote_conditions) if votes_for.count == 0 || [:duplicate] # this voter has never voted vote_params = { :votable => self, :voter => [:voter], :vote_scope => [:vote_scope] } vote_params[:vote_ip] = [:vote_ip] if [:vote_ip] vote = Redmineup::ActsAsVotable::Vote.new(vote_params) else # this voter is potentially changing his vote vote = votes_for.last end 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 return false if vote.invalid? self.vote_registered = vote.changed? vote.save!(validate: false) update_cached_votes([:vote_scope]) vote end |
#vote_down(voter, options = {}) ⇒ Object
120 121 122 123 124 |
# File 'lib/redmineup/acts_as_votable/votable.rb', line 120 def vote_down(voter, ={}) self.vote_by :voter => voter, :vote => false, :vote_scope => [:vote_scope], :vote_weight => [:vote_weight], :vote_ip => [:vote_ip], :vote_by_ip => [:vote_by_ip] end |
#vote_registered? ⇒ Boolean
51 52 53 |
# File 'lib/redmineup/acts_as_votable/votable.rb', line 51 def vote_registered? self.vote_registered end |
#vote_up(voter, options = {}) ⇒ Object
114 115 116 117 118 |
# File 'lib/redmineup/acts_as_votable/votable.rb', line 114 def vote_up(voter, ={}) self.vote_by :voter => voter, :vote => true, :vote_scope => [:vote_scope], :vote_weight => [:vote_weight], :vote_ip => [:vote_ip], :vote_by_ip => [:vote_by_ip] end |
#voted_on_by?(voter) ⇒ Boolean
voters
308 309 310 311 |
# File 'lib/redmineup/acts_as_votable/votable.rb', line 308 def voted_on_by?(voter) votes = find_votes_for :voter_id => voter.id, :voter_type => voter.class.base_class.name votes.count > 0 end |
#weighted_average(skip_cache = false, vote_scope = nil) ⇒ Object
294 295 296 297 298 299 300 301 302 303 304 305 |
# File 'lib/redmineup/acts_as_votable/votable.rb', line 294 def weighted_average(skip_cache = false, vote_scope = nil) if !skip_cache && self.respond_to?(scope_cache_field :cached_weighted_average, vote_scope) return self.send(scope_cache_field :cached_weighted_average, vote_scope) end count = count_votes_total(skip_cache, vote_scope).to_i if count > 0 weighted_score(skip_cache, vote_scope).to_f / count else 0.0 end end |
#weighted_score(skip_cache = false, vote_scope = nil) ⇒ Object
285 286 287 288 289 290 291 292 |
# File 'lib/redmineup/acts_as_votable/votable.rb', line 285 def weighted_score(skip_cache = false, vote_scope = nil) if !skip_cache && self.respond_to?(scope_cache_field :cached_weighted_score, vote_scope) return self.send(scope_cache_field :cached_weighted_score, vote_scope) end ups = get_up_votes(:vote_scope => vote_scope).sum(:vote_weight) downs = get_down_votes(:vote_scope => vote_scope).sum(:vote_weight) ups - downs end |
#weighted_total(skip_cache = false, vote_scope = nil) ⇒ Object
276 277 278 279 280 281 282 283 |
# File 'lib/redmineup/acts_as_votable/votable.rb', line 276 def weighted_total(skip_cache = false, vote_scope = nil) if !skip_cache && self.respond_to?(scope_cache_field :cached_weighted_total, vote_scope) return self.send(scope_cache_field :cached_weighted_total, vote_scope) end ups = get_up_votes(:vote_scope => vote_scope).sum(:vote_weight) downs = get_down_votes(:vote_scope => vote_scope).sum(:vote_weight) ups + downs end |