Module: Surveyor::Models::AnswerMethods

Included in:
Answer
Defined in:
lib/surveyor/models/answer_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
# 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"

  # Mustache
  base.send :include, MustacheContext

  @@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, :input_mask, :input_mask_placeholder
end

Instance Method Details

#css_classObject



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

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

#default_argsObject



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

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

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



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

def default_value_for(context = nil, locale = nil)
  in_context(translation(locale)[:default_value], context)
end

#display_type=(val) ⇒ Object



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

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

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



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

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

#initialize(*args) ⇒ Object

Instance Methods



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

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

#split(text, position = nil) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/surveyor/models/answer_methods.rb', line 63

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



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

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



73
74
75
76
77
# File 'lib/surveyor/models/answer_methods.rb', line 73

def translation(locale)
  {:text => self.text, :help_text => self.help_text, :default_value => self.default_value}.with_indifferent_access.merge(
    (self.question.translation(locale)[:answers] || {})[self.reference_identifier] || {}
  )
end