Class: Voterable::Voteable

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document, Mongoid::Timestamps
Defined in:
lib/voterable/voteable.rb

Overview

A Voteable is any object that can be voted on, Voteables can be voted up or down, Voteables keep track of the votes in either direction

Voteables has_many votes, Voteables can belong_to one voter. It is up to the outsied program to assign voteables to voters

Constant Summary collapse

VOTEABLE =

after_initialize :setup

{}
VOTEBACK =
{}
TALLY_TYPES =
{ :all_time => [0, Time.now - Time.at(0)],
 :year     => [0, 365.days_in_seconds],
 :month    => [0, 30.days_in_seconds],
 :week     => [0, 7.days_in_seconds],
 :day      => [0, 1.days_in_seconds]
}
TALLYS =
%w(point count up down).freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.down_voted_by(voter) ⇒ Object



74
75
76
77
# File 'lib/voterable/voteable.rb', line 74

def self.down_voted_by(voter)
   votes = Vote.where(voter_id:voter.id, voteable_type:self.name, vote: :down)
   votes.collect{|x| x.voteable}.compact
end

.options(value) ⇒ Object



53
54
55
# File 'lib/voterable/voteable.rb', line 53

def self.options(value)
   VOTEABLE[name][value] ||= 0
end

.sort_by(hsh = {}) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/voterable/voteable.rb', line 99

def self.sort_by(hsh = {})

   hsh[:page]       ||= 1         
   hsh[:limit]      ||= 30
   hsh[:period]     ||= :all_time
   hsh[:tally_type] ||= :point 

   page = ( hsh[:page].to_i >= 1 ? hsh[:page].to_i : 1 ) 
   skip_count = (page-1)*hsh[:limit]

   sorted = nil

   case hsh[:period]
   when :latest
      # return self.order_by(:created_at, :desc).page(hsh[:page]).per(hsh[:limit])
      sorted = self.order_by(:created_at, :desc).skip(skip_count).limit(hsh[:limit])
   when :all_time
      index = '0.'
   when :year
      index = '1.'
   when :month
      index = '2.'
   when :week
      index = '3.'
   when :day
      index = '4.'
   end

   unless sorted
      string = "tallys." + index + hsh[:tally_type].to_s
      sorted = self.order_by(string,:desc).skip(skip_count).limit(hsh[:limit]) #.where(:tallys.exists => true)
   end

   # Array into the class and add necessary methods for pagination
   sorted.instance_variable_set("@current_page", page)
   sorted.instance_variable_set("@num_pages", (self.count.to_f/hsh[:limit]).ceil ) # TODO eliminate self.count
   sorted.instance_variable_set("@limit_value", hsh[:limit])
   sorted.instance_eval do
      def current_page
         @current_page        
      end
      def num_pages
         @num_pages
      end
      def limit_value
         @limit_value
      end
   end

   sorted
end

.up_voted_by(voter) ⇒ Object

Need to make sure these only return kind of voteable



69
70
71
72
# File 'lib/voterable/voteable.rb', line 69

def self.up_voted_by(voter)
   votes = Vote.where(voter_id:voter.id, voteable_type:self.name, vote: :up)
   votes.collect{|x| x.voteable}.compact
end

.update_tallysObject

Updates all the tallys for the specified class



62
63
64
65
66
# File 'lib/voterable/voteable.rb', line 62

def self.update_tallys
   self.all.each do |n| 
      n.update_tallys if n.votes.count > 0
   end
end

.voteable(klass = self, options = nil) ⇒ Object

Set Options



45
46
47
# File 'lib/voterable/voteable.rb', line 45

def self.voteable(klass = self, options = nil)
   VOTEABLE[klass.name] ||= options
end

.voteback(klass = self, options = nil) ⇒ Object



49
50
51
# File 'lib/voterable/voteable.rb', line 49

def self.voteback(klass = self, options = nil)
   VOTEBACK[klass.name] ||= options
end

.voted_on_by(voter) ⇒ Object

Returns hash with up and down votes this should be faster than getting up and down voteables seperatly

Examples:

getting up and down voted voteables

Voteable.voted_on_by(voter) # => {:up => [<up_voted>], :down => [<down_voted>]}


85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/voterable/voteable.rb', line 85

def self.voted_on_by(voter)
   votes = Vote.where(voter_id:voter.id, voteable_type:self.name)
   up_voted = [] ; down_voted = []
   votes.each do |vt|         #Sort voteables in to up and down voted
      if vt.vote == :up
         up_voted << vt.voteable
      elsif vt.vote == :down
         down_voted << vt.voteable
      end
   end
   {:up => up_voted, :down => down_voted}
