Module: Mongo::Voteable
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/voteable_mongo/tasks.rb,
lib/voteable_mongo/voting.rb,
lib/voteable_mongo/helpers.rb,
lib/voteable_mongo/voteable.rb,
lib/voteable_mongo/integrations/mongoid.rb
Defined Under Namespace
Modules: ClassMethods, Helpers, Integrations, Tasks, Voting
Constant Summary collapse
- DEFAULT_VOTES =
{ 'up' => [], 'down' => [], 'up_count' => 0, 'down_count' => 0, 'count' => 0, 'point' => 0 }
- VOTEABLE =
How many points should be assigned for each up or down vote and other options This hash should manipulated using voteable method
{}
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
154 155 156 |
# File 'lib/voteable_mongo/voteable.rb', line 154 def down_voter_ids votes.try(:[], 'down') || [] end |
#down_voters(klass) ⇒ Object
Get down voters
189 190 191 |
# File 'lib/voteable_mongo/voteable.rb', line 189 def down_voters(klass) klass.where(:_id => { '$in' => down_voter_ids }) end |
#down_votes_count ⇒ Object
Get the number of down votes
169 170 171 |
# File 'lib/voteable_mongo/voteable.rb', line 169 def down_votes_count votes.try(:[], 'down_count') || 0 end |
#up_voter_ids ⇒ Object
Array of up voter ids
149 150 151 |
# File 'lib/voteable_mongo/voteable.rb', line 149 def up_voter_ids votes.try(:[], 'up') || [] end |
#up_voters(klass) ⇒ Object
Get up voters
184 185 186 |
# File 'lib/voteable_mongo/voteable.rb', line 184 def up_voters(klass) klass.where(:_id => { '$in' => up_voter_ids }) end |
#up_votes_count ⇒ Object
Get the number of up votes
164 165 166 |
# File 'lib/voteable_mongo/voteable.rb', line 164 def up_votes_count votes.try(:[], 'up_count') || 0 end |
#vote(options) ⇒ Object
Make a vote on this votee
121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/voteable_mongo/voteable.rb', line 121 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
138 139 140 141 142 |
# File 'lib/voteable_mongo/voteable.rb', line 138 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
144 145 146 |
# File 'lib/voteable_mongo/voteable.rb', line 144 def voted_by?(voter) !!vote_value(voter) end |
#voter_ids ⇒ Object
Array of voter ids
159 160 161 |
# File 'lib/voteable_mongo/voteable.rb', line 159 def voter_ids up_voter_ids + down_voter_ids end |
#voters(klass) ⇒ Object
Get voters
194 195 196 |
# File 'lib/voteable_mongo/voteable.rb', line 194 def voters(klass) klass.where(:_id => { '$in' => voter_ids }) end |
#votes_count ⇒ Object
Get the number of votes
174 175 176 |
# File 'lib/voteable_mongo/voteable.rb', line 174 def votes_count votes.try(:[], 'count') || 0 end |
#votes_point ⇒ Object
Get the votes point
179 180 181 |
# File 'lib/voteable_mongo/voteable.rb', line 179 def votes_point votes.try(:[], 'point') || 0 end |