Class: EasyAdmin::Layouts::Builders::BaseLayoutBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/easy_admin/layouts/builders/base_layout_builder.rb

Overview

Base builder for constructing layout AST

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(layout_type = :show, resource_class: nil) ⇒ BaseLayoutBuilder

Returns a new instance of BaseLayoutBuilder.



9
10
11
12
13
# File 'lib/easy_admin/layouts/builders/base_layout_builder.rb', line 9

def initialize(layout_type = :show, resource_class: nil)
  @root_node = Nodes::Root.new(layout_type: layout_type)
  @current_container_stack = [@root_node]
  @resource_class = resource_class
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Allow custom DSL extensions



113
114
115
116
117
118
119
120
121
# File 'lib/easy_admin/layouts/builders/base_layout_builder.rb', line 113

def method_missing(method, *args, &block)
  # Check if it's a registered component type
  if EasyAdmin::Layouts.registered_components.key?(method)
    component_class = EasyAdmin::Layouts.registered_components[method]
    render(component_class, *args)
  else
    super
  end
end

Instance Attribute Details

#current_container_stackObject

Returns the value of attribute current_container_stack.



7
8
9
# File 'lib/easy_admin/layouts/builders/base_layout_builder.rb', line 7

def current_container_stack
  @current_container_stack
end

#resource_classObject (readonly)

Returns the value of attribute resource_class.



6
7
8
# File 'lib/easy_admin/layouts/builders/base_layout_builder.rb', line 6

def resource_class
  @resource_class
end

#root_nodeObject (readonly)

Returns the value of attribute root_node.



6
7
8
# File 'lib/easy_admin/layouts/builders/base_layout_builder.rb', line 6

def root_node
  @root_node
end

Instance Method Details

#add_node(node) ⇒ Object

Add node to current container



21
22
23
24
# File 'lib/easy_admin/layouts/builders/base_layout_builder.rb', line 21

def add_node(node)
  current_container.add_child(node)
  node
end

#belongs_to_field(name, **options) ⇒ Object



176
177
178
179
# File 'lib/easy_admin/layouts/builders/base_layout_builder.rb', line 176

def belongs_to_field(name, **options)
  delegate_to_resource(:belongs_to_field, name, **options)
  field(name, type: :belongs_to, **options)
end

#boolean_field(name, **options) ⇒ Object



166
167
168
169
# File 'lib/easy_admin/layouts/builders/base_layout_builder.rb', line 166

def boolean_field(name, **options)
  delegate_to_resource(:boolean_field, name, **options)
  field(name, type: :boolean, **options)
end

#buildObject

Build and return the AST



108
109
110
# File 'lib/easy_admin/layouts/builders/base_layout_builder.rb', line 108

def build
  @root_node
end

#content(&block) ⇒ Object

Delegate content method calls



202
203
204
205
# File 'lib/easy_admin/layouts/builders/base_layout_builder.rb', line 202

def content(&block)
  content_node = Nodes::Content.new(block: block)
  add_node(content_node)
end

#current_containerObject

Get current container for adding nodes



16
17
18
# File 'lib/easy_admin/layouts/builders/base_layout_builder.rb', line 16

def current_container
  @current_container_stack.last
end

#date_field(name, **options) ⇒ Object



156
157
158
159
# File 'lib/easy_admin/layouts/builders/base_layout_builder.rb', line 156

def date_field(name, **options)
  delegate_to_resource(:date_field, name, **options)
  field(name, type: :date, **options)
end

#datetime_field(name, **options) ⇒ Object



161
162
163
164
# File 'lib/easy_admin/layouts/builders/base_layout_builder.rb', line 161

def datetime_field(name, **options)
  delegate_to_resource(:datetime_field, name, **options)
  field(name, type: :datetime, **options)
end

#divider(**attributes) ⇒ Object

Add divider



98
99
100
# File 'lib/easy_admin/layouts/builders/base_layout_builder.rb', line 98

def divider(**attributes)
  add_node(Nodes::Divider.new(attributes))
end

#email_field(name, **options) ⇒ Object



151
152
153
154
# File 'lib/easy_admin/layouts/builders/base_layout_builder.rb', line 151

def email_field(name, **options)
  delegate_to_resource(:email_field, name, **options)
  field(name, type: :email, **options)
end

#field(name, **options) ⇒ Object

Add field



79
80
81
82
# File 'lib/easy_admin/layouts/builders/base_layout_builder.rb', line 79

def field(name, **options)
  field_node = Nodes::FieldNode.new(name, options)
  add_node(field_node)
end

#fields(*names, **common_options) ⇒ Object

Add multiple fields at once



85
86
87
88
89
# File 'lib/easy_admin/layouts/builders/base_layout_builder.rb', line 85

def fields(*names, **common_options)
  names.each do |name|
    field(name, **common_options)
  end
end

#file_field(name, **options) ⇒ Object



186
187
188
189
# File 'lib/easy_admin/layouts/builders/base_layout_builder.rb', line 186

