class Quby::Questionnaires::Entities::Panel

Attributes

items[RW]
key[RW]
questionnaire[R]
title[RW]

Public Class Methods

new(options = {}) click to toggle source
# File lib/quby/questionnaires/entities/panel.rb, line 14
def initialize(options = {})
  @questionnaire = options[:questionnaire]
  @title = options[:title]
  @key = options[:key]
  @items = options[:items] || []
end

Public Instance Methods

as_json(options = {}) click to toggle source
# File lib/quby/questionnaires/entities/panel.rb, line 21
def as_json(options = {})
  super.merge(title: title, index: index, items: json_items)
end
index() click to toggle source
# File lib/quby/questionnaires/entities/panel.rb, line 25
def index
  @questionnaire.panels.index(self)
end
json_items() click to toggle source
# File lib/quby/questionnaires/entities/panel.rb, line 49
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
next() click to toggle source
# File lib/quby/questionnaires/entities/panel.rb, line 29
def next
  this_panel_index = index

  if this_panel_index < @questionnaire.panels.size
    return @questionnaire.panels[this_panel_index + 1]
  else
    nil
  end
end
prev() click to toggle source
# File lib/quby/questionnaires/entities/panel.rb, line 39
def prev
  this_panel_index = index

  if this_panel_index > 0
    return @questionnaire.panels[this_panel_index - 1]
  else
    nil
  end
end
validations() click to toggle source
# File lib/quby/questionnaires/entities/panel.rb, line 63
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