class Quby::Compiler::Entities::Question
Constants
- MARKDOWN_ATTRIBUTES
Attributes
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.
Whether to skip the uniqueness validation on radio and select option values
How should we display this question
Whether the browser should autocomplete this question (off by default)
In case of being displayed inside a table, amount of columns/rows to span
Slider only: where to place the sliding thing by default Can have value :hidden for a hidden handle.
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.
Whether this radio question is deselectable
Whether we can collapse this in bulk view
In what modes do we display this question NOTE We always display questions in print-view (if they have an answer)
Extra data hash to store on the question item's html element
data-attributes for the input tag.
Standard attributes
Amount of rows and cols a textarea has
Minimum and maximum values for float and integer types
Multiple-choice questions have options to choose from
Some questions are a tree.
options for grouping questions and setting a minimum or maximum number of answered questions in the group
Whether we use the :description, the :value or :none for the score header above this question
Text
variable name that will be replaced with the answer to this question In all following text elements that support markdown
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
To specify size of string/number input boxes
Table
this question might belong to
What kind of question is this?
To display unit for number items
Structuring
Public Class Methods
rubocop:disable CyclomaticComplexity, Metrics/MethodLength
Quby::Compiler::Entities::Item::new
# File lib/quby/compiler/entities/question.rb, line 126 def initialize(key, options = {}) super(options) @extra_data ||= {} @options = [] @allow_duplicate_option_values = options[:allow_duplicate_option_values] @questionnaire = options[:questionnaire] @key = key @sbg_key = options[:sbg_key] @type = options[:type] @as = options[:as] @title = options[:title] @context_free_title = options[:context_free_title] @allow_blank_titles = options[:allow_blank_titles] @description = options[:description] @display_modes = options[:display_modes] @presentation = options[:presentation] @validations = [] @parent = options[:parent] @hidden = options[:hidden] @table = options[:table] @parent_option_key = options[:parent_option_key] @autocomplete = options[:autocomplete] || "off" @show_values = options[:show_values] || :bulk @deselectable = (options[:deselectable].nil? || options[:deselectable]) @disallow_bulk = options[:disallow_bulk] @score_header = options[:score_header] || :none @sets_textvar = "#{questionnaire.key}_#{options[:sets_textvar]}" if options[:sets_textvar] @unit = options[:unit] @lines = options[:lines] || 6 @cols = options[:cols] || 40 @default_invisible = options[:default_invisible] || false @labels ||= [] @col_span = options[:col_span] || 1 @row_span = options[:row_span] || 1 set_depends_on(options[:depends_on]) @question_group = options[:question_group] @group_minimum_answered = options[:group_minimum_answered] @group_maximum_answered = options[:group_maximum_answered] @input_data = {} @input_data[:value_tooltip] = true if options[:value_tooltip] # Require subquestions of required questions by default options[:required] = true if @parent&.validations&.first&.fetch(:type, nil) == :requires_answer @validations << {type: :requires_answer, explanation: options[:error_explanation]} if options[:required] if @type == :float @validations << {type: :valid_float, explanation: options[:error_explanation]} elsif @type == :integer @validations << {type: :valid_integer, explanation: options[:error_explanation]} end if options[:minimum] and (@type == :integer || @type == :float) fail "deprecated" # pretty sure this is not used anywhere end if options[:maximum] and (@type == :integer || @type == :float) fail "deprecated" # pretty sure this is not used anywhere end @default_position = options[:default_position] if @question_group if @group_minimum_answered @validations << {type: :answer_group_minimum, group: @question_group, value: @group_minimum_answered, explanation: options[:error_explanation]} end if @group_maximum_answered @validations << {type: :answer_group_maximum, group: @question_group, value: @group_maximum_answered, explanation: options[:error_explanation]} end end end
Public Instance Methods
Returns all possible answer keys of this question (excluding subquestions, including options). radio/select/scale-options do not create answer_keys
, but answer values.
# File lib/quby/compiler/entities/question.rb, line 272 def answer_keys [key] end
Quby::Compiler::Entities::Item#as_json
# File lib/quby/compiler/entities/question.rb, line 227 def as_json(options = {}) # 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, size: size, 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
The keys this question claims as his own. Not including options and subquestions. Includes keys for the question, inputs and answers.
# File lib/quby/compiler/entities/question.rb, line 266 def claimed_keys [key] end
# File lib/quby/compiler/entities/question.rb, line 330 def codebook_key(key, questionnaire, opts = {}) key.to_s.gsub(/^v_/, "#{opts[:roqua_key] || questionnaire.key.to_s}_") end
# File lib/quby/compiler/entities/question.rb, line 338 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
# File lib/quby/compiler/entities/question.rb, line 334 def codebook_output_type type end
# File lib/quby/compiler/entities/question.rb, line 215 def expand_depends_on_input_keys return unless @depends_on @depends_on = questionnaire.expand_input_keys(@depends_on) @extra_data[:"depends-on"] = @depends_on.to_json rescue => e raise e.class, "Question #{key} depends_on contains an error: #{e.message}" end
# File lib/quby/compiler/entities/question.rb, line 290 def html_id "answer_#{key}_input" end
Returns all keys belonging to html inputs generated by this question.
# File lib/quby/compiler/entities/question.rb, line 250 def input_keys if options.blank? answer_keys else # Some options don't have a key (inner_title), they are stripped options.map { |opt| opt.input_key }.compact end end
# File lib/quby/compiler/entities/question.rb, line 259 def key_in_use?(k) claimed_keys.include?(k) || options.any? { |option| option.key_in_use?(k) } end
rubocop:disable AccessorMethodName
# File lib/quby/compiler/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
# File lib/quby/compiler/entities/question.rb, line 302 def show_values_in_mode?(mode) case show_values when :none then false when :all then true else show_values == mode end end
# File lib/quby/compiler/entities/question.rb, line 314 def subquestion? !parent_option_key.nil? end
# File lib/quby/compiler/entities/question.rb, line 310 def subquestions options.map { |opt| opt.questions }.flatten end
# File lib/quby/compiler/entities/question.rb, line 318 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? options_string = options.map do |option| option.to_codebook(questionnaire, opts) end.compact.join("\n") output << options_string unless options.blank? output.join("\n") end
# File lib/quby/compiler/entities/question.rb, line 349 def variable_descriptions {key => context_free_title}.with_indifferent_access end
# File lib/quby/compiler/entities/question.rb, line 294 def view_selector table.blank? ? "#item_#{key}" : "[data-for=#{key}], #answer_#{key}_input" end