Module: Mongoid::Votable
Constant Summary collapse
- VOTE_POINT =
How many points should be assigned for each up or down vote. This array should be manipulated using Votable.vote_point method
{}
Instance Method Summary collapse
-
#down_voter_ids ⇒ Object
Array of down voter ids.
-
#down_votes_count ⇒ Object
Get the number of down votes.
-
#up_voter_ids ⇒ Object
Array of up voter ids.
-
#up_votes_count ⇒ Object
Get the number of up votes.
-
#vote(options) ⇒ Object
Make a vote on this votee.
-
#vote_value(voter) ⇒ Object
Get a voted value on this votee.
-
#votes_count ⇒ Object
Get the number of votes count.
-
#votes_point ⇒ Object
(also: #votes_score)
Get the votes point.
Instance Method Details
#down_voter_ids ⇒ Object
Array of down voter ids
239 240 241 242 243 244 245 |
# File 'lib/mongoid/votable.rb', line 239 def down_voter_ids if self.has_attribute?(:votable) self["votable"]["down_voter_ids"] else [] end end |
#down_votes_count ⇒ Object
Get the number of down votes
206 207 208 |
# File 'lib/mongoid/votable.rb', line 206 def down_votes_count down_voter_ids.length end |
#up_voter_ids ⇒ Object
Array of up voter ids
230 231 232 233 234 235 236 |
# File 'lib/mongoid/votable.rb', line 230 def up_voter_ids if self.has_attribute?(:votable) self['votable']["up_voter_ids"] else [] end end |
#up_votes_count ⇒ Object
Get the number of up votes
201 202 203 |
# File 'lib/mongoid/votable.rb', line 201 def up_votes_count up_voter_ids.length end |
#vote(options) ⇒ Object
Make a vote on this votee
174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/mongoid/votable.rb', line 174 def vote() [:votee_id] ||= _id [:votee] ||= self if [:unvote] [:value] ||= vote_value([:voter_id]) else [:revote] ||= !vote_value([:voter_id]).nil? end self.class.vote() end |
#vote_value(voter) ⇒ Object
Get a voted value on this votee
Let’s NOT CONVERT bson::objectid’s to strings. vp 2022-08-31
192 193 194 195 196 197 198 |
# File 'lib/mongoid/votable.rb', line 192 def vote_value(voter) ## The last last one, voter.id not voter.id.to_s _vp_ 2022-09-19 voter_id = voter.is_a?(BSON::ObjectId) ? voter : voter.is_a?(String) ? voter : voter.id return :up if up_voter_ids.try(:include?, voter_id) return :down if down_voter_ids.try(:include?, voter_id) end |
#votes_count ⇒ Object
Get the number of votes count
211 212 213 214 215 216 217 |
# File 'lib/mongoid/votable.rb', line 211 def votes_count if self.has_attribute?(:votable) self["votable"]["votes_count"] else 0 end end |
#votes_point ⇒ Object Also known as: votes_score
Get the votes point
220 221 222 223 224 225 226 |
# File 'lib/mongoid/votable.rb', line 220 def votes_point if self.has_attribute?(:votable) self["votable"]["votes_point"] else 0 end end |