Module: Fe::ChoiceFieldConcern

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

Instance Method Summary collapse

Instance Method Details

#choices(locale = nil) ⇒ Object

Returns choices stored one per line in content field



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/models/fe/concerns/choice_field_concern.rb', line 22

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_translations[locale] || content
    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



127
128
129
130
131
132
# File 'app/models/fe/concerns/choice_field_concern.rb', line 127

def conditional_match(answer_sheet)
  displayed_response = display_response(answer_sheet)
  (is_true(displayed_response) && is_true(conditional_answer)) ||
    (is_response_false(answer_sheet) && is_false(conditional_answer)) ||
    (displayed_response == conditional_answer)
end

#default_label?Boolean

element view provides the element label?

Returns:

  • (Boolean)


83
84
85
86
87
88
89
# File 'app/models/fe/concerns/choice_field_concern.rb', line 83

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



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'app/models/fe/concerns/choice_field_concern.rb', line 108

def display_response(app=nil)
  r = responses(app)
  r.reject! {|a| a.class == Answer && a.value.blank?}
  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, app) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/models/fe/concerns/choice_field_concern.rb', line 47

def has_answer?(choice, app)
  responses(app).each do |r|   # loop through Answers
                               # legacy field type choices may be int or tinyint
                               # raise r.inspect + ' - ' + choice.inspect if id == 1137 && r != 1
                               # r = r.to_s
    return true if  case true
                      when is_true(r) then is_true(choice)
                      when is_false(r) then is_false(choice)
                      else
                        r.to_s == choice.to_s
                    end
  end
  false
end

#is_response_false(answer_sheet) ⇒ Object



134
135
136
# File 'app/models/fe/concerns/choice_field_concern.rb', line 134

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

#ptemplateObject

which view to render this element?



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/models/fe/concerns/choice_field_concern.rb', line 63

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

#validation_class(answer_sheet) ⇒ Object

css class names for javascript-based validation



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/models/fe/concerns/choice_field_concern.rb', line 92

def validation_class(answer_sheet)
  if self.required?(answer_sheet)
    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