I have overrode Rails' ActionDispatch::Routing::RouteSet::Dispatcher.controller_reference
method to check if a controller exists by checking for the required constants and then creating them based upon a controller with the same name in the Generic
namespace.
The problem w/ my code is that it is using begin/rescue
, it won't work w/ deeply namespaced controllers, and it's rather verbose.
Can anyone provide some improvements to this code?
class ActionDispatch::Routing::RouteSet::Dispatcher
private
def controller_reference(controller_param)
const_name = @controller_class_names[controller_param] ||= "#{controller_param.camelize}Controller"
obj = Object
const_name.split('::').each do |cn|
begin
obj = obj.const_get(cn)
rescue
if obj == Object
obj = obj.const_set(cn, Class.new(ApplicationController))
else
puts "Creating #{obj}::#{cn} based on Generic::#{cn}"
obj = obj.const_set(cn, Class.new("Generic::#{cn}".constantize))
end
end
end
ActiveSupport::Dependencies.constantize(const_name)
end
end