Module: Fe::ChoiceFieldConcern

Extended by:
ActiveSupport::Concern
Included in:
ChoiceField
Defined in:
app/models/concerns/fe/choice_field_concern.rb

Instance Method Summary collapse

Instance Method Details

#choices(locale = nil) ⇒ Object

Returns choices stored one per line in content field



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/models/concerns/fe/choice_field_concern.rb', line 43

def choices(locale = nil)
  retVal = Array.new
  if ['yes-no', 'acceptance'].include?(self.style)
    return [[_('Yes'),1],[_('No'),0]]
  elsif source.present?
    begin
      doc = XML::Document.file(source)
      options = doc.find(text_xpath).collect { |n| n.content }
      values = doc.find(value_xpath).collect { |n| n.content }
      retVal = [options, values].transpose
    rescue NameError, LibXML::XML::Error
      doc = REXML::Document.new Net::HTTP.get_response(URI.parse(source)).body
      retVal = [ doc.elements.collect(text_xpath){|c|c.text}, doc.elements.collect(value_xpath){|c|c.text} ].transpose
    end
  elsif content.present?
    choices = content(locale)
    choices.split("\n").each do |opt|
      pair = opt.strip.split(";").reverse!
      pair[1] ||= pair[0]
      retVal << pair
    end
  end
  return retVal
end

#conditional_match(answer_sheet) ⇒ Object



147
148
149
150
# File 'app/models/concerns/fe/choice_field_concern.rb', line 147

def conditional_match(answer_sheet)
  has_answer?(conditional_answers, answer_sheet) || 
    (responses(answer_sheet).empty? && conditional_answers.empty?)
end

#default_label?Boolean

element view provides the element label?

Returns:

  • (Boolean)


104
105
106
107
108
109
110
# File 'app/models/concerns/fe/choice_field_concern.rb', line 104

def default_label?
  if self.style == 'acceptance' || self.hide_option_labels?
    false   # template provides its own label
  else
    true
  end
end

#display_response(app = nil) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'app/models/concerns/fe/choice_field_concern.rb', line 129

def display_response(app = nil)
  r = responses(app)
  if r.blank?
    ""
  elsif self.style == 'yes-no'
    ans = r.first
    if ans.class == Answer
      is_true(ans.value) ? "Yes" : "No"
    else
      is_true(ans) ? "Yes" : "No"
    end
  elsif self.style == 'acceptance'
    "Accepted"  # if not blank, it's accepted
  else
    r.compact.join(', ')
  end
end

#has_answer?(choice, answer_sheet) ⇒ Boolean

choices can be an array, in which case any match returns true

Returns:

  • (Boolean)


69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/models/concerns/fe/choice_field_concern.rb', line 69

def has_answer?(choice, answer_sheet)
  if choice.is_a?(Array)
    return choice.any? { |c| 
      has_answer?(c, answer_sheet)
    }
  end

  responses(answer_sheet).any? { |r|
    is_true(r) && is_true(choice) ||
    is_false(r) && is_false(choice) ||
    r.to_s.strip == choice.to_s.strip
  }
end

#is_response_false(answer_sheet) ⇒ Object



152
153
154
# File 'app/models/concerns/fe/choice_field_concern.rb', line 152

def is_response_false(answer_sheet)
  is_false(display_response(answer_sheet))
end

#ptemplateObject

which view to render this element?



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/models/concerns/fe/choice_field_concern.rb', line 84

def ptemplate
  # TODO case would be nicer
  if self.style == 'checkbox'
    'fe/checkbox_field'
  elsif self.style == 'drop-down'
    'fe/drop_down_field'
  elsif self.style == 'radio'
    'fe/radio_button_field'
  elsif self.style == 'yes-no'
    'fe/yes_no'
  elsif self.style == 'rating'
    'fe/rating'
  elsif self.style == 'acceptance'
    'fe/acceptance'
  elsif self.style == 'country'
    'fe/country'
  end
end

#rating_after_label(locale = nil) ⇒ Object



30
31
32
33
34
# File 'app/models/concerns/fe/choice_field_concern.rb', line 30

def rating_after_label(locale = nil)
  rating_after_label_translations && 
    rating_after_label_translations[locale].present? ?
    rating_after_label_translations[locale] : self[:rating_after_label]
end

#rating_before_label(locale = nil) ⇒ Object



24
25
26
27
28
# File 'app/models/concerns/fe/choice_field_concern.rb', line 24

def rating_before_label(locale = nil)
  rating_before_label_translations &&
    rating_before_label_translations[locale].present? ?
    rating_before_label_translations[locale] : self[:rating_before_label]
end

#rating_na_label(locale = nil) ⇒ Object



36
37
38
39
40
# File 'app/models/concerns/fe/choice_field_concern.rb', line 36

def rating_na_label(locale = nil)
  rating_na_label_translations && 
    rating_na_label_translations[locale].present? ?
    rating_na_label_translations[locale] : self[:rating_na_label]
end

#validation_class(answer_sheet, page = nil) ⇒ Object

css class names for javascript-based validation



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'app/models/concerns/fe/choice_field_concern.rb', line 113

def validation_class(answer_sheet, page = nil)
  if self.required?(answer_sheet, page)
    if self.style == 'drop-down'
      'validate-selection required'
    elsif self.style == 'rating'
      'validate-rating required'
    elsif self.style == 'acceptance'
      'required'
    else
      'validate-one-required required'
    end
  else
    ''
  end
end