Module: Surveyor::Models::QuestionMethods

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RenderText

#render_answer_text, #render_help_text, #render_question_text, #render_text

Class Method Details

.included(base) ⇒ Object



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
# 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 :belongs_to, :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

  # Whitelisting attributes
  base.send :attr_accessible, :survey_section, :question_group, :survey_section_id, :question_group_id, :text, :short_text, :help_text, :pick, :reference_identifier, :data_export_identifier, :common_namespace, :common_identifier, :display_order, :display_type, :is_mandatory, :display_width, :custom_class, :custom_renderer, :correct_answer_id
end

Instance Method Details

#css_class(response_set) ⇒ Object



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

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

#default_argsObject



41
42
43
44
45
46
47
48
# File 'lib/surveyor/models/question_methods.rb', line 41

def default_args
  self.is_mandatory ||= true
  self.display_type ||= "default"
  self.pick ||= "none"
  self.data_export_identifier ||= Surveyor::Common.normalize(text)
  self.short_text ||= text
  self.api_id ||= Surveyor::Common.generate_api_id
end

#dependent?Boolean

Returns:

  • (Boolean)


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

def dependent?
  self.dependency != nil
end

#display_type=(val) ⇒ Object



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

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

#initialize(*args) ⇒ Object

Instance Methods



36
37
38
39
# File 'lib/surveyor/models/question_methods.rb', line 36

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

#mandatory?Boolean

Returns:

  • (Boolean)


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

def mandatory?
  self.is_mandatory == true
end

#part_of_group?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/surveyor/models/question_methods.rb', line 71

def part_of_group?
  !self.question_group.nil?
end

#pick=(val) ⇒ Object



50
51
52
# File 'lib/surveyor/models/question_methods.rb', line 50

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

#renderer(g = question_group) ⇒ Object



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

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

#solo?Boolean

Returns:

  • (Boolean)


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

def solo?
  self.question_group.nil?
end

#split_text(part = nil) ⇒ Object



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

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

#triggered?(response_set) ⇒ Boolean

Returns:

  • (Boolean)


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

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