Class: Tadpoll::Poll
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Tadpoll::Poll
- Defined in:
- lib/tadpoll/poll.rb
Class Method Summary collapse
-
.create_poll_with_options(name, options = []) ⇒ Object
Create new poll with option.
Instance Method Summary collapse
-
#create_options(options = []) ⇒ Object
Create new option for a poll.
-
#find_votes_for(args = {}) ⇒ Object
Votes with given parameters.
-
#unvote(voter) ⇒ Object
Remove vote from poll for given voter.
-
#voted_on? ⇒ Boolean
T / F Has this Poll been voted on.
-
#voted_on_by?(voter) ⇒ Boolean
T / F Has this Poll been voted on by given Voter.
Class Method Details
.create_poll_with_options(name, options = []) ⇒ Object
Create new poll with option
11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/tadpoll/poll.rb', line 11 def self.(name, = []) return false if name.blank? poll = Tadpoll::Poll.new(name: name) if poll.save poll.() return poll else return false end end |
Instance Method Details
#create_options(options = []) ⇒ Object
Create new option for a poll
25 26 27 28 29 30 31 |
# File 'lib/tadpoll/poll.rb', line 25 def ( = []) if .any? .each do |option| Tadpoll::Option.new_option(option, self.id) end end end |
#find_votes_for(args = {}) ⇒ Object
Votes with given parameters
44 45 46 |
# File 'lib/tadpoll/poll.rb', line 44 def find_votes_for(args = {}) votes.where(args) end |
#unvote(voter) ⇒ Object
Remove vote from poll for given voter
34 35 36 37 38 39 40 41 |
# File 'lib/tadpoll/poll.rb', line 34 def unvote(voter) return false if voter.nil? if self.voted_on_by?(voter) _votes_ = find_votes_for({voter_id: voter.id}) _votes_.each(&:destroy) end return true end |
#voted_on? ⇒ Boolean
T / F Has this Poll been voted on
49 50 51 |
# File 'lib/tadpoll/poll.rb', line 49 def voted_on? votes.count > 0 end |
#voted_on_by?(voter) ⇒ Boolean
T / F Has this Poll been voted on by given Voter
54 55 56 57 |
# File 'lib/tadpoll/poll.rb', line 54 def voted_on_by?(voter) votes = find_votes_for({voter_id: voter.id}) votes.count > 0 end |