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
35
36
37
# 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
  
  # Scopes
  base.send :default_scope, :order => "created_at ASC"
  
  @@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



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

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)


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

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



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

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

#date_valueObject



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

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

#date_value=(val) ⇒ Object



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

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

#datetime_formatObject



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

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

#default_argsObject



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

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

#initialize(*args) ⇒ Object

Instance Methods



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

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

#json_valueObject



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/surveyor/models/response_methods.rb', line 92

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

#time_formatObject



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

def time_format
  '%H:%M'
end

#time_valueObject



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

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

#time_value=(val) ⇒ Object



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

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

#to_sObject

used in dependency_explanation_helper



84
85
86
87
88
89
90
# File 'lib/surveyor/models/response_methods.rb', line 84

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