Class: EasyAdmin::Layouts::Builders::BaseLayoutBuilder
- Inherits:
-
Object
- Object
- EasyAdmin::Layouts::Builders::BaseLayoutBuilder
- Defined in:
- lib/easy_admin/layouts/builders/base_layout_builder.rb
Overview
Base builder for constructing layout AST
Direct Known Subclasses
Instance Attribute Summary collapse
-
#current_container_stack ⇒ Object
Returns the value of attribute current_container_stack.
-
#resource_class ⇒ Object
readonly
Returns the value of attribute resource_class.
-
#root_node ⇒ Object
readonly
Returns the value of attribute root_node.
Instance Method Summary collapse
-
#add_node(node) ⇒ Object
Add node to current container.
- #belongs_to_field(name, **options) ⇒ Object
- #boolean_field(name, **options) ⇒ Object
-
#build ⇒ Object
Build and return the AST.
-
#content(&block) ⇒ Object
Delegate content method calls.
-
#current_container ⇒ Object
Get current container for adding nodes.
- #date_field(name, **options) ⇒ Object
- #datetime_field(name, **options) ⇒ Object
-
#divider(**attributes) ⇒ Object
Add divider.
- #email_field(name, **options) ⇒ Object
-
#field(name, **options) ⇒ Object
Add field.
-
#fields(*names, **common_options) ⇒ Object
Add multiple fields at once.
- #file_field(name, **options) ⇒ Object
-
#grid(columns: 2, **attributes, &block) ⇒ Object
Add grid layout.
- #has_many_field(name, **options) ⇒ Object
-
#id_field(**options) ⇒ Object
Field DSL delegation methods - delegate to resource class.
-
#initialize(layout_type = :show, resource_class: nil) ⇒ BaseLayoutBuilder
constructor
A new instance of BaseLayoutBuilder.
- #json_field(name, **options) ⇒ Object
-
#method_missing(method, *args, &block) ⇒ Object
Allow custom DSL extensions.
- #number_field(name, **options) ⇒ Object
- #password_field(name = :password, **options) ⇒ Object
-
#render(component_class, **props) ⇒ Object
Render custom component.
- #respond_to_missing?(method, include_private = false) ⇒ Boolean
-
#section(title = nil, **attributes, &block) ⇒ Object
Add section.
- #select_field(name, **options) ⇒ Object
-
#spacer(size: 4, **attributes) ⇒ Object
Add spacer.
-
#tabs(type: :horizontal, **attributes, &block) ⇒ Object
Add tabs container.
- #text_field(name, **options) ⇒ Object
- #textarea_field(name, **options) ⇒ Object
-
#with_container(container_node, &block) ⇒ Object
Execute block with new container.
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_stack ⇒ Object
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_class ⇒ Object (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_node ⇒ Object (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, **) delegate_to_resource(:belongs_to_field, name, **) field(name, type: :belongs_to, **) 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, **) delegate_to_resource(:boolean_field, name, **) field(name, type: :boolean, **) end |
#build ⇒ Object
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_container ⇒ Object
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, **) delegate_to_resource(:date_field, name, **) field(name, type: :date, **) 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, **) delegate_to_resource(:datetime_field, name, **) field(name, type: :datetime, **) 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, **) delegate_to_resource(:email_field, name, **) field(name, type: :email, **) 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, **) field_node = Nodes::FieldNode.new(name, ) 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, **) names.each do |name| field(name, **) 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, **) delegate_to_resource(:file_field, name, **) field(name, type: :file, **) 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, **) delegate_to_resource(:has_many_field, name, **) field(name, type: :has_many, **) 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(**) delegate_to_resource(:id_field, **) field(:id, type: :number, **) 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, **) delegate_to_resource(:json_field, name, **) field(name, type: :json, **) 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, **) delegate_to_resource(:number_field, name, **) field(name, type: :number, **) 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, **) delegate_to_resource(:password_field, name, **) field(name, type: :password, **) 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
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, **) delegate_to_resource(:select_field, name, **) field(name, type: :select, **) 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, **) delegate_to_resource(:text_field, name, **) field(name, type: :string, **) 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, **) delegate_to_resource(:textarea_field, name, **) field(name, type: :text, **) 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 |