Sha256: 65d14b6c990d888197775f72595d9f15ec7342b3c7791bb463d7e2f4f59a9a5e

Contents?: true

Size: 1.94 KB

Versions: 8

Compression:

Stored size: 1.94 KB

Contents

# frozen_string_literal: true

module Quby
  module Questionnaires
    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

8 entries across 8 versions & 1 rubygems

Version Path
quby-5.6.7 lib/quby/questionnaires/entities/panel.rb
quby-5.6.6 lib/quby/questionnaires/entities/panel.rb
quby-5.6.5 lib/quby/questionnaires/entities/panel.rb
quby-5.6.3 lib/quby/questionnaires/entities/panel.rb
quby-5.6.2 lib/quby/questionnaires/entities/panel.rb
quby-5.6.1 lib/quby/questionnaires/entities/panel.rb
quby-5.6.0 lib/quby/questionnaires/entities/panel.rb
quby-5.5.0 lib/quby/questionnaires/entities/panel.rb