Module: Surveyor::Models::AnswerMethods

Includes:
RenderText
Included in:
Answer
Defined in:
lib/surveyor/models/answer_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
# File 'lib/surveyor/models/answer_methods.rb', line 6

def self.included(base)
  # Associations
  base.send :belongs_to, :question
  base.send :has_many, :responses
  base.send :has_many, :validations, :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
    # this causes issues with building and saving
    # base.send :validates_numericality_of, :question_id, :allow_nil => false, :only_integer => true
    @@validations_already_included = true
  end
  
  # Whitelisting attributes
  base.send :attr_accessible, :question, :question_id, :text, :short_text, :help_text, :weight, :response_class, :reference_identifier, :data_export_identifier, :common_namespace, :common_identifier, :display_order, :is_exclusive, :display_length, :custom_class, :custom_renderer, :default_value, :display_type
end

Instance Method Details

#css_classObject



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

def css_class
  [(is_exclusive ? "exclusive" : nil), custom_class].compact.join(" ")
end

#default_argsObject



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

def default_args
  self.is_exclusive ||= false
  self.display_type ||= "default"
  self.response_class ||= "answer"
  self.short_text ||= text
  self.data_export_identifier ||= Surveyor::Common.normalize(text)
  self.api_id ||= Surveyor::Common.generate_api_id
end

#initialize(*args) ⇒ Object

Instance Methods



31
32
33
34
# File 'lib/surveyor/models/answer_methods.rb', line 31

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

#split_or_hidden_text(part = nil, context = nil) ⇒ Object



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

def split_or_hidden_text(part = nil, context = nil)
  return "" if display_type == "hidden_label"
  part == :pre ? self.render_answer_text(text.split("|",2)[0], context) : (part == :post ? self.render_answer_text(text.split("|",2)[1], context) : self.render_answer_text(text, context))
end