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

.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
32
33
34
# 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"

  # Mustache
  base.send :include, MustacheContext

  @@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



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

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

#default_argsObject



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

def default_args
  self.is_mandatory ||= false
  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)


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

def dependent?
  self.dependency != nil
end

#display_type=(val) ⇒ Object



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

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

#help_text_for(context = nil, locale = nil) ⇒ Object



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

def help_text_for(context = nil, locale = nil)
  in_context(translation(locale)[:help_text], context)
end

#initialize(*args) ⇒ Object

Instance Methods



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

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

#mandatory?Boolean

Returns:

  • (Boolean)


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

def mandatory?
  self.is_mandatory == true
end

#part_of_group?Boolean

Returns:

  • (Boolean)


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

def part_of_group?
  !self.question_group.nil?
end

#pick=(val) ⇒ Object



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

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

#renderer(g = question_group) ⇒ Object



96
97
98
99
# File 'lib/surveyor/models/question_methods.rb', line 96

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)


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

def solo?
  self.question_group.nil?
end

#split(text, position = nil) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/surveyor/models/question_methods.rb', line 86

def split(text, position=nil)
  case position
  when :pre
    text.split("|",2)[0]
  when :post
    text.split("|",2)[1]
  else
    text
  end.to_s
end

#text_for(position = nil, context = nil, locale = nil) ⇒ Object



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

def text_for(position = nil, context = nil, locale = nil)
  return "" if display_type == "hidden_label"
  imaged(split(in_context(translation(locale)[:text], context), position))
end

#translation(locale) ⇒ Object



100
101
102
103
104
# File 'lib/surveyor/models/question_methods.rb', line 100

def translation(locale)
  {:text => self.text, :help_text => self.help_text}.with_indifferent_access.merge(
    (self.survey_section.survey.translation(locale)[:questions] || {})[self.reference_identifier] || {}
  )
end

#triggered?(response_set) ⇒ Boolean

Returns:

  • (Boolean)


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

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