Module: Surveyor::Models::ResponseMethods

Included in:
Response
Defined in:
lib/surveyor/models/response_methods.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/surveyor/models/response_methods.rb', line 4

def self.included(base)
  # Associations
  base.send :belongs_to, :response_set
  base.send :belongs_to, :question
  base.send :belongs_to, :answer

  @@validations_already_included ||= nil
  unless @@validations_already_included
    # Validations
    base.send :validates_presence_of, :question_id, :answer_id

    @@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
      if result[:string_value] && !answer_id.blank? && Answer.exists?(answer_id)
        answer = Answer.find(answer_id)
        result.delete(:string_value) unless answer.response_class && answer.response_class.to_sym == :string
      end
      result
    end
  end
end

Instance Method Details

#answer_id=(val) ⇒ Object



46
47
48
# File 'lib/surveyor/models/response_methods.rb', line 46

def answer_id=(val)
  write_attribute :answer_id, (val.is_a?(Array) ? val.detect{|x| !x.to_s.blank?} : val)
end

#correct?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/surveyor/models/response_methods.rb', line 49

def correct?
  question.correct_answer.nil? or self.answer.response_class != "answer" or (question.correct_answer.id.to_i == answer.id.to_i)
end

#date_formatObject



73
74
75
# File 'lib/surveyor/models/response_methods.rb', line 73

def date_format
  '%Y-%m-%d'
end

#date_valueObject



61
62
63
# File 'lib/surveyor/models/response_methods.rb', line 61

def date_value
  read_attribute(:datetime_value).strftime( date_format ) unless read_attribute(:datetime_value).blank?
end

#date_value=(val) ⇒ Object



65
66
67
# File 'lib/surveyor/models/response_methods.rb', line 65

def date_value=(val)
  self.datetime_value = Time.zone.parse(val) ? Time.zone.parse(val).to_datetime : nil
end

#datetime_formatObject



77
78
79
# File 'lib/surveyor/models/response_methods.rb', line 77

def datetime_format
  '%Y-%m-%d %H:%M:%S'
end

#default_argsObject



42
43
44
# File 'lib/surveyor/models/response_methods.rb', line 42

def default_args
  self.api_id ||= Surveyor::Common.generate_api_id
end

#initialize(*args) ⇒ Object

Instance Methods



37
38
39
40
# File 'lib/surveyor/models/response_methods.rb', line 37

def initialize(*args)
  super(*args)
  default_args
end

#json_valueObject



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/surveyor/models/response_methods.rb', line 105

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.try{|d| d.utc.strftime(found)} : as(answer.response_class)
end

#time_formatObject



69
70
71
# File 'lib/surveyor/models/response_methods.rb', line 69

def time_format
  '%H:%M'
end

#time_valueObject



53
54
55
# File 'lib/surveyor/models/response_methods.rb', line 53

def time_value
  read_attribute(:datetime_value).strftime( time_format ) unless read_attribute(:datetime_value).blank?
end

#time_value=(val) ⇒ Object



57
58
59
# File 'lib/surveyor/models/response_methods.rb', line 57

def time_value=(val)
  self.datetime_value = Time.zone.parse("#{Date.today.to_s} #{val}") ? Time.zone.parse("#{Date.today.to_s} #{val}").to_datetime : nil
end

#to_formatted_sObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/surveyor/models/response_methods.rb', line 81

def to_formatted_s
  return "" if answer.nil? || answer.response_class.nil?
  return case t = answer.response_class.to_sym
         when :string, :text, :integer, :float
           send("#{t}_value".to_sym).to_s
         when :date
           date_value
         when :time
           time_value
         when :datetime
           read_attribute(:datetime_value).strftime(datetime_format)
         else
           to_s
         end
end

#to_sObject

used in dependency_explanation_helper



97
98
99
100
101
102
103
# File 'lib/surveyor/models/response_methods.rb', line 97

def to_s # used in dependency_explanation_helper
  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