Module: Mongo::Voteable::InstanceMethods
- Defined in:
- lib/voteable_mongo/voteable.rb
Instance Method Summary collapse
-
#down_voter_ids ⇒ Object
Array of down voter ids.
-
#down_voters(klass) ⇒ Object
Get down voters.
-
#down_votes_count ⇒ Object
Get the number of down votes.
-
#up_voter_ids ⇒ Object
Array of up voter ids.
-
#up_voters(klass) ⇒ Object
Get up voters.
-
#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.
- #voted_by?(voter) ⇒ Boolean
-
#voter_ids ⇒ Object
Array of voter ids.
-
#voters(klass) ⇒ Object
Get voters.
-
#votes_count ⇒ Object
Get the number of votes.
-
#votes_point ⇒ Object
Get the votes point.
Instance Method Details
#down_voter_ids ⇒ Object
Array of down voter ids
160 161 162 |
# File 'lib/voteable_mongo/voteable.rb', line 160 def down_voter_ids votes.try(:[], 'down') || [] end |
#down_voters(klass) ⇒ Object
Get down voters
195 196 197 |
# File 'lib/voteable_mongo/voteable.rb', line 195 def down_voters(klass) klass.where(:_id => { '$in' => down_voter_ids }) end |
#down_votes_count ⇒ Object
Get the number of down votes
175 176 177 |
# File 'lib/voteable_mongo/voteable.rb', line 175 def down_votes_count votes.try(:[], 'down_count') || 0 end |
#up_voter_ids ⇒ Object
Array of up voter ids
155 156 157 |
# File 'lib/voteable_mongo/voteable.rb', line 155 def up_voter_ids votes.try(:[], 'up') || [] end |
#up_voters(klass) ⇒ Object
Get up voters
190 191 192 |
# File 'lib/voteable_mongo/voteable.rb', line 190 def up_voters(klass) klass.where(:_id => { '$in' => up_voter_ids }) end |
#up_votes_count ⇒ Object
Get the number of up votes
170 171 172 |
# File 'lib/voteable_mongo/voteable.rb', line 170 def up_votes_count votes.try(:[], 'up_count') || 0 end |
#vote(options) ⇒ Object
Make a vote on this votee
127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/voteable_mongo/voteable.rb', line 127 def vote() [:votee_id] = id [:votee] = self [:voter_id] ||= [:voter].id if [:unvote] [:value] ||= vote_value([:voter_id]) else [:revote] ||= vote_value([:voter_id]).present? end self.class.vote() end |
#vote_value(voter) ⇒ Object
Get a voted value on this votee
144 145 146 147 148 |
# File 'lib/voteable_mongo/voteable.rb', line 144 def vote_value(voter) voter_id = Helpers.get_mongo_id(voter) return :up if up_voter_ids.include?(voter_id) return :down if down_voter_ids.include?(voter_id) end |
#voted_by?(voter) ⇒ Boolean
150 151 152 |
# File 'lib/voteable_mongo/voteable.rb', line 150 def voted_by?(voter) !!vote_value(voter) end |
#voter_ids ⇒ Object
Array of voter ids
165 166 167 |
# File 'lib/voteable_mongo/voteable.rb', line 165 def voter_ids up_voter_ids + down_voter_ids end |
#voters(klass) ⇒ Object
Get voters
200 201 202 |
# File 'lib/voteable_mongo/voteable.rb', line 200 def voters(klass) klass.where(:_id => { '$in' => voter_ids }) end |
#votes_count ⇒ Object
Get the number of votes
180 181 182 |
# File 'lib/voteable_mongo/voteable.rb', line 180 def votes_count votes.try(:[], 'count') || 0 end |
#votes_point ⇒ Object
Get the votes point
185 186 187 |
# File 'lib/voteable_mongo/voteable.rb', line 185 def votes_point votes.try(:[], 'point') || 0 end |