Module: Mongoid::AtomicVotes
- Defined in:
- lib/mongoid_atomic_votes/version.rb,
lib/mongoid_atomic_votes/atomic_votes.rb
Defined Under Namespace
Modules: ClassMethods Classes: Vote
Constant Summary collapse
- VERSION =
'1.0.0'
Class Method Summary collapse
Instance Method Summary collapse
-
#has_votes? ⇒ Boolean
Indicates whether the document has votes or not.
-
#retract(voted_by) ⇒ Boolean
Removes previously added vote.
-
#vote(value, voted_by) ⇒ Boolean
Creates an embedded vote record and updates number of votes and vote value.
-
#voted_by?(voted_by) ⇒ Boolean
Indicates whether the document has a vote from particular voter object.
Class Method Details
.included(base) ⇒ Object
4 5 6 7 8 9 10 |
# File 'lib/mongoid_atomic_votes/atomic_votes.rb', line 4 def included(base) define_fields(base) define_relations(base) define_scopes(base) base.extend ClassMethods end |
Instance Method Details
#has_votes? ⇒ Boolean
Indicates whether the document has votes or not.
69 70 71 |
# File 'lib/mongoid_atomic_votes/atomic_votes.rb', line 69 def has_votes? self.vote_count > 0 end |
#retract(voted_by) ⇒ Boolean
Removes previously added vote.
61 62 63 64 |
# File 'lib/mongoid_atomic_votes/atomic_votes.rb', line 61 def retract(voted_by) mark = self.votes.find_by(voted_by_id: voted_by.id) mark && remove_vote_mark(mark) end |
#vote(value, voted_by) ⇒ Boolean
Creates an embedded vote record and updates number of votes and vote value.
52 53 54 55 |
# File 'lib/mongoid_atomic_votes/atomic_votes.rb', line 52 def vote(value, voted_by) mark = Vote.new(value: value, voted_by_id: voted_by.id, voter_type: voted_by.class.name) add_vote_mark(mark) end |
#voted_by?(voted_by) ⇒ Boolean
Indicates whether the document has a vote from particular voter object
77 78 79 80 81 |
# File 'lib/mongoid_atomic_votes/atomic_votes.rb', line 77 def voted_by?(voted_by) !!self.votes.find_by(voted_by_id: voted_by.id) rescue NoMethodError, Mongoid::Errors::DocumentNotFound false end |