def file_field(name, **options)
  delegate_to_resource(:file_field, name, **options)
  field(name, type: :file, **options)
end

#grid(columns: 2, **attributes, &block) ⇒ Object

Add grid layout



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/easy_admin/layouts/builders/base_layout_builder.rb', line 66

def grid(columns: 2, **attributes, &block)
  grid_node = Nodes::Grid.new(attributes.merge(columns: columns))
  
  if block_given?
    with_container(grid_node, &block)
  else
    add_node(grid_node)
  end
  
  grid_node
end

#has_many_field(name, **options) ⇒ Object



181
182
183
184
# File 'lib/easy_admin/layouts/builders/base_layout_builder.rb', line 181

def has_many_field(name, **options)
  delegate_to_resource(:has_many_field, name, **options)
  field(name, type: :has_many, **options)
end

#id_field(**options) ⇒ Object

Field DSL delegation methods - delegate to resource class



131
132
133
134
# File 'lib/easy_admin/layouts/builders/base_layout_builder.rb', line 131

def id_field(**options)
  delegate_to_resource(:id_field, **options)
  field(:id, type: :number, **options)
end

#json_field(name, **options) ⇒ Object



191
192
193
194
# File 'lib/easy_admin/layouts/builders/base_layout_builder.rb', line 191

def json_field(name, **options)
  delegate_to_resource(:json_field, name, **options)
  field(name, type: :json, **options)
end

#number_field(name, **options) ⇒ Object



146
147
148
149
# File 'lib/easy_admin/layouts/builders/base_layout_builder.rb', line 146

def number_field(name, **options)
  delegate_to_resource(:number_field, name, **options)
  field(name, type: :number, **options)
end

#password_field(name = :password, **options) ⇒ Object



196
197
198
199
# File 'lib/easy_admin/layouts/builders/base_layout_builder.rb', line 196

def password_field(name = :password, **options)
  delegate_to_resource(:password_field, name, **options)
  field(name, type: :password, **options)
end

#render(component_class, **props) ⇒ Object

Render custom component



92
93
94
95
# File 'lib/easy_admin/layouts/builders/base_layout_builder.rb', line 92

def render(component_class, **props)
  render_node = Nodes::RenderNode.new(component_class, props)
  add_node(render_node)
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


123
124
125
126
127
# File 'lib/easy_admin/layouts/builders/base_layout_builder.rb', line 123

def respond_to_missing?(method, include_private = false)
  EasyAdmin::Layouts.registered_components.key?(method) || 
  field_method?(method) || 
  super
end

#section(title = nil, **attributes, &block) ⇒ Object

Add section



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/easy_admin/layouts/builders/base_layout_builder.rb', line 53

def section(title = nil, **attributes, &block)
  section_node = Nodes::Section.new(title, attributes)
  
  if block_given?
    with_container(section_node, &block)
  else
    add_node(section_node)
  end
  
  section_node
end

#select_field(name, **options) ⇒ Object



171
172
173
174
# File 'lib/easy_admin/layouts/builders/base_layout_builder.rb', line 171

def select_field(name, **options)
  delegate_to_resource(:select_field, name, **options)
  field(name, type: :select, **options)
end

#spacer(size: 4, **attributes) ⇒ Object

Add spacer



103
104
105
# File 'lib/easy_admin/layouts/builders/base_layout_builder.rb', line 103

def spacer(size: 4, **attributes)
  add_node(Nodes::Spacer.new(attributes.merge(size: size)))
end

#tabs(type: :horizontal, **attributes, &block) ⇒ Object

Add tabs container



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/easy_admin/layouts/builders/base_layout_builder.rb', line 38

def tabs(type: :horizontal, **attributes, &block)
  tabs_node = Nodes::Tabs.new(attributes.merge(type: type))
  add_node(tabs_node)
  
  if block_given?
    @current_container_stack.push(tabs_node)
    tabs_builder = TabsBuilder.new(self, tabs_node)
    tabs_builder.instance_exec(&block)
    @current_container_stack.pop
  end
  
  tabs_node
end

#text_field(name, **options) ⇒ Object



136
137
138
139
# File 'lib/easy_admin/layouts/builders/base_layout_builder.rb', line 136

def text_field(name, **options)
  delegate_to_resource(:text_field, name, **options)
  field(name, type: :string, **options)
end

#textarea_field(name, **options) ⇒ Object



141
142
143
144
# File 'lib/easy_admin/layouts/builders/base_layout_builder.rb', line 141

def textarea_field(name, **options)
  delegate_to_resource(:textarea_field, name, **options)
  field(name, type: :text, **options)
end

#with_container(container_node, &block) ⇒ Object

Execute block with new container



27
28
29
30
31
32
33
# File 'lib/easy_admin/layouts/builders/base_layout_builder.rb', line 27

def with_container(container_node, &block)
  add_node(container_node)
  @current_container_stack.push(container_node)
  instance_exec(&block) if block_given?
  @current_container_stack.pop
  container_node
end