Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verify controller keys within register_module #1495

Merged
merged 1 commit into from Oct 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
67 changes: 49 additions & 18 deletions lib/alchemy/modules.rb
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
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