Class: RubyUIAdmin::Menu::Builder
- Inherits:
-
Object
- Object
- RubyUIAdmin::Menu::Builder
- Defined in:
- lib/ruby_ui_admin/menu/builder.rb
Overview
Evaluates a config.main_menu block into a tree of menu items. The block is
instance_exec'd against a Builder, so section/resource/link/... are DSL calls.
config. = -> do
section "Tables", icon: "table" do
resource :user
resource :buyer, label: "Buyers"
link "Docs", "https://example.com"
end
end
Instance Attribute Summary collapse
-
#items ⇒ Object
readonly
Returns the value of attribute items.
Class Method Summary collapse
Instance Method Summary collapse
- #all_dashboards(except: []) ⇒ Object
- #all_resources(except: []) ⇒ Object
-
#current_user ⇒ Object
Available inside the menu block.
- #dashboard(id, label: nil) ⇒ Object
-
#initialize ⇒ Builder
constructor
A new instance of Builder.
- #link(label, path = nil, **args) ⇒ Object (also: #link_to)
- #params ⇒ Object
- #resource(name, label: nil) ⇒ Object (also: #resources)
- #section(label = nil, icon: nil, &block) ⇒ Object (also: #group)
Constructor Details
#initialize ⇒ Builder
Returns a new instance of Builder.
45 46 47 |
# File 'lib/ruby_ui_admin/menu/builder.rb', line 45 def initialize @items = [] end |
Instance Attribute Details
#items ⇒ Object (readonly)
Returns the value of attribute items.
43 44 45 |
# File 'lib/ruby_ui_admin/menu/builder.rb', line 43 def items @items end |
Class Method Details
.build(&block) ⇒ Object
37 38 39 40 41 |
# File 'lib/ruby_ui_admin/menu/builder.rb', line 37 def self.build(&block) builder = new builder.instance_exec(&block) builder.items end |
Instance Method Details
#all_dashboards(except: []) ⇒ Object
80 81 82 83 84 85 86 87 |
# File 'lib/ruby_ui_admin/menu/builder.rb', line 80 def all_dashboards(except: []) excluded = Array(except).map(&:to_s) RubyUIAdmin.dashboard_manager.dashboards.each do |dash| next if excluded.include?(dash.id.to_s) @items << DashboardItem.new(dashboard: dash, label: nil) end end |
#all_resources(except: []) ⇒ Object
71 72 73 74 75 76 77 78 |
# File 'lib/ruby_ui_admin/menu/builder.rb', line 71 def all_resources(except: []) excluded = Array(except).map(&:to_s) RubyUIAdmin.resource_manager..each do |rc| next if excluded.include?(rc.route_key) @items << ResourceItem.new(resource: rc, label: nil) end end |
#current_user ⇒ Object
Available inside the menu block.
90 |
# File 'lib/ruby_ui_admin/menu/builder.rb', line 90 def current_user = RubyUIAdmin::Current.user |
#dashboard(id, label: nil) ⇒ Object
66 67 68 69 |
# File 'lib/ruby_ui_admin/menu/builder.rb', line 66 def dashboard(id, label: nil) dash = RubyUIAdmin.dashboard_manager.dashboards.find { |d| d.id.to_s == id.to_s } @items << DashboardItem.new(dashboard: dash, label: label) if dash end |
#link(label, path = nil, **args) ⇒ Object Also known as: link_to
61 62 63 |
# File 'lib/ruby_ui_admin/menu/builder.rb', line 61 def link(label, path = nil, **args) @items << LinkItem.new(label: label, path: path || args[:path]) end |
#params ⇒ Object
91 |
# File 'lib/ruby_ui_admin/menu/builder.rb', line 91 def params = RubyUIAdmin::Current.params |
#resource(name, label: nil) ⇒ Object Also known as: resources
55 56 57 58 |
# File 'lib/ruby_ui_admin/menu/builder.rb', line 55 def resource(name, label: nil, **) resource = RubyUIAdmin.resource_manager.find_by_route_key(name.to_s) @items << ResourceItem.new(resource: resource, label: label) if resource end |
#section(label = nil, icon: nil, &block) ⇒ Object Also known as: group
49 50 51 52 |
# File 'lib/ruby_ui_admin/menu/builder.rb', line 49 def section(label = nil, icon: nil, &block) children = self.class.build(&block) @items << SectionItem.new(label: label, icon: icon, items: children) end |