Class: Poll
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Poll
- Defined in:
- app/models/poll.rb
Class Method Summary collapse
-
.find_current ⇒ Object
Find the current poll.
- .marker ⇒ Object
Instance Method Summary collapse
-
#clear_responses ⇒ Object
clear the number of the responses for the poll and all of its options.
- #option_attributes=(option_attributes) ⇒ Object
-
#submit_response(option) ⇒ Object
submit a response to the poll and update the response counts for the poll and the option.
Class Method Details
.find_current ⇒ Object
Find the current poll. The current poll is defined as the poll that has the latest start date that is no later than the current date. If no poll has a defined start date, then there is no current poll.
16 17 18 19 |
# File 'app/models/poll.rb', line 16 def self.find_current Poll.find(:first, :order => 'start_date DESC', :conditions => [ 'start_date IS NOT NULL AND start_date <= ?', Date.today ]) end |
.marker ⇒ Object
11 |
# File 'app/models/poll.rb', line 11 def self.marker; "<!-- Polls Extension -->" end |
Instance Method Details
#clear_responses ⇒ Object
clear the number of the responses for the poll and all of its options
44 45 46 47 48 49 |
# File 'app/models/poll.rb', line 44 def clear_responses self.update_attribute(:response_count, 0) self..find(:all, :conditions => ["response_count >= 1"]).each do |option| option.update_attribute(:response_count, 0) end end |
#option_attributes=(option_attributes) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 |
# File 'app/models/poll.rb', line 21 def option_attributes=(option_attributes) option_attributes.each do |attributes| if attributes[:id].blank? attributes[:response_count] = 0 .build(attributes) else option = .detect { |a| a.id == attributes[:id].to_i } option.attributes = attributes end end end |
#submit_response(option) ⇒ Object
submit a response to the poll and update the response counts for the poll and the option
35 36 37 38 39 40 |
# File 'app/models/poll.rb', line 35 def submit_response(option) option.update_attribute(:response_count, option.response_count + 1) self.update_attribute(:response_count, self.response_count + 1) return true end |