Class: ResponseSet
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- ResponseSet
- Includes:
- TinyCode
- Defined in:
- app/models/response_set.rb
Instance Attribute Summary collapse
-
#current_section_id ⇒ Object
Returns the value of attribute current_section_id.
Instance Method Summary collapse
- #complete! ⇒ Object
-
#count_group_responses(group) ⇒ Object
Returns the number of response groups (count of group responses enterted) for this question group.
-
#count_question_responses(question) ⇒ Object
Counts the number of responses for the current user for this question.
- #default_args ⇒ Object
-
#empty? ⇒ Boolean
ResponseSet has an awareness of its internal state.
-
#find_response(answer_id) ⇒ Object
ResponseSet knows if certain answers are contained in this response set This method acts as an interface for the dependency analysis.
- #has_answered_question?(question) ⇒ Boolean
- #has_not_answered_question?(question) ⇒ Boolean
-
#initialize(*args) ⇒ ResponseSet
constructor
Instance methods.
-
#response_attributes=(response_attributes) ⇒ Object
“responses”=>{ string “6”=>“20”=>{“string_value”=>“saf”}, text “7”=>“21”=>{“text_value”=>“”}, radio+txt “1”=>“answer_id”=>“1”, “4”=>{“string_value”=>“”}, radio “2”=>“answer_id”=>“6”, radio “3”=>“answer_id”=>“10”, check “4”=>“answer_id”=>“15”, check+txt “5”=>“16”=>{“selected”=>“1”, “19”=>“string_value”=>“”} }, “survey_code”=>“test_survey”, “commit”=>“Next Section (Utensiles and you!) >>”, “authenticity_token”=>“8bee21081eea820ab1c658358c0baaa2e46de5d1”, “_method”=>“put”, “action”=>“update”, “controller”=>“app”, “response_set_code”=>“T2x8HhCQej”, “section”=>“2”.
- #response_for(question_id, answer_id, response_group = nil) ⇒ Object
-
#response_group_attributes=(response_attributes) ⇒ Object
method to process responses in response groups.
- #save_responses ⇒ Object
Methods included from TinyCode
Constructor Details
#initialize(*args) ⇒ ResponseSet
Instance methods
21 22 23 24 |
# File 'app/models/response_set.rb', line 21 def initialize(*args) super(*args) default_args end |
Instance Attribute Details
#current_section_id ⇒ Object
Returns the value of attribute current_section_id.
15 16 17 |
# File 'app/models/response_set.rb', line 15 def current_section_id @current_section_id end |
Instance Method Details
#complete! ⇒ Object
141 142 143 |
# File 'app/models/response_set.rb', line 141 def complete! self.completed_at = Time.now end |
#count_group_responses(group) ⇒ Object
Returns the number of response groups (count of group responses enterted) for this question group
165 166 167 168 169 170 171 172 |
# File 'app/models/response_set.rb', line 165 def count_group_responses(group) counts = [] group.questions.each do |question| counts << Response.count("response_group",:conditions => ["response_set_id =? AND question_id=? AND response_group IS NOT NULL", self.id, question.id], :distinct => true) end counts.max #since response groups can be partially filled, such that for one response group the user may have answered only one of the questions in the group. We want to still count the partially complete group response. end |
#count_question_responses(question) ⇒ Object
Counts the number of responses for the current user for this question
160 161 162 |
# File 'app/models/response_set.rb', line 160 def count_question_responses(question) Response.count(:conditions => ["response_set_id =? AND question_id=?", self.id, question.id]) end |
#default_args ⇒ Object
26 27 28 29 |
# File 'app/models/response_set.rb', line 26 def default_args self.started_at ||= Time.now self.access_code = ResponseSet.make_tiny_code end |
#empty? ⇒ Boolean
ResponseSet has an awareness of its internal state
137 138 139 |
# File 'app/models/response_set.rb', line 137 def empty? self.responses.empty? end |
#find_response(answer_id) ⇒ Object
ResponseSet knows if certain answers are contained in this response set This method acts as an interface for the dependency analysis
155 156 157 |
# File 'app/models/response_set.rb', line 155 def find_response(answer_id) self.responses.find_by_answer_id(answer_id) end |
#has_answered_question?(question) ⇒ Boolean
145 146 147 |
# File 'app/models/response_set.rb', line 145 def has_answered_question?(question) !has_not_answered_question?(question) end |
#has_not_answered_question?(question) ⇒ Boolean
149 150 151 |
# File 'app/models/response_set.rb', line 149 def has_not_answered_question?(question) self.responses.find_all_by_question_id(question.id).empty? end |
#response_attributes=(response_attributes) ⇒ Object
“responses”=>{ string “6”=>“20”=>{“string_value”=>“saf”}, text “7”=>“21”=>{“text_value”=>“”}, radio+txt “1”=>“answer_id”=>“1”, “4”=>{“string_value”=>“”}, radio “2”=>“answer_id”=>“6”, radio “3”=>“answer_id”=>“10”, check “4”=>“answer_id”=>“15”, check+txt “5”=>“16”=>{“selected”=>“1”, “19”=>“string_value”=>“”}
},
“survey_code”=>“test_survey”, “commit”=>“Next Section (Utensiles and you!) >>”, “authenticity_token”=>“8bee21081eea820ab1c658358c0baaa2e46de5d1”, “_method”=>“put”, “action”=>“update”, “controller”=>“app”, “response_set_code”=>“T2x8HhCQej”, “section”=>“2”
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'app/models/response_set.rb', line 54 def response_attributes=(response_attributes) response_attributes.each do |question_id, responses_hash| Response.delete_all(["response_set_id =? AND question_id =?", self.id, question_id]) if (answer_id = responses_hash[:answer_id]) if (!responses_hash[:answer_id].empty?) # Dropdowns return answer id but have an empty value if they are not set... ignoring those. #radio or dropdown - only one response responses.build({:question_id => question_id, :answer_id => answer_id}.merge(responses_hash[answer_id] || {})) end else #possibly multiples responses - unresponded radios end up here too # we use the variable question_id, not the "question_id" in the response_hash responses_hash.delete_if{|k,v| k == "question_id"}.each do |answer_id, response_hash| unless response_hash.delete_if{|k,v| v.blank?}.empty? responses.build({:question_id => question_id, :answer_id => answer_id}.merge(response_hash)) end end end end end |
#response_for(question_id, answer_id, response_group = nil) ⇒ Object
31 32 33 34 |
# File 'app/models/response_set.rb', line 31 def response_for(question_id, answer_id, response_group = nil) found = responses.find_by_question_id_and_answer_id_and_response_group(question_id, answer_id, response_group) found.blank? ? responses.new(:question_id => question_id, :answer_id => answer_id, :response_group => response_group) : found end |
#response_group_attributes=(response_attributes) ⇒ Object
method to process responses in response groups
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'app/models/response_set.rb', line 106 def response_group_attributes=(response_attributes) response_attributes.each do |question_id, responses_group_hash| Response.delete_all(["response_set_id =? AND question_id =?", self.id, question_id]) responses_group_hash.each do |response_group_number, group_hash| if (answer_id = group_hash[:answer_id]) # if group_hash has an answer_id key we treat it differently if (!group_hash[:answer_id].empty?) # dropdowns return empty values in answer_ids if they are not selected #radio or dropdown - only one response responses.build({:question_id => question_id, :answer_id => answer_id, :response_group => response_group_number}.merge(group_hash[answer_id] || {})) end else #possibly multiples responses - unresponded radios end up here too # we use the variable question_id in the key, not the "question_id" in the response_hash... same with response_group key group_hash.delete_if{|k,v| (k == "question_id") or (k == "response_group")}.each do |answer_id, inner_hash| unless inner_hash.delete_if{|k,v| v.blank?}.empty? responses.build({:question_id => question_id, :answer_id => answer_id, :response_group => response_group_number}.merge(inner_hash)) end end end end end end |
#save_responses ⇒ Object
130 131 132 133 134 |
# File 'app/models/response_set.rb', line 130 def save_responses responses.each do |response| response.save(false) end end |