lib/nitro/dispatcher.rb in nitro-0.28.0 vs lib/nitro/dispatcher.rb in nitro-0.29.0

- old
+ new

@@ -1,8 +1,8 @@ require 'nitro/controller' require 'nitro/compiler' -require 'nitro/routing' +require 'nitro/router' require 'nitro/helper/default' module Nitro # Raised when an action can not be found for a path @@ -61,32 +61,30 @@ # '/users' => UsersController # mounts /users # ) # disp.publish '/' => MainController def add_controller(controllers) - for path, c in controllers - unless (c.ancestors.include?(Controller) or c.ancestors.include?(Publishable)) - c.send :include, Publishable + for path, klass in controllers + unless (klass.ancestors.include?(Controller) or klass.ancestors.include?(Publishable)) + klass.send :include, Publishable end - auto_mixin(c) + # Automatically mixin controller helpers. - # Try to setup a template_root if none is defined: - - unless c.template_root - c.module_eval %{ - def self.template_root - "#{Template.root}#{path}".gsub(/\\/$/, '') - end - } - end + mixin_auto_helpers(klass) - # Keep the mount point as an annotation. + # Customize the class for mounting at the given path. + #-- + # gmosx, TODO: should actually create an instance, thus + # allowing mounting the same controller to multiple + # paths, plus simplifying the code. This instance will + # be dup-ed for each request. + #++ - c.ann.self.mount_point = path.gsub(/^\//, '') + klass.mount_at(path) - c.mounted(path) if c.respond_to?(:mounted) + klass.mounted(path) if klass.respond_to?(:mounted) end (@controllers ||= {}).update(controllers) update_routes() @@ -98,16 +96,16 @@ # Call this method to automatically include helpers in the # Controllers. For each Controller 'XxxController' the # default helper 'Helper' and the auto helper # 'XxxControllerHelper' (if it exists) are included. - def auto_mixin(c) - c.helper(Nitro::DefaultHelper) + def mixin_auto_helpers(klass) + klass.helper(Nitro::DefaultHelper) begin - if helper = Module.by_name("#{c}Helper") - c.helper(helper) + if helper = Module.by_name("#{klass}Helper") + klass.helper(helper) end rescue NameError # The auto helper is not defined. end end @@ -121,16 +119,11 @@ @controllers.each do |base, c| base = '' if base == '/' for m in c.action_methods m = m.to_sym if route = c.ann(m).route and (!route.nil?) - unless c.ann(m).params.nil? - keys = c.ann(m).params.keys - else - keys = [] - end - @routes << [route, "#{base}/#{m}", *keys] + add_route(route.first, c, m, route.last) end end end end @@ -157,10 +150,16 @@ # watch out for excessive String creation. # TODO: add caching. #++ def dispatch(path, context = nil) - path = route(path, context) + # Try if the router can directly decode the path. + + klass, action, params = decode_route(path) + if klass + context.params.update(params) + return klass, "#{action}_action", controller.ann.self.mount_point + end parts = path.split('/') parts.shift # get rid of the leading '/'. if klass = controller_class_for("/#{parts.first}")