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



99
100
101
102
103
# File 'lib/avo/resources/items/holder.rb', line 99

def add_item(instance)
  @items << instance

  increment_order_index
end

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



79
80
81
# File 'lib/avo/resources/items/holder.rb', line 79

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



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

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
45
46
47
48
49
50
# 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)

    alert_type = :error
    message = "There's an invalid field configuration for this resource. <br/> <code class='px-1 py-px rounded-sm bg-red-600'>field :#{field_name}, as: :#{as}</code>"

    if as == :markdown
      alert_type = :warning
      message = "In Avo 3.16.2 we renamed the <code>:markdown</code> field to <code>:easy_mde</code>. <br/><br/>You may continue to use that one or the new and improved one with Active Storage support. <br/><br/> Read more about it in the <a href=\"https://docs.avohq.io/3.0/fields/markdown.html\" target=\"_blank\">docs</a>."
    end

    # 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:,
      alert_type:,
      message:
    })
  end

  add_item field_parser.instance
end

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



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

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

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



75
76
77
# File 'lib/avo/resources/items/holder.rb', line 75

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


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

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



67
68
69
# File 'lib/avo/resources/items/holder.rb', line 67

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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/avo/resources/items/holder.rb', line 52

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



71
72
73
# File 'lib/avo/resources/items/holder.rb', line 71

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