end

.vtback(value) ⇒ Object



57
58
59
# File 'lib/voterable/voteable.rb', line 57

def self.vtback(value)
   VOTEBACK[name][value] ||= 0
end

Instance Method Details

#get_tally(tally_type, period = :all_time) ⇒ Object



264
265
266
267
# File 'lib/voterable/voteable.rb', line 264

def get_tally(tally_type, period = :all_time)
   tally = self.tallys.find_or_initialize_by(name: period)
   tally.public_send(tally_type)
end

#set_tally(tally_type, value, period = :all_time) ⇒ Object



269
270
271
272
# File 'lib/voterable/voteable.rb', line 269

def set_tally(tally_type, value, period = :all_time)
   tally = self.tallys.find_or_initialize_by(name: period)
   tally.public_send(tally_type.to_s+'=',value)
end

#setupObject



257
258
259
260
261
262
# File 'lib/voterable/voteable.rb', line 257

def setup
   #Add Empty Tally Documents
   TALLY_TYPES.each_key do |t|
      self.tallys << Tally.new(name: t) unless tallys.where(name: t).first
   end
end

#unvote(vtr, vt = nil) ⇒ Object



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
# File 'lib/voterable/voteable.rb', line 200

def unvote(vtr, vt = nil)
   original_points = self.point #Record original points to update user 

   vt ||= Vote.find(voter_id:vtr.id, voteable_id:self.id) 
   return nil unless vt # Return if vote doens't exist

   self.point -= self.class.options(vt.vote)
   vtr.reputation -= self.class.vtback(vt.vote)
   
   self.count -= 1

   value = vt.vote
   case value
      when :up   ; self.up -= 1
      when :down ; self.down -= 1
   end

   #Update votee reputation
   self.voter.reputation += self.point - original_points
   self.voter.save
   vtr.save

   vt.destroy
   self.save
end

#update_tally(period = :all_time, bracket_votes = nil) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/voterable/voteable.rb', line 235

def update_tally(period = :all_time, bracket_votes = nil)

   bracket_time = TALLY_TYPES[period] 
   time_1 = Time.now - bracket_time[1]
   time_2 = Time.now - bracket_time[0]

   if bracket_votes
      bracket_votes = bracket_votes.where(:updated_at.lte => time_2).and(:updated_at.gte => time_1)
   else
      bracket_votes = self.votes.where(:updated_at.lte => time_2).and(:updated_at.gte => time_1) #.to_a
   end
   up_count   = bracket_votes.where(vote: :up).count
   down_count = bracket_votes.where(vote: :down).count
   
   set_tally(:up, up_count, period)
   set_tally(:down, down_count, period)
   set_tally(:count, up_count+down_count, period)
   set_tally(:point, up_count*self.class.options(:up) + down_count*self.class.options(:down), period)

   bracket_votes
end

#update_tallysObject

Updates tally assuming that classes will



227
228
229
230
231
232
233
# File 'lib/voterable/voteable.rb', line 227

def update_tallys
   bracket_votes = nil
   TALLY_TYPES.each_key do |period|
      bracket_votes = self.update_tally(period,bracket_votes)
   end
   self.save
end

#vote(vtr, value) ⇒ vote

Vote the voteable thing up or down

Parameters:

  • value (Symbol)

    Value of the vote, either :up or :down

Returns:

  • (vote)

    vote that was cast



160
161
162
163
164
165
166
167
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
# File 'lib/voterable/voteable.rb', line 160

def vote(vtr, value)

   original_points = self.point #Record original points to update user 

   #Check that voter is not self's voter 
   return nil if vtr == self.voter

   vt = Vote.where(voter_id:vtr.id, voteable_id:self.id).first
   if vt && vt.vote == value
      unvote(vtr,vt)
      return nil
   elsif vt
      self.point -= self.class.options(vt.vote) #Remove old vote points
      vtr.reputation -= self.class.vtback(vt.vote)
   else
      self.count += 1
      vt = Vote.new(voter_id:vtr.id, voteable_id:self.id)
      votes << vt
   end

   case value
      when :up ; self.up += 1
      when :down ; self.down += 1
   end

   self.point += self.class.options(value) #Add new vote points
   vtr.reputation += self.class.vtback(value)
   self.save
   vtr.save

   #Update votee reputation
   self.voter.reputation += self.point - original_points
   self.voter.save

   # Set vote to up or down
   vt.vote= value
   vt.save
   return vt
end