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 =
{}
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



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

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

.options(value) ⇒ Object



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

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

.sort_by(hsh = {}) ⇒ Object



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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/voterable/voteable.rb', line 74

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 )
   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



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

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

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

Set Options



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

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

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



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

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

.vtback(value) ⇒ Object



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

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

Instance Method Details

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



243
244
245
246
# File 'lib/voterable/voteable.rb', line 243

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



248
249
250
251
# File 'lib/voterable/voteable.rb', line 248

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



236
237
238
239
240
241
# File 'lib/voterable/voteable.rb', line 236

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, vote = nil) ⇒ Object



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

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

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

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

   value = vote.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

   votes.destroy_all(vote_id:vote.id)
   self.save
end

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



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/voterable/voteable.rb', line 214

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)
   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



206
207
208
209
210
211
212
# File 'lib/voterable/voteable.rb', line 206

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



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
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/voterable/voteable.rb', line 139

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

#votes_pointObject

Instance Methods



129
130
131
# File 'lib/voterable/voteable.rb', line 129

def votes_point
   point || 0
end