Sha256: aa7041a6857891b0ad98671b1ef8c8c1b86034d2cf4a9be54a569588c5c4efea

Contents?: true

Size: 1.82 KB

Versions: 70

Compression:

Stored size: 1.82 KB

Contents

module Avo
  class ItemsHolder
    attr_reader :tools
    attr_accessor :items
    attr_accessor :invalid_fields

    def initialize
      @items = []
      @items_index = 0
      @invalid_fields = []
    end

    # Adds a field to the bag
    def field(field_name, **args, &block)
      field_parser = Avo::Dsl::FieldParser.new(id: field_name, order_index: @items_index, **args, &block).parse

      if field_parser.invalid?
        as = args.fetch(:as, nil)

        # End execution ehre and add the field to the invalid_fileds payload so we know to wanr the developer about that.
        # @todo: Make sure this warning is still active
        return add_invalid_field({
          name: field_name,
          as: as,
          # resource: resource_class.name,
          message: "There's an invalid field configuration for this resource. <br/> <code class='px-1 py-px rounded bg-red-600'>field :#{field_name}, as: #{as}</code>"
        })
      end

      add_item field_parser.instance
    end

    def tabs(instance)
      add_item instance
    end

    def tab(name, **args, &block)
      add_item Avo::TabBuilder.parse_block(name: name, **args, &block)
    end

    def tool(klass, **args)
      instance = klass.new(**args)
      add_item instance
    end

    def panel(panel_name = nil, **args, &block)
      panel = Avo::PanelBuilder.parse_block(name: panel_name, **args, &block)

      add_item panel
    end

    def heading(body = nil, **args, &block)
      field = Avo::Fields::HeadingField.new(body, **args)

      add_item field
    end

    def sidebar(instance)
      add_item instance
    end

    def add_item(instance)
      @items << instance

      increment_order_index
    end

    private

    def add_invalid_field(payload)
      @invalid_fields << payload
    end

    def increment_order_index
      @items_index += 1
    end
  end
end

Version data entries

70 entries across 70 versions & 1 rubygems

Version Path
avo-2.19.0 lib/avo/items_holder.rb
avo-2.18.1 lib/avo/items_holder.rb
avo-2.18.1.pre.1.eagerloaddirs lib/avo/items_holder.rb
avo-2.18.0 lib/avo/items_holder.rb
avo-2.17.1.pre.5.stackedlayout lib/avo/items_holder.rb
avo-2.17.1.pre.4.issue.1342 lib/avo/items_holder.rb
avo-2.17.1.pre.3 lib/avo/items_holder.rb
avo-2.17.1.pre.2.customauthorizationclients lib/avo/items_holder.rb
avo-2.17.1.pre.1.zeitwerk.eager.load.dir lib/avo/items_holder.rb
avo-2.17.0 lib/avo/items_holder.rb