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



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

def add_item(instance)
  @items << instance

  increment_order_index
end

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



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

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



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

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
51
52
# 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 = "Invalid field configuration"
    description = "Check your resource file for: <code>field :#{field_name}, as: :#{as}</code>"

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

    # End execution here and add the field to the invalid_fields payload so we know to warn the developer about that.
    return add_invalid_field({
      name: field_name,
      as:,
      alert_type:,
      message:,
      description:
    })
  end

  add_item field_parser.instance
end

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



85
86
87
# File 'lib/avo/resources/items/holder.rb', line 85

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

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



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

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


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

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



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

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



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

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



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

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