Class: Quby::Compiler::Outputs::LocaleSerializer
- Inherits:
-
Object
- Object
- Quby::Compiler::Outputs::LocaleSerializer
- Defined in:
- lib/quby/compiler/outputs/locale_serializer.rb
Overview
We use Quby2 serialializer for html sanitization, since it’s largest set we allow.
Instance Method Summary collapse
- #as_json(options = {}) ⇒ Object
- #info_block_locale_values(info_block, panel:) ⇒ Object
-
#initialize(questionnaire) ⇒ LocaleSerializer
constructor
A new instance of LocaleSerializer.
- #items_locale_values(items, panel:) ⇒ Object
- #option_locale_values(option, question:) ⇒ Object
- #options_locale_values(question:) ⇒ Object
- #panel_locale_values ⇒ Object
- #question_locale_values(question, panel:) ⇒ Object
- #question_type_specific_values(question) ⇒ Object
- #questions_for_question(question) ⇒ Object
- #slider_label_locale_values(question:, panel:) ⇒ Object
- #split_to_units_question(question) ⇒ Object
- #validations_locale_values ⇒ Object
Constructor Details
#initialize(questionnaire) ⇒ LocaleSerializer
Returns a new instance of LocaleSerializer.
6 7 8 9 |
# File 'lib/quby/compiler/outputs/locale_serializer.rb', line 6 def initialize(questionnaire) @questionnaire = questionnaire @quby_frontend_v2_serializer = QubyFrontendV2Serializer.new(questionnaire) end |
Instance Method Details
#as_json(options = {}) ⇒ Object
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/quby/compiler/outputs/locale_serializer.rb', line 13 def as_json( = {}) { key: key, title: title, short_description: short_description, footer: @quby_frontend_v2_serializer., **panel_locale_values, **validations_locale_values, }.compact end |
#info_block_locale_values(info_block, panel:) ⇒ Object
52 53 54 55 56 57 |
# File 'lib/quby/compiler/outputs/locale_serializer.rb', line 52 def info_block_locale_values(info_block, panel:) { "#{info_block.key}.html" => info_block.html, **items_locale_values(info_block.items, panel: panel), } end |
#items_locale_values(items, panel:) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/quby/compiler/outputs/locale_serializer.rb', line 33 def items_locale_values(items, panel:) items.flat_map { |item| case item when Quby::Compiler::Entities::Text { "#{item.key}" => item.html } when Quby::Compiler::Entities::Question questions_for_question(item).flat_map { |question| question_locale_values(question, panel:) } when Quby::Compiler::Entities::InfoBlock info_block_locale_values(item, panel:) when Quby::Compiler::Entities::Table {} # quby1 only, doesn't use translations. else raise "Unknown panel item type #{item.class} for locale serialization" end }.reduce(:merge) || {} # reduce is nil when no items. end |
#option_locale_values(option, question:) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/quby/compiler/outputs/locale_serializer.rb', line 97 def option_locale_values(option, question:) base_key = option.i18n_key || "#{question.key}.#{option.key}" if option.is_a?(Quby::Compiler::Entities::QuestionOptgroup) { "#{base_key}.label" => option.label, # no html **option..map { option_locale_values(_1, question:) }.reduce(:merge) } elsif option.inner_title { "#{base_key}.html" => @quby_frontend_v2_serializer.inner_title_as_json(option, 0)[:html] } elsif option.placeholder {"#{question.key}.placeholder" => option.label || option.description} else v2_option = @quby_frontend_v2_serializer.option_as_json(option) { "#{base_key}.label" => v2_option[:label], "#{base_key}.description" => v2_option[:description], "#{base_key}.context_free_description" => option.context_free_description, } end end |
#options_locale_values(question:) ⇒ Object
91 92 93 94 95 |
# File 'lib/quby/compiler/outputs/locale_serializer.rb', line 91 def (question:) question..map { |option, res| option_locale_values(option, question:) }.reduce(:merge) end |
#panel_locale_values ⇒ Object
24 25 26 27 28 29 30 31 |
# File 'lib/quby/compiler/outputs/locale_serializer.rb', line 24 def panel_locale_values @questionnaire.panels.flat_map { |panel| { "#{panel.key}.title" => panel.title, **items_locale_values(panel.items, panel: panel) }.compact }.reduce(:merge) end |
#question_locale_values(question, panel:) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/quby/compiler/outputs/locale_serializer.rb', line 59 def question_locale_values(question, panel:) v2_question = @quby_frontend_v2_serializer.question(question) or return {} # type hidden res = { "#{question.key}.title" => v2_question[:title], "#{question.key}.context_free_title" => question.context_free_title, # Might be useful if we want to translate in roqua as well. "#{question.key}.description" => v2_question[:description], "#{question.key}.context_description" => v2_question[:contextDescription], # Might be useful if we want to translate in roqua as well. **question_type_specific_values(question), } rescue puts question.key raise end |
#question_type_specific_values(question) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/quby/compiler/outputs/locale_serializer.rb', line 73 def question_type_specific_values(question) as_type = question.as || question.type case question.as || question.type when :slider (question: question, panel: nil) when :split_to_units split_to_units_question(question) when :radio, :check_box, :select, :scale (question: question) when :integer, :float {"#{question.key}.unit" => question.unit} when :string, :textarea, :date, :country_select {} else raise "Unknown question as #{as_type} for locale serialization" end end |
#questions_for_question(question) ⇒ Object
138 139 140 141 142 143 144 |
# File 'lib/quby/compiler/outputs/locale_serializer.rb', line 138 def questions_for_question(question) questions = [question] questions << question.title_question if question.title_question questions + question.&.flat_map do |option| option.questions # We only allow subquestions one level deep. end end |
#slider_label_locale_values(question:, panel:) ⇒ Object
120 121 122 123 124 125 126 127 |
# File 'lib/quby/compiler/outputs/locale_serializer.rb', line 120 def (question:, panel:) return {} unless question.as == :slider && question.labels.present? base_key = question.labels_i18n_key || "#{question.key}.labels" question.labels.each_with_index.to_h { |label, idx| ["#{base_key}.#{idx}", label] } end |
#split_to_units_question(question) ⇒ Object
129 130 131 132 133 134 135 136 |
# File 'lib/quby/compiler/outputs/locale_serializer.rb', line 129 def split_to_units_question(question) return unless question.type != :split_to_units return {} unless question.conversions.present? # Units without conversions are translated by quby2. question.units.to_h { |unit| ["#{question.key}.units.#{unit.to_s.gsub(/[^a-zA-Z]/, "")}", unit.to_s] } end |
#validations_locale_values ⇒ Object
146 147 148 149 150 151 152 |
# File 'lib/quby/compiler/outputs/locale_serializer.rb', line 146 def validations_locale_values @questionnaire.validations.select { |validation| validation.config[:explanation].present? }.to_h { |validation| ["#{validation.config[:field_key]}.validation.#{validation.type}.explanation", validation.config[:explanation]] } end |