Sha256: 8774b8c2a7ef79ab9d52a28305230d7ea757a0810ea04cd10fa3b074c9fb24c4

Contents?: true

Size: 1.93 KB

Versions: 13

Compression:

Stored size: 1.93 KB

Contents

# frozen_string_literal: true

module Quby
  module Compiler
    module Entities
      class Panel < Item
        attr_accessor :title
        attr_accessor :items
        attr_accessor :key
        attr_reader :questionnaire

        def initialize(options = {})
          @questionnaire = options[:questionnaire]
          @title = options[:title]
          @key = options[:key]
          @items = options[:items] || []
        end

        def as_json(options = {})
          super.merge(title: title, index: index, items: json_items)
        end

        def index
          @questionnaire.panels.index(self)
        end

        def next
          this_panel_index = index

          if this_panel_index < @questionnaire.panels.size
            return @questionnaire.panels[this_panel_index + 1]
          else
            nil
          end
        end

        def prev
          this_panel_index = index

          if this_panel_index > 0
            return @questionnaire.panels[this_panel_index - 1]
          else
            nil
          end
        end

        def json_items
          items.map do |item|
            case item
            when Text
              { type: 'html', html: item.html }
            when Question
              next if item.table # things inside a table are added to the table, AND ALSO to the panel. skip them.
              { type: 'question', key: item.key }
            when Table
              { type: "table" }
            end
          end.compact
        end

        def validations
          vals = {}
          items.each do |item|
            if item.is_a? Question
              item.options.each do |opt|
                if opt.questions
                  opt.questions.each do |q|
                    vals[q.key] = q.validations
                  end
                end
              end
              vals[item.key] = item.validations
            end
          end
          vals
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
quby-compiler-0.5.14 lib/quby/compiler/entities/panel.rb
quby-compiler-0.5.13 lib/quby/compiler/entities/panel.rb
quby-compiler-0.5.12 lib/quby/compiler/entities/panel.rb
quby-compiler-0.5.11 lib/quby/compiler/entities/panel.rb
quby-compiler-0.5.9 lib/quby/compiler/entities/panel.rb
quby-compiler-0.5.8 lib/quby/compiler/entities/panel.rb
quby-compiler-0.5.7 lib/quby/compiler/entities/panel.rb
quby-compiler-0.5.6 lib/quby/compiler/entities/panel.rb
quby-compiler-0.5.5 lib/quby/compiler/entities/panel.rb
quby-compiler-0.5.4 lib/quby/compiler/entities/panel.rb
quby-compiler-0.5.3 lib/quby/compiler/entities/panel.rb
quby-compiler-0.5.2 lib/quby/compiler/entities/panel.rb
quby-compiler-0.5.1 lib/quby/compiler/entities/panel.rb