Sha256: 360260238e62afbd1c613ac601ecfc51651fbdf8834113e79c744d724e23af09

Contents?: true

Size: 1.98 KB

Versions: 27

Compression:

Stored size: 1.98 KB

Contents

# frozen_string_literal: true

require 'quby/questionnaires/entities'

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

27 entries across 27 versions & 1 rubygems

Version Path
quby-5.4.0 lib/quby/questionnaires/entities/panel.rb
quby-5.3.1 lib/quby/questionnaires/entities/panel.rb
quby-5.3.0 lib/quby/questionnaires/entities/panel.rb
quby-5.2.0 lib/quby/questionnaires/entities/panel.rb
quby-5.1.3 lib/quby/questionnaires/entities/panel.rb
quby-5.1.2 lib/quby/questionnaires/entities/panel.rb
quby-5.1.1 lib/quby/questionnaires/entities/panel.rb
quby-5.1.0 lib/quby/questionnaires/entities/panel.rb
quby-5.0.5 lib/quby/questionnaires/entities/panel.rb
quby-5.0.4 lib/quby/questionnaires/entities/panel.rb
quby-5.0.3 lib/quby/questionnaires/entities/panel.rb
quby-5.0.2 lib/quby/questionnaires/entities/panel.rb
quby-5.0.1 lib/quby/questionnaires/entities/panel.rb
quby-5.0.0 lib/quby/questionnaires/entities/panel.rb
quby-4.0.4 lib/quby/questionnaires/entities/panel.rb
quby-5.0.0.pre4 lib/quby/questionnaires/entities/panel.rb
quby-5.0.0.pre3 lib/quby/questionnaires/entities/panel.rb
quby-5.0.0.pre2 lib/quby/questionnaires/entities/panel.rb
quby-5.0.0.pre1 lib/quby/questionnaires/entities/panel.rb
quby-4.0.3 lib/quby/questionnaires/entities/panel.rb