Class: Quby::Questionnaires::Entities::Question
- Defined in:
- lib/quby/questionnaires/entities/question.rb
Direct Known Subclasses
Quby::Questionnaires::Entities::Questions::CheckboxQuestion, Quby::Questionnaires::Entities::Questions::DateQuestion, Quby::Questionnaires::Entities::Questions::DeprecatedQuestion, Quby::Questionnaires::Entities::Questions::FloatQuestion, Quby::Questionnaires::Entities::Questions::IntegerQuestion, Quby::Questionnaires::Entities::Questions::RadioQuestion, Quby::Questionnaires::Entities::Questions::SelectQuestion, Quby::Questionnaires::Entities::Questions::StringQuestion, Quby::Questionnaires::Entities::Questions::TextQuestion
Constant Summary collapse
- MARKDOWN_ATTRIBUTES =
%w(description title).freeze
Instance Attribute Summary collapse
-
#allow_blank_titles ⇒ Object
Question validation fails when there are no title and no context_free_title.
-
#allow_duplicate_option_values ⇒ Object
readonly
Whether to skip the uniqueness validation on radio and select option values.
-
#as ⇒ Object
How should we display this question.
-
#autocomplete ⇒ Object
Whether the browser should autocomplete this question (off by default).
-
#col_span ⇒ Object
In case of being displayed inside a table, amount of columns/rows to span.
-
#cols ⇒ Object
Returns the value of attribute cols.
-
#context_free_title ⇒ Object
rubocop:enable AccessorMethodName.
-
#default_invisible ⇒ Object
Returns the value of attribute default_invisible.
-
#default_position ⇒ Object
Slider only: where to place the sliding thing by default Can have value :hidden for a hidden handle.
-
#dependencies ⇒ Object
Returns the value of attribute dependencies.
-
#depends_on ⇒ Object
This question should not validate itself unless the depends_on question is answered.
-
#description ⇒ Object
Returns the value of attribute description.
-
#deselectable ⇒ Object
Whether this radio question is deselectable.
-
#disallow_bulk ⇒ Object
Whether we can collapse this in bulk view.
-
#display_modes ⇒ Object
In what modes do we display this question NOTE We always display questions in print-view (if they have an answer).
-
#extra_data ⇒ Object
Extra data hash to store on the question item’s html element.
-
#group_maximum_answered ⇒ Object
Returns the value of attribute group_maximum_answered.
-
#group_minimum_answered ⇒ Object
Returns the value of attribute group_minimum_answered.
-
#hidden ⇒ Object
To hide old questions.
-
#input_data ⇒ Object
data-attributes for the input tag.
-
#key ⇒ Object
Standard attributes.
-
#labels ⇒ Object
Returns the value of attribute labels.
-
#lines ⇒ Object
Amount of rows and cols a textarea has.
-
#maximum ⇒ Object
Returns the value of attribute maximum.
-
#minimum ⇒ Object
Minimum and maximum values for float and integer types.
-
#options ⇒ Object
Multiple-choice questions have options to choose from.
-
#parent ⇒ Object
Some questions are a tree.
-
#parent_option_key ⇒ Object
Returns the value of attribute parent_option_key.
-
#question_group ⇒ Object
options for grouping questions and setting a minimum or maximum number of answered questions in the group.
-
#questionnaire ⇒ Object
Returns the value of attribute questionnaire.
-
#row_span ⇒ Object
Returns the value of attribute row_span.
-
#sbg_key ⇒ Object
Returns the value of attribute sbg_key.
-
#score_header ⇒ Object
Whether we use the :description, the :value or :none for the score header above this question.
-
#sets_textvar ⇒ Object
Text variable name that will be replaced with the answer to this question In all following text elements that support markdown.
-
#show_values ⇒ Object
Whether we show the value for each option :all => in all questionnaire display modes :none => in none of display modes :paged => for only in :paged display mode :bulk => for only in :bulk display mode.
-
#size ⇒ Object
To specify size of string/number input boxes.
-
#table ⇒ Object
Table this question might belong to.
-
#title ⇒ Object
Returns the value of attribute title.
-
#type ⇒ Object
What kind of question is this?.
-
#unit ⇒ Object
To display unit for number items.
-
#validations ⇒ Object
Structuring.
Attributes inherited from Item
#presentation, #raw_content, #switch_cycle
Instance Method Summary collapse
-
#answer_keys ⇒ Object
Returns all possible answer keys of this question (excluding subquestions, including options).
- #as_json(options = {}) ⇒ Object
-
#claimed_keys ⇒ Object
The keys this question claims as his own.
- #codebook_key(key, questionnaire, opts = {}) ⇒ Object
- #codebook_output_range ⇒ Object
- #codebook_output_type ⇒ Object
- #expand_depends_on_input_keys ⇒ Object
- #hidden? ⇒ Boolean
- #html_id ⇒ Object
-
#initialize(key, options = {}) ⇒ Question
constructor
rubocop:disable CyclomaticComplexity, Metrics/MethodLength.
-
#input_keys ⇒ Object
Returns all keys belonging to html inputs generated by this question.
- #key_in_use?(k) ⇒ Boolean
-
#set_depends_on(keys) ⇒ Object
rubocop:disable AccessorMethodName.
- #show_values_in_mode?(mode) ⇒ Boolean
- #subquestion? ⇒ Boolean
- #subquestions ⇒ Object
- #to_codebook(questionnaire, opts = {}) ⇒ Object
- #variable_descriptions ⇒ Object
- #view_selector ⇒ Object
Constructor Details
#initialize(key, options = {}) ⇒ Question
rubocop:disable CyclomaticComplexity, Metrics/MethodLength
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/quby/questionnaires/entities/question.rb', line 126 def initialize(key, = {}) super() @extra_data ||= {} @options = [] @allow_duplicate_option_values = [:allow_duplicate_option_values] @questionnaire = [:questionnaire] @key = key @sbg_key = [:sbg_key] @type = [:type] @as = [:as] @title = [:title] @context_free_title = [:context_free_title] @allow_blank_titles = [:allow_blank_titles] @description = [:description] @display_modes = [:display_modes] @presentation = [:presentation] @validations = [] @parent = [:parent] @hidden = [:hidden] @table = [:table] @parent_option_key = [:parent_option_key] @autocomplete = [:autocomplete] || "off" @show_values = [:show_values] || :bulk @deselectable = ([:deselectable].nil? || [:deselectable]) @disallow_bulk = [:disallow_bulk] @score_header = [:score_header] || :none @sets_textvar = "#{questionnaire.key}_#{[:sets_textvar]}" if [:sets_textvar] @unit = [:unit] @lines = [:lines] || 6 @cols = [:cols] || 40 @default_invisible = [:default_invisible] || false @labels ||= [] @col_span = [:col_span] || 1 @row_span = [:row_span] || 1 set_depends_on([:depends_on]) @question_group = [:question_group] @group_minimum_answered = [:group_minimum_answered] @group_maximum_answered = [:group_maximum_answered] @input_data = {} @input_data[:value_tooltip] = true if [:value_tooltip] # Require subquestions of required questions by default [:required] = true if @parent&.validations&.first&.fetch(:type, nil) == :requires_answer @validations << {type: :requires_answer, explanation: [:error_explanation]} if [:required] if @type == :float @validations << {type: :valid_float, explanation: [:error_explanation]} elsif @type == :integer @validations << {type: :valid_integer, explanation: [:error_explanation]} end if [:minimum] and (@type == :integer || @type == :float) fail "deprecated" # pretty sure this is not used anywhere end if [:maximum] and (@type == :integer || @type == :float) fail "deprecated" # pretty sure this is not used anywhere end @default_position = [:default_position] if @question_group if @group_minimum_answered @validations << {type: :answer_group_minimum, group: @question_group, value: @group_minimum_answered, explanation: [:error_explanation]} end if @group_maximum_answered @validations << {type: :answer_group_maximum, group: @question_group, value: @group_maximum_answered, explanation: [:error_explanation]} end end end |
Instance Attribute Details
#allow_blank_titles ⇒ Object
Question validation fails when there are no title and no context_free_title. When :allow_blank_titles => true passed, validation does not fail. Any other value will raise the failure.
45 46 47 |
# File 'lib/quby/questionnaires/entities/question.rb', line 45 def allow_blank_titles @allow_blank_titles end |
#allow_duplicate_option_values ⇒ Object (readonly)
Whether to skip the uniqueness validation on radio and select option values
34 35 36 |
# File 'lib/quby/questionnaires/entities/question.rb', line 34 def allow_duplicate_option_values @allow_duplicate_option_values end |
#as ⇒ Object
How should we display this question
28 29 30 |
# File 'lib/quby/questionnaires/entities/question.rb', line 28 def as @as end |
#autocomplete ⇒ Object
Whether the browser should autocomplete this question (off by default)
52 53 54 |
# File 'lib/quby/questionnaires/entities/question.rb', line 52 def autocomplete @autocomplete end |
#col_span ⇒ Object
In case of being displayed inside a table, amount of columns/rows to span
114 115 116 |
# File 'lib/quby/questionnaires/entities/question.rb', line 114 def col_span @col_span end |
#cols ⇒ Object
Returns the value of attribute cols.
108 109 110 |
# File 'lib/quby/questionnaires/entities/question.rb', line 108 def cols @cols end |
#context_free_title ⇒ Object
rubocop:enable AccessorMethodName
211 212 213 |
# File 'lib/quby/questionnaires/entities/question.rb', line 211 def context_free_title @context_free_title end |
#default_invisible ⇒ Object
Returns the value of attribute default_invisible.
117 118 119 |
# File 'lib/quby/questionnaires/entities/question.rb', line 117 def default_invisible @default_invisible end |
#default_position ⇒ Object
Slider only: where to place the sliding thing by default Can have value :hidden for a hidden handle.
121 122 123 |
# File 'lib/quby/questionnaires/entities/question.rb', line 121 def default_position @default_position end |
#dependencies ⇒ Object
Returns the value of attribute dependencies.
66 67 68 |
# File 'lib/quby/questionnaires/entities/question.rb', line 66 def dependencies @dependencies end |
#depends_on ⇒ Object
This question should not validate itself unless the depends_on question is answered. May also be an array of “#question_key_#option_key” strings that specify options this question depends on.
86 87 88 |
# File 'lib/quby/questionnaires/entities/question.rb', line 86 def depends_on @depends_on end |
#description ⇒ Object
Returns the value of attribute description.
20 21 22 |
# File 'lib/quby/questionnaires/entities/question.rb', line 20 def description @description end |
#deselectable ⇒ Object
Whether this radio question is deselectable
74 75 76 |
# File 'lib/quby/questionnaires/entities/question.rb', line 74 def deselectable @deselectable end |
#disallow_bulk ⇒ Object
Whether we can collapse this in bulk view
81 82 83 |
# File 'lib/quby/questionnaires/entities/question.rb', line 81 def disallow_bulk @disallow_bulk end |
#display_modes ⇒ Object
In what modes do we display this question NOTE We always display questions in print-view (if they have an answer)
38 39 40 |
# File 'lib/quby/questionnaires/entities/question.rb', line 38 def display_modes @display_modes end |
#extra_data ⇒ Object
Extra data hash to store on the question item’s html element
89 90 91 |
# File 'lib/quby/questionnaires/entities/question.rb', line 89 def extra_data @extra_data end |
#group_maximum_answered ⇒ Object
Returns the value of attribute group_maximum_answered.
100 101 102 |
# File 'lib/quby/questionnaires/entities/question.rb', line 100 def group_maximum_answered @group_maximum_answered end |
#group_minimum_answered ⇒ Object
Returns the value of attribute group_minimum_answered.
99 100 101 |
# File 'lib/quby/questionnaires/entities/question.rb', line 99 def group_minimum_answered @group_minimum_answered end |
#hidden ⇒ Object
To hide old questions
31 32 33 |
# File 'lib/quby/questionnaires/entities/question.rb', line 31 def hidden @hidden end |
#input_data ⇒ Object
data-attributes for the input tag.
92 93 94 |
# File 'lib/quby/questionnaires/entities/question.rb', line 92 def input_data @input_data end |
#key ⇒ Object
Standard attributes
14 15 16 |
# File 'lib/quby/questionnaires/entities/question.rb', line 14 def key @key end |
#labels ⇒ Object
Returns the value of attribute labels.
22 23 24 |
# File 'lib/quby/questionnaires/entities/question.rb', line 22 def labels @labels end |
#lines ⇒ Object
Amount of rows and cols a textarea has
107 108 109 |
# File 'lib/quby/questionnaires/entities/question.rb', line 107 def lines @lines end |
#maximum ⇒ Object
Returns the value of attribute maximum.
49 50 51 |
# File 'lib/quby/questionnaires/entities/question.rb', line 49 def maximum @maximum end |
#minimum ⇒ Object
Minimum and maximum values for float and integer types
48 49 50 |
# File 'lib/quby/questionnaires/entities/question.rb', line 48 def minimum @minimum end |
#options ⇒ Object
Multiple-choice questions have options to choose from
41 42 43 |
# File 'lib/quby/questionnaires/entities/question.rb', line 41 def @options end |
#parent ⇒ Object
Some questions are a tree.
77 78 79 |
# File 'lib/quby/questionnaires/entities/question.rb', line 77 def parent @parent end |
#parent_option_key ⇒ Object
Returns the value of attribute parent_option_key.
78 79 80 |
# File 'lib/quby/questionnaires/entities/question.rb', line 78 def parent_option_key @parent_option_key end |
#question_group ⇒ Object
options for grouping questions and setting a minimum or maximum number of answered questions in the group
98 99 100 |
# File 'lib/quby/questionnaires/entities/question.rb', line 98 def question_group @question_group end |
#questionnaire ⇒ Object
Returns the value of attribute questionnaire.
17 18 19 |
# File 'lib/quby/questionnaires/entities/question.rb', line 17 def questionnaire @questionnaire end |
#row_span ⇒ Object
Returns the value of attribute row_span.
115 116 117 |
# File 'lib/quby/questionnaires/entities/question.rb', line 115 def row_span @row_span end |
#sbg_key ⇒ Object
Returns the value of attribute sbg_key.
16 17 18 |
# File 'lib/quby/questionnaires/entities/question.rb', line 16 def sbg_key @sbg_key end |
#score_header ⇒ Object
Whether we use the :description, the :value or :none for the score header above this question
95 96 97 |
# File 'lib/quby/questionnaires/entities/question.rb', line 95 def score_header @score_header end |
#sets_textvar ⇒ Object
Text variable name that will be replaced with the answer to this question In all following text elements that support markdown
104 105 106 |
# File 'lib/quby/questionnaires/entities/question.rb', line 104 def sets_textvar @sets_textvar end |
#show_values ⇒ Object
Whether we show the value for each option :all => in all questionnaire display modes :none => in none of display modes :paged => for only in :paged display mode :bulk => for only in :bulk display mode
59 60 61 |
# File 'lib/quby/questionnaires/entities/question.rb', line 59 def show_values @show_values end |
#size ⇒ Object
To specify size of string/number input boxes
71 72 73 |
# File 'lib/quby/questionnaires/entities/question.rb', line 71 def size @size end |
#table ⇒ Object
Table this question might belong to
111 112 113 |
# File 'lib/quby/questionnaires/entities/question.rb', line 111 def table @table end |
#title ⇒ Object
Returns the value of attribute title.
18 19 20 |
# File 'lib/quby/questionnaires/entities/question.rb', line 18 def title @title end |
#type ⇒ Object
What kind of question is this?
25 26 27 |
# File 'lib/quby/questionnaires/entities/question.rb', line 25 def type @type end |
#unit ⇒ Object
To display unit for number items
69 70 71 |
# File 'lib/quby/questionnaires/entities/question.rb', line 69 def unit @unit end |
#validations ⇒ Object
Structuring
65 66 67 |
# File 'lib/quby/questionnaires/entities/question.rb', line 65 def validations @validations end |
Instance Method Details
#answer_keys ⇒ Object
Returns all possible answer keys of this question (excluding subquestions, including options). radio/select/scale-options do not create answer_keys, but answer values.
271 272 273 |
# File 'lib/quby/questionnaires/entities/question.rb', line 271 def answer_keys [key] end |
#as_json(options = {}) ⇒ Object
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/quby/questionnaires/entities/question.rb', line 227 def as_json( = {}) # rubocop:disable SymbolName super.merge( key: key, title: Quby::MarkdownParser.new(title).to_html, description: Quby::MarkdownParser.new(description).to_html, type: type, unit: unit, hidden: hidden?, displayModes: display_modes, defaultInvisible: default_invisible, viewSelector: view_selector, parentKey: parent&.key, parentOptionKey: parent_option_key, deselectable: deselectable, presentation: presentation, as: as, questionGroup: question_group ) end |
#claimed_keys ⇒ Object
The keys this question claims as his own. Not including options and subquestions. Includes keys for the question, inputs and answers.
265 266 267 |
# File 'lib/quby/questionnaires/entities/question.rb', line 265 def claimed_keys [key] end |
#codebook_key(key, questionnaire, opts = {}) ⇒ Object
328 329 330 |
# File 'lib/quby/questionnaires/entities/question.rb', line 328 def codebook_key(key, questionnaire, opts = {}) key.to_s.gsub(/^v_/, "#{opts[:roqua_key] || questionnaire.key.to_s}_") end |
#codebook_output_range ⇒ Object
336 337 338 339 340 341 342 343 344 345 |
# File 'lib/quby/questionnaires/entities/question.rb', line 336 def codebook_output_range range_min = validations.find { |i| i[:type] == :minimum }&.fetch(:value, nil) range_max = validations.find { |i| i[:type] == :maximum }&.fetch(:value, nil) if range_min || range_max "(#{[range_min, "value", range_max].compact.join(" <= ")})" else "" end end |
#codebook_output_type ⇒ Object
332 333 334 |
# File 'lib/quby/questionnaires/entities/question.rb', line 332 def codebook_output_type type end |
#expand_depends_on_input_keys ⇒ Object
215 216 217 218 219 220 221 |
# File 'lib/quby/questionnaires/entities/question.rb', line 215 def return unless @depends_on @depends_on = questionnaire.(@depends_on) @extra_data[:"depends-on"] = @depends_on.to_json rescue => e raise e.class, "Question #{key} depends_on contains an error: #{e.}" end |
#hidden? ⇒ Boolean
296 297 298 |
# File 'lib/quby/questionnaires/entities/question.rb', line 296 def hidden? hidden end |
#html_id ⇒ Object
288 289 290 |
# File 'lib/quby/questionnaires/entities/question.rb', line 288 def html_id "answer_#{key}_input" end |
#input_keys ⇒ Object
Returns all keys belonging to html inputs generated by this question.
249 250 251 252 253 254 255 256 |
# File 'lib/quby/questionnaires/entities/question.rb', line 249 def input_keys if .blank? answer_keys else # Some options don't have a key (inner_title), they are stripped .map { |opt| opt.input_key }.compact end end |
#key_in_use?(k) ⇒ Boolean
258 259 260 261 |
# File 'lib/quby/questionnaires/entities/question.rb', line 258 def key_in_use?(k) claimed_keys.include?(k) || .any? { |option| option.key_in_use?(k) } end |
#set_depends_on(keys) ⇒ Object
rubocop:disable AccessorMethodName
204 205 206 207 208 |
# File 'lib/quby/questionnaires/entities/question.rb', line 204 def set_depends_on(keys) return if keys.blank? keys = [keys] unless keys.is_a?(Array) @depends_on = keys end |
#show_values_in_mode?(mode) ⇒ Boolean
300 301 302 303 304 305 306 |
# File 'lib/quby/questionnaires/entities/question.rb', line 300 def show_values_in_mode?(mode) case show_values when :none then false when :all then true else show_values == mode end end |
#subquestion? ⇒ Boolean
312 313 314 |
# File 'lib/quby/questionnaires/entities/question.rb', line 312 def subquestion? !parent_option_key.nil? end |
#subquestions ⇒ Object
308 309 310 |
# File 'lib/quby/questionnaires/entities/question.rb', line 308 def subquestions .map { |opt| opt.questions }.flatten end |
#to_codebook(questionnaire, opts = {}) ⇒ Object
316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/quby/questionnaires/entities/question.rb', line 316 def to_codebook(questionnaire, opts = {}) output = [] question_key = codebook_key(key, questionnaire, opts) output << "#{question_key} #{codebook_output_type} #{codebook_output_range}#{' deprecated' if hidden}" output << "\"#{context_free_title}\"" unless context_free_title.blank? = .map do |option| option.to_codebook(questionnaire, opts) end.compact.join("\n") output << unless .blank? output.join("\n") end |
#variable_descriptions ⇒ Object
347 348 349 |
# File 'lib/quby/questionnaires/entities/question.rb', line 347 def variable_descriptions {key => context_free_title}.with_indifferent_access end |
#view_selector ⇒ Object
292 293 294 |
# File 'lib/quby/questionnaires/entities/question.rb', line 292 def view_selector table.blank? ? "#item_#{key}" : "[data-for=#{key}], #answer_#{key}_input" end |