Module: Surveyor::Models::ResponseMethods

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::ForbiddenAttributesProtection, ActiveModel::Validations, ActsAsResponse
Included in:
Response
Defined in:
lib/surveyor/models/response_methods.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Methods included from ActsAsResponse

#as

Instance Method Details

#answer_id=(val) ⇒ Object



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

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)


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

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



79
80
81
# File 'lib/surveyor/models/response_methods.rb', line 79

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

#date_valueObject



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

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

#date_value=(val) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/surveyor/models/response_methods.rb', line 66

def date_value=(val)
  self.datetime_value =
    if val && time = Time.zone.parse(val)
      time.to_datetime
    else
      nil
    end
end

#datetime_formatObject



83
84
85
# File 'lib/surveyor/models/response_methods.rb', line 83

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

#default_argsObject



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

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

#initialize(*args) ⇒ Object

Instance Methods



33
34
35
36
# File 'lib/surveyor/models/response_methods.rb', line 33

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

#json_valueObject



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/surveyor/models/response_methods.rb', line 111

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



75
76
77
# File 'lib/surveyor/models/response_methods.rb', line 75

def time_format
  '%H:%M'
end

#time_valueObject



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

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

#time_value=(val) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/surveyor/models/response_methods.rb', line 53

def time_value=(val)
  self.datetime_value =
    if val && time = Time.zone.parse("#{Date.today.to_s} #{val}")
      time.to_datetime
    else
      nil
    end
end

#to_formatted_sObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/surveyor/models/response_methods.rb', line 87

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 ) unless read_attribute(:datetime_value).blank?) || ''
         else
           to_s
         end
end

#to_sObject

used in dependency_explanation_helper



103
104
105
106
107
108
109
# File 'lib/surveyor/models/response_methods.rb', line 103

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