lib/quby/compiler/entities/questionnaire.rb in quby-compiler-0.4.7 vs lib/quby/compiler/entities/questionnaire.rb in quby-compiler-0.4.8

- old
+ new

@@ -170,10 +170,23 @@ @questions_tree_cache = (@panels && @panels.map do |panel| panel.items.map { |item| recurse.call(item) if item.is_a?(Quby::Compiler::Entities::Question) } end) end + # sorts parents before children, so showing makes more sense and visiblity rules are ordered correctly + def sorted_questions + return @sorted_questions if @sorted_questions + + key_to_order_by = questions.map.with_index.to_h { [_1.key, [_2]] } + questions.each do |question| + if question.parent + key_to_order_by[question.key].unshift(key_to_order_by[question.parent.key].first) + end + end + @sorted_questions = questions.sort_by { key_to_order_by[_1.key] } + end + def questions question_hash.values end def questions_of_type(type) @@ -199,47 +212,10 @@ validations: validations, visibilityRules: visibility_rules } end - # rubocop:disable Metrics/MethodLength - def to_codebook(options = {}) - output = [] - output << title - output << "Date unknown" - output << "" - - options[:extra_vars]&.each do |var| - output << "#{var[:key]} #{var[:type]}" - output << "\"#{var[:description]}\"" - output << "" - end - - top_questions = panels.map do |panel| - panel.items.select { |item| item.is_a? Question } - end.flatten.compact - - top_questions.each do |question| - output << question.to_codebook(self, options) - output << "" - end - - flags.each_value do |flag| - output << flag.to_codebook(options) - output << "" - end - - textvars.each_value do |textvar| - output << textvar.to_codebook(options) - output << "" - end - - output = output.join("\n") - strip_tags(output.gsub(/\<([ 1-9])/, '&lt;\1')).gsub("&lt;", "<") - end - # rubocop:enable Metrics/MethodLength - def key_in_use?(key) fields.key_in_use?(key) || score_calculations.key?(key) end def add_score_calculation(builder) @@ -413,12 +389,17 @@ end end end.uniq(&:config) end + # Order is important + # quby2 follows them one by one and ignores rules by hidden question, so needs to be in order of interface. + # Flags don't have this issue, so as long as they are first, their order doesn't matter. def visibility_rules - @visibility_rules ||= fields.question_hash.values.flat_map { |question| VisibilityRule.from(question) } \ - + flags.values.flat_map { |flag| VisibilityRule.from_flag(flag) } + @visibility_rules ||= [ + *flags.values.flat_map { |flag| VisibilityRule.from_flag(flag) }, + *sorted_questions.flat_map { |question| VisibilityRule.from(question) } + ] end private def validate_depends_on_flag(textvar_key, textvar_options)