Class: Formol::Poll
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Formol::Poll
show all
- Defined in:
- app/models/formol/poll.rb
Defined Under Namespace
Classes: NoOptionProvided, Option, PollException, PollExpiredError, TooManyVotesError, TopicLockedError, UserAlreadyVoter, Vote
Instance Attribute Summary collapse
Instance Method Summary
collapse
Instance Attribute Details
#poll_vote_ids ⇒ Object
16
17
18
|
# File 'app/models/formol/poll.rb', line 16
def poll_vote_ids
@poll_vote_ids
end
|
Instance Method Details
#expired? ⇒ Boolean
67
68
69
|
# File 'app/models/formol/poll.rb', line 67
def expired?
expires_at < Time.now
end
|
#expires_at ⇒ Object
71
72
73
|
# File 'app/models/formol/poll.rb', line 71
def expires_at
created_at + duration.to_i
end
|
#multiple? ⇒ Boolean
59
60
61
|
# File 'app/models/formol/poll.rb', line 59
def multiple?
max_options > 1
end
|
#single? ⇒ Boolean
63
64
65
|
# File 'app/models/formol/poll.rb', line 63
def single?
!multiple?
end
|
#vote(user, option_ids) ⇒ Object
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
# File 'app/models/formol/poll.rb', line 79
def vote(user, option_ids)
option_ids = [option_ids] unless option_ids.respond_to?(:each)
option_ids.reject!(&:blank?)
option_ids.compact!
raise NoOptionProvided.new if option_ids.empty?
raise TooManyVotesError.new if option_ids.length > max_options
raise UserAlreadyVoter.new if voted?(user)
raise TopicLockedError.new if topic.locked?
raise PollExpiredError.new if expired?
options.find(option_ids).to_a.each do |option| option.votes.create!(:voter => user)
end
end
|
#voted?(user) ⇒ Boolean
75
76
77
|
# File 'app/models/formol/poll.rb', line 75
def voted?(user)
voters.include?(user)
end
|