Sha256: 2def8c3112a675181f44f4e403f747b69a081a651e85e8df11adf3a52d761ad3

Contents?: true

Size: 1.76 KB

Versions: 41

Compression:

Stored size: 1.76 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 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

41 entries across 41 versions & 1 rubygems

Version Path
avo-2.13.6.pre.2 lib/avo/items_holder.rb
avo-2.13.6.pre.1 lib/avo/items_holder.rb
avo-2.13.5.pre.2 lib/avo/items_holder.rb
avo-2.13.5.pre.1 lib/avo/items_holder.rb
avo-2.13.4.pre.1 lib/avo/items_holder.rb
avo-2.13.3.pre.4 lib/avo/items_holder.rb
avo-2.13.3.pre.3 lib/avo/items_holder.rb
avo-2.13.3.pre.2 lib/avo/items_holder.rb
avo-2.13.3.pre.1 lib/avo/items_holder.rb
avo-2.13.2.pre.2 lib/avo/items_holder.rb
avo-2.13.2.pre.1 lib/avo/items_holder.rb
avo-2.13.1 lib/avo/items_holder.rb
avo-2.13.0 lib/avo/items_holder.rb
avo-2.12.1.pre.1 lib/avo/items_holder.rb
avo-2.12.0 lib/avo/items_holder.rb
avo-2.11.3.pre.3 lib/avo/items_holder.rb
avo-2.11.3.pre.2 lib/avo/items_holder.rb
avo-2.11.3.pre.1 lib/avo/items_holder.rb
avo-2.11.2.pre.3 lib/avo/items_holder.rb
avo-2.11.2.pre.2 lib/avo/items_holder.rb