Class: Avo::Resources::Items::Holder

Inherits:
Object
  • Object
show all
Defined in:
lib/avo/resources/items/holder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from: nil, parent: nil) ⇒ Holder

Returns a new instance of Holder.



6
7
8
9
10
11
12
# File 'lib/avo/resources/items/holder.rb', line 6

def initialize(from: nil, parent: nil)
  @items = []
  @items_index = 0
  @invalid_fields = []
  @from = from
  @parent = parent
end

Instance Attribute Details

#fromObject (readonly)

Returns the value of attribute from.



2
3
4
# File 'lib/avo/resources/items/holder.rb', line 2

def from
  @from
end

#invalid_fieldsObject

Returns the value of attribute invalid_fields.



4
5
6
# File 'lib/avo/resources/items/holder.rb', line 4

def invalid_fields
  @invalid_fields
end

#itemsObject

Returns the value of attribute items.



3
4
5
# File 'lib/avo/resources/items/holder.rb', line 3

def items
  @items
end

#parentObject (readonly)

Returns the value of attribute parent.



2
3
4
# File 'lib/avo/resources/items/holder.rb', line 2

def parent
  @parent
end

#toolsObject (readonly)

Returns the value of attribute tools.



2
3
4
# File 'lib/avo/resources/items/holder.rb', line 2

def tools
  @tools
end

Instance Method Details

#add_item(instance) ⇒ Object



93
94
95
96
97
# File 'lib/avo/resources/items/holder.rb', line 93

def add_item(instance)
  @items << instance

  increment_order_index
end

#card(title: nil, **args, &block) ⇒ Object



73
74
75
# File 'lib/avo/resources/items/holder.rb', line 73

def card(title: nil, **args, &block)
  add_item Avo::Resources::Items::Card::Builder.parse_block(title: title, parent: @parent, **args, &block)
end

#check_sidebar_is_inside_a_panelObject



87
88
89
90
91
# File 'lib/avo/resources/items/holder.rb', line 87

def check_sidebar_is_inside_a_panel
  unless @from.eql?(Avo::Resources::Items::Panel::Builder) || @from.eql?(Avo::Resources::Items::MainPanel::Builder)
    raise "The sidebar must be inside a panel."
  end
end

#collaboration_timeline(**args) ⇒ Object



14
15
16
# File 'lib/avo/resources/items/holder.rb', line 14

def collaboration_timeline(**args)
  add_item Avo::Resources::Items::Collaboration.new(**args)
end

#field(field_name, **args, &block) ⇒ Object

Adds a field to the items_holder



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/avo/resources/items/holder.rb', line 19

def field(field_name, **args, &block)
  # If the field is paresed inside a tab group, add it to the tab
  # This will happen when the field is parsed inside a tab group from a resource method
  if from.present? && from.class == Avo::Resources::Items::TabGroup::Builder
    return from.field(field_name, holder: self, **args, &block)
  end

  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)

    message = "Invalid field configuration"
    description = "Check your resource file for: <code>field :#{field_name}, as: :#{as}</code>"

    return add_invalid_field({
      name: field_name,
      as:,
      alert_type: :error,
      message:,
      description:
    })
  end

  add_item field_parser.instance
end

#header(**args, &block) ⇒ Object



77
78
79
# File 'lib/avo/resources/items/holder.rb', line 77

def header(**args, &block)
  add_item Avo::Resources::Items::Header.new(**args)
end

#panel(title: nil, **args, &block) ⇒ Object



69
70
71
# File 'lib/avo/resources/items/holder.rb', line 69

def panel(title: nil, **args, &block)
  add_item Avo::Resources::Items::ItemGroup::Builder.parse_block(title: title, parent: @parent, **args, &block)
end


81
82
83
84
85
# File 'lib/avo/resources/items/holder.rb', line 81

def sidebar(**args, &block)
  check_sidebar_is_inside_a_panel

  add_item Avo::Resources::Items::Sidebar::Builder.parse_block(parent: @parent, **args, &block)
end

#tab(title:, **args, &block) ⇒ Object



61
62
63
# File 'lib/avo/resources/items/holder.rb', line 61

def tab(title:, **args, &block)
  add_item Avo::Resources::Items::Tab::Builder.parse_block(title: title, parent: @parent, **args, &block)
end

#tabs(tab = nil, id: nil, title: nil, description: nil, **args, &block) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/avo/resources/items/holder.rb', line 46

def tabs(tab = nil, id: nil, title: nil, description: nil, **args, &block)
  if tab.present?
    add_item tab
  else
    add_item Avo::Resources::Items::TabGroup::Builder.parse_block(
      parent: @parent,
      id: id,
      title: title,
      description: description,
      **args,
      &block
    )
  end
end

#tool(klass, **args) ⇒ Object



65
66
67
# File 'lib/avo/resources/items/holder.rb', line 65

def tool(klass, **args)
  add_item klass.new(**args, view: parent.view, parent: parent)
end