Module: Alchemy::Modules

Included in:
BaseController
Defined in:
lib/alchemy/modules.rb

Constant Summary collapse

@@alchemy_modules =
YAML.load_file(File.expand_path("../../config/alchemy/modules.yml", __dir__))

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



10
11
12
# File 'lib/alchemy/modules.rb', line 10

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'
}


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/alchemy/modules.rb', line 27

def register_module(module_definition)
  definition_hash = module_definition.deep_stringify_keys

  ### Validate controller(s) existence
  if definition_hash["navigation"].is_a?(Hash)
    defined_controllers = [definition_hash["navigation"]["controller"]]

    if definition_hash["navigation"]["sub_navigation"].is_a?(Array)
      defined_controllers.concat(definition_hash["navigation"]["sub_navigation"].map { |x| x["controller"] })
    end

    validate_controllers_existence(defined_controllers)
  end

  @@alchemy_modules |= [definition_hash]
end

Instance Method Details

#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



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/alchemy/modules.rb', line 66

def module_definition_for(name_or_params)
  case name_or_params
  when String
    alchemy_modules.detect { |m| m["name"] == name_or_params }
  when Hash
    name_or_params.stringify_keys!
    alchemy_modules.detect do |alchemy_module|
      module_navi = alchemy_module.fetch("navigation", {})
      definition_from_mainnavi(module_navi, name_or_params) ||
        definition_from_subnavi(module_navi, name_or_params) ||
        definition_from_nested(module_navi, name_or_params)
    end
  else
    raise ArgumentError, "Could not find module definition for #{name_or_params}"
  end
end