Class: Poll
- Inherits:
-
Renderable
- Object
- Renderable
- Poll
- Includes:
- E9Polls::Model, E9Rails::ActiveRecord::InheritableOptions
- Defined in:
- app/models/poll.rb
Instance Method Summary collapse
- #answers ⇒ Object
- #as_json(options = {}) ⇒ Object
- #percentage_for(poll_answer) ⇒ Object
- #question ⇒ Object
- #vote ⇒ Object
- #vote=(id) ⇒ Object
- #votes ⇒ Object
Instance Method Details
#answers ⇒ Object
33 34 35 |
# File 'app/models/poll.rb', line 33 def answers poll_answers end |
#as_json(options = {}) ⇒ Object
19 20 21 22 23 24 25 26 27 |
# File 'app/models/poll.rb', line 19 def as_json(={}) {}.tap do |hash| hash[:id] = self.id, hash[:name] = self.name, hash[:question] = self.question, hash[:answers] = self.answers, hash[:errors] = self.errors end end |
#percentage_for(poll_answer) ⇒ Object
45 46 47 |
# File 'app/models/poll.rb', line 45 def percentage_for(poll_answer) votes.zero? ? 0 : (poll_answer.votes.to_f / votes * 100).round(2) end |
#question ⇒ Object
29 30 31 |
# File 'app/models/poll.rb', line 29 def question template end |
#vote ⇒ Object
15 16 17 |
# File 'app/models/poll.rb', line 15 def vote nil end |
#vote=(id) ⇒ Object
37 38 39 |
# File 'app/models/poll.rb', line 37 def vote=(id) poll_answers.find_by_id(id).try(:vote!) end |
#votes ⇒ Object
41 42 43 |
# File 'app/models/poll.rb', line 41 def votes poll_answers.sum(:votes).to_i end |