Module: Fe::ChoiceFieldConcern
- Extended by:
- ActiveSupport::Concern
- Included in:
- ChoiceField
- Defined in:
- app/models/concerns/fe/choice_field_concern.rb
Instance Method Summary collapse
-
#choices(locale = nil) ⇒ Object
Returns choices stored one per line in content field.
- #conditional_match(answer_sheet) ⇒ Object
-
#default_label? ⇒ Boolean
element view provides the element label?.
- #display_response(app = nil) ⇒ Object
-
#has_answer?(choice, answer_sheet) ⇒ Boolean
choices can be an array, in which case any match returns true.
- #is_response_false(answer_sheet) ⇒ Object
-
#ptemplate ⇒ Object
which view to render this element?.
- #rating_after_label(locale = nil) ⇒ Object
- #rating_before_label(locale = nil) ⇒ Object
- #rating_na_label(locale = nil) ⇒ Object
-
#validation_class(answer_sheet, page = nil) ⇒ Object
css class names for javascript-based validation.
Instance Method Details
#choices(locale = nil) ⇒ Object
Returns choices stored one per line in content field
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'app/models/concerns/fe/choice_field_concern.rb', line 47 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) = doc.find(text_xpath).collect { |n| n.content } values = doc.find(value_xpath).collect { |n| n.content } retVal = [, values].transpose rescue NameError, LibXML::XML::Error url = URI.parse(source) url.query = [url.query, "locale=#{locale}"].compact.join('&') if locale.present? doc = REXML::Document.new Net::HTTP.get_response(url).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
153 154 155 156 |
# File 'app/models/concerns/fe/choice_field_concern.rb', line 153 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?
110 111 112 113 114 115 116 |
# File 'app/models/concerns/fe/choice_field_concern.rb', line 110 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
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'app/models/concerns/fe/choice_field_concern.rb', line 135 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
75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'app/models/concerns/fe/choice_field_concern.rb', line 75 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
158 159 160 |
# File 'app/models/concerns/fe/choice_field_concern.rb', line 158 def is_response_false(answer_sheet) is_false(display_response(answer_sheet)) end |
#ptemplate ⇒ Object
which view to render this element?
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'app/models/concerns/fe/choice_field_concern.rb', line 90 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
34 35 36 37 38 |
# File 'app/models/concerns/fe/choice_field_concern.rb', line 34 def (locale = nil) && [locale].present? ? [locale] : self[:rating_after_label] end |
#rating_before_label(locale = nil) ⇒ Object
28 29 30 31 32 |
# File 'app/models/concerns/fe/choice_field_concern.rb', line 28 def (locale = nil) && [locale].present? ? [locale] : self[:rating_before_label] end |
#rating_na_label(locale = nil) ⇒ Object
40 41 42 43 44 |
# File 'app/models/concerns/fe/choice_field_concern.rb', line 40 def (locale = nil) && [locale].present? ? [locale] : self[:rating_na_label] end |
#validation_class(answer_sheet, page = nil) ⇒ Object
css class names for javascript-based validation
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'app/models/concerns/fe/choice_field_concern.rb', line 119 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 |