Module: Alchemy::Modules
- Included in:
- BaseController
- Defined in:
- lib/alchemy/modules.rb
Class Method Summary collapse
- .alchemy_modules ⇒ Object
- .included(base) ⇒ Object
-
.register_module(module_definition) ⇒ Object
Register a Alchemy module.
- .to_legacy_hash(menu_item) ⇒ Object
Instance Method Summary collapse
- #alchemy_modules ⇒ Object
-
#module_definition_for(name_or_params) ⇒ Object
Get the module definition for given module name.
Class Method Details
.alchemy_modules ⇒ Object
31 32 33 |
# File 'lib/alchemy/modules.rb', line 31 def alchemy_modules Alchemy.config..map { || to_legacy_hash() } end |
.included(base) ⇒ Object
6 7 8 |
# File 'lib/alchemy/modules.rb', line 6 def included(base) base.send :helper_method, :alchemy_modules, :module_definition_for end |
.register_module(module_definition) ⇒ Object
Register a Alchemy module.
A module is a Hash that must have at least a name and a navigation key that has a controller and action name.
Example:
name: 'module',
navigation: {
controller: 'admin/controller_name',
action: 'index'
}
23 24 25 26 27 28 |
# File 'lib/alchemy/modules.rb', line 23 def register_module(module_definition) = Alchemy::Configurations::MenuItem.from_legacy_hash(module_definition) return if .name.present? && Alchemy.config..any? { |item| item.name == .name } Alchemy.config..add() end |
.to_legacy_hash(menu_item) ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/alchemy/modules.rb', line 36 def to_legacy_hash() { "name" => .name, "engine_name" => .engine_name, "position" => .position, "navigation" => () }.compact end |
Instance Method Details
#alchemy_modules ⇒ Object
67 68 69 |
# File 'lib/alchemy/modules.rb', line 67 def alchemy_modules Alchemy::Modules.alchemy_modules end |
#module_definition_for(name_or_params) ⇒ Object
Get the module definition for given module name
You can also pass a hash of an module definition. It then tries to find the module defintion from controller name and action name
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/alchemy/modules.rb', line 76 def module_definition_for(name_or_params) = case name_or_params when String Alchemy.config..detect { |item| item.name == name_or_params } when Hash params = name_or_params.stringify_keys Alchemy.config..detect do |item| [item, *item.children, *item.nested].any? do |entry| entry.matches_controller?(controller: params["controller"], action: params["action"]) end end else raise ArgumentError, "Could not find module definition for #{name_or_params}" end Alchemy::Modules.to_legacy_hash() if end |