Skip to content

Commit

Permalink
verify controller key within register_module
Browse files Browse the repository at this point in the history
  • Loading branch information
westonganger committed Oct 25, 2018
1 parent 7412b08 commit 2c8494a
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 19 deletions.
67 changes: 49 additions & 18 deletions lib/alchemy/modules.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,56 @@ module Modules

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

def self.included(base)
base.send :helper_method, :alchemy_modules, :module_definition_for
end
class << self
def included(base)
base.send :helper_method, :alchemy_modules, :module_definition_for
end

# 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'
# }
#
def self.register_module(module_definition)
@@alchemy_modules << module_definition.deep_stringify_keys
# 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'
# }
#
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

private

def validate_controllers_existence(controllers)
controllers.each do |controller_val|
next if controller_val.blank?

controller_name = "#{controller_val.camelize}Controller"

begin
controller_name.constantize
rescue NameError
raise "Error in AlchemyCMS module definition: '#{definition_hash['name']}'. Could not find the matching controller class #{controller_name.sub(/^::/, '')} for the specified controller: '#{controller_val}'"
end
end
end
end

# Get the module definition for given module name
Expand Down
38 changes: 37 additions & 1 deletion spec/libraries/modules_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,52 @@ class ModulesTestController < ApplicationController
{
'name' => 'module',
'navigation' => {
'controller' => 'admin/controller_name',
'controller' => 'register_module_dummy',
'action' => 'index'
}
}
end

let(:bad_alchemy_module_a) do
{
'name' => 'bad_module_a',
'navigation' => {
'controller' => 'bad_module',
'action' => 'index'
}
}
end

let(:bad_alchemy_module_b) do
{
'name' => 'bad_module_b',
'navigation' => {
'controller' => 'register_module_dummy',
'action' => 'index',
'sub_navigation' => [{
'controller' => 'bad_module',
'action' => 'index'
}]
}
}
end

it "registers a module definition into global list of modules" do
class ::RegisterModuleDummyController
### mock the existence of the controller
end

Modules.register_module(alchemy_module)
expect(Modules.alchemy_modules).to include(alchemy_module)
end

it "fails to register a module when a matching navigation controller cannot be found" do
expect { Modules.register_module(bad_alchemy_module_a) }.to raise_error(NameError)
end

it "fails to register a module when a matching sub_navigation controller cannot be found" do
expect { Modules.register_module(bad_alchemy_module_b) }.to raise_error(NameError)
end
end
end
end

0 comments on commit 2c8494a

Please sign in to comment.