lib/jets/router.rb in jets-1.4.11 vs lib/jets/router.rb in jets-1.5.0
- old
+ new
@@ -1,9 +1,11 @@
require 'text-table'
module Jets
class Router
+ autoload :Scope, 'jets/router/scope'
+
attr_reader :routes
def initialize
@routes = []
end
@@ -16,10 +18,37 @@
define_method method_name do |path, options|
create_route(options.merge(path: path, method: __method__))
end
end
+ def create_route(options)
+ # Currently only using scope to add namespace
+ # TODO: Can use it to add additional things like authorization_type
+ # Would be good to add authorization_type at the controller level also
+ options[:path] = add_namespace(options[:path])
+ @routes << Route.new(options)
+ end
+
+ def add_namespace(path)
+ return path unless @scope
+ ns = @scope.full_namespace
+ return path unless ns
+ "#{ns}/#{path}"
+ end
+
+ def namespace(ns, &block)
+ scope(namespace: ns, &block)
+ end
+
+ def scope(options={})
+ root_level = @scope.nil?
+ @scope = root_level ? Scope.new(options) : @scope.new(options)
+ yield
+ ensure
+ @scope = @scope.parent if @scope
+ end
+
# resources macro expands to all the routes
def resources(name)
get "#{name}", to: "#{name}#index"
get "#{name}/new", to: "#{name}#new" unless api_mode?
get "#{name}/:id", to: "#{name}#show"
@@ -41,13 +70,9 @@
jets upgrade
EOL
end
api_mode = Jets.config.mode == 'api' || Jets.config.api_mode || Jets.config.api_generator
api_mode
- end
-
- def create_route(options)
- @routes << Route.new(options)
end
# root "posts#index"
def root(to)
@routes << Route.new(path: '', to: to, method: :get, root: true)