lib/surveyor/models/response_methods.rb in surveyor-0.22.0 vs lib/surveyor/models/response_methods.rb in surveyor-1.0.0

- old
+ new

@@ -17,10 +17,13 @@ @@validations_already_included = true end base.send :include, Surveyor::ActsAsResponse # includes "as" instance method + # Whitelisting attributes + base.send :attr_accessible, :response_set, :question, :answer, :date_value, :time_value, :response_set_id, :question_id, :answer_id, :datetime_value, :integer_value, :float_value, :unit, :text_value, :string_value, :response_other, :response_group, :survey_section_id + # Class methods base.instance_eval do def applicable_attributes(attrs) result = HashWithIndifferentAccess.new(attrs) answer_id = result[:answer_id].is_a?(Array) ? result[:answer_id].last : result[:answer_id] # checkboxes are arrays / radio buttons are not arrays @@ -82,9 +85,22 @@ if self.answer.response_class == "answer" and self.answer_id return self.answer.text else return "#{(self.string_value || self.text_value || self.integer_value || self.float_value || nil).to_s}" end + end + + def json_value + return nil if answer.response_class == "answer" + + formats = { + 'datetime' => '%Y-%m-%dT%H:%M%:z', + 'date' => '%Y-%m-%d', + 'time' => '%H:%M' + } + + found = formats[answer.response_class] + found ? datetime_value.strftime(found) : as(answer.response_class) end end end end