Module: Surveyor::Models::QuestionMethods

Included in:
Question
Defined in:
lib/surveyor/models/question_methods.rb

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) included(base)



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/surveyor/models/question_methods.rb', line 6

def self.included(base)
  # Associations
  base.send :belongs_to, :survey_section
  base.send :belongs_to, :question_group, :dependent => :destroy
  base.send :has_many, :answers, :order => "display_order ASC", :dependent => :destroy # it might not always have answers
  base.send :has_one, :dependency, :dependent => :destroy
  base.send :has_one, :correct_answer, :class_name => "Answer", :dependent => :destroy

  # Scopes
  base.send :default_scope, :order => "display_order ASC"
  
  @@validations_already_included ||= nil
  unless @@validations_already_included
    # Validations
    base.send :validates_presence_of, :text, :display_order
    # this causes issues with building and saving
    #, :survey_section_id
    base.send :validates_inclusion_of, :is_mandatory, :in => [true, false]
    
    @@validations_already_included = true
  end
end

Instance Method Details

- (Object) css_class(response_set)



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

def css_class(response_set)
  [(dependent? ? "q_dependent" : nil), (triggered?(response_set) ? nil : "q_hidden"), custom_class].compact.join(" ")
end

- (Object) default_args



35
36
37
38
39
40
41
42
43
# File 'lib/surveyor/models/question_methods.rb', line 35

def default_args
  self.is_mandatory ||= true
  self.display_type ||= "default"
  self.pick ||= "none"
  self.display_order ||= self.survey_section ? self.survey_section.questions.count : 0
  self.data_export_identifier ||= Surveyor::Common.normalize(text)
  self.short_text ||= text
  self.api_id ||= UUID.generate
end

- (Boolean) dependent?



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

def dependent?
  self.dependency != nil
end

- (Object) display_type=(val)



48
49
50
# File 'lib/surveyor/models/question_methods.rb', line 48

def display_type=(val)
  write_attribute(:display_type, val.nil? ? nil : val.to_s)
end

- (QuestionMethods) initialize(*args)

Instance Methods



30
31
32
33
# File 'lib/surveyor/models/question_methods.rb', line 30

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

- (Boolean) mandatory?



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

def mandatory?
  self.is_mandatory == true
end

- (Boolean) part_of_group?



66
67
68
# File 'lib/surveyor/models/question_methods.rb', line 66

def part_of_group?
  !self.question_group.nil?
end

- (Object) pick=(val)



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

def pick=(val)
  write_attribute(:pick, val.nil? ? nil : val.to_s)
end

- (Object) renderer(g = question_group)



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

def renderer(g = question_group)
  r = [g ? g.renderer.to_s : nil, display_type].compact.join("_")
  r.blank? ? :default : r.to_sym
end

- (Boolean) solo?



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

def solo?
  self.question_group.nil?
end

- (Object) split_text(part = nil)



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

def split_text(part = nil)
  (part == :pre ? text.split("|",2)[0] : (part == :post ? text.split("|",2)[1] : text)).to_s
end

- (Boolean) triggered?(response_set)



59
60
61
# File 'lib/surveyor/models/question_methods.rb', line 59

def triggered?(response_set)
  dependent? ? self.dependency.is_met?(response_set) : true
end