Class: Tadpoll::Option
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Tadpoll::Option
- Defined in:
- lib/tadpoll/option.rb
Class Method Summary collapse
-
.new_option(name, poll_id) ⇒ Object
Create Option.
Instance Method Summary collapse
-
#find_votes_for(voter) ⇒ Object
Votes with given parameters.
-
#unvote(voter) ⇒ Object
Destory Votes with given parameters.
-
#vote(voter) ⇒ Object
Vote.
-
#vote_count ⇒ Object
Count of Votes for an Option.
-
#voted_on? ⇒ Boolean
T / F Has the Option been voted on.
-
#voted_on_by?(voter) ⇒ Boolean
T / F Has this Option been voted on by given Voter.
Class Method Details
Instance Method Details
#find_votes_for(voter) ⇒ Object
Votes with given parameters
54 55 56 |
# File 'lib/tadpoll/option.rb', line 54 def find_votes_for(voter) votes.where(voter_id: voter.id) end |
#unvote(voter) ⇒ Object
Destory Votes with given parameters
44 45 46 47 48 49 50 51 |
# File 'lib/tadpoll/option.rb', line 44 def unvote(voter) return false if voter.nil? if self.voted_on_by?(voter) _votes_ = find_votes_for(voter) _votes_.each(&:destroy) end return true end |
#vote(voter) ⇒ Object
Vote
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/tadpoll/option.rb', line 22 def vote(voter) return false if voter.nil? if self.poll.voted_on_by?(voter) return false else vote = Tadpoll::Vote.new( option: self, voter_id: voter.id, voter_type: voter.class.base_class.name, poll_id: self.poll_id ) end if vote.save return true else return false end end |
#vote_count ⇒ Object
Count of Votes for an Option
59 60 61 |
# File 'lib/tadpoll/option.rb', line 59 def vote_count votes.count end |
#voted_on? ⇒ Boolean
T / F Has the Option been voted on
64 65 66 |
# File 'lib/tadpoll/option.rb', line 64 def voted_on? votes.count > 0 end |
#voted_on_by?(voter) ⇒ Boolean
T / F Has this Option been voted on by given Voter
69 70 71 72 |
# File 'lib/tadpoll/option.rb', line 69 def voted_on_by?(voter) votes = find_votes_for(voter) votes.count > 0 end |