lib/jets/router.rb in jets-1.9.32 vs lib/jets/router.rb in jets-2.0.0
- old
+ new
@@ -1,12 +1,15 @@
require 'text-table'
module Jets
class Router
+ include Dsl
+
attr_reader :routes
def initialize
@routes = []
+ @scope = Scope.new
end
def draw(&block)
instance_eval(&block)
check_collision!
@@ -18,55 +21,44 @@
collision = Jets::Resource::ApiGateway::RestApi::Routes::Collision.new
collide = collision.collision?(paths)
raise collision.exception if collide
end
- # Methods supported by API Gateway
- %w[any delete get head options patch post put].each do |method_name|
- 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)
+ infer_to_option!(options)
+ handle_on!(options)
+ MethodCreator.new(options, @scope).define_url_helper!
+ @routes << Route.new(options, @scope)
end
- def add_namespace(path)
- return path unless @scope
- ns = @scope.full_namespace
- return path unless ns
- "#{ns}/#{path}"
- end
+ # Can possibly infer to option from the path. Example:
+ #
+ # get 'posts/index'
+ # get 'posts', to: 'posts#index'
+ #
+ # get 'posts/show'
+ # get 'posts', to: 'posts#show'
+ #
+ def infer_to_option!(options)
+ return if options[:to]
- def namespace(ns, &block)
- scope(namespace: ns, &block)
- end
+ path = options[:path].to_s
+ return unless path.include?('/')
- def scope(options={})
- root_level = @scope.nil?
- @scope = root_level ? Scope.new(options) : @scope.new(options)
- yield
- ensure
- @scope = @scope.parent if @scope
+ items = path.split('/')
+ if items.size == 2
+ options[:to] = items.join('#')
+ end
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"
- post "#{name}", to: "#{name}#create"
- get "#{name}/:id/edit", to: "#{name}#edit" unless api_mode?
- put "#{name}/:id", to: "#{name}#update"
- post "#{name}/:id", to: "#{name}#update" # for binary uploads
- patch "#{name}/:id", to: "#{name}#update"
- delete "#{name}/:id", to: "#{name}#delete"
+ def handle_on!(options)
+ if options[:on] && !%w[resources resource].include?(@scope.from.to_s)
+ raise Error.new("ERROR: The `on:` option can only be used within a resource or resources block")
+ end
+ options[:on] ||= @on_option if @on_option
end
def api_mode?
if Jets.config.key?(:api_mode) || Jets.config.key?(:api_generator)
puts <<~EOL.color(:yellow)
@@ -79,17 +71,10 @@
end
api_mode = Jets.config.mode == 'api' || Jets.config.api_mode || Jets.config.api_generator
api_mode
end
- # root "posts#index"
- def root(to, options={})
- default = {path: '', to: to, method: :get, root: true}
- options = default.merge(options)
- @routes << Route.new(options)
- end
-
# Useful for creating API Gateway Resources
def all_paths
results = []
paths = routes.map(&:path)
paths.each do |p|
@@ -139,10 +124,11 @@
@@drawn_router = router
end
def self.clear!
@@drawn_router = nil
+ Jets::Router::Helpers::NamedRoutesHelper.clear!
end
def self.routes
drawn_router.routes
end
@@ -153,16 +139,16 @@
# Output: ["posts", "posts/:id", "posts/:id/edit"]
def self.all_paths
drawn_router.all_paths
end
- def self.routes_help
+ def self.help(routes)
return "Your routes table is empty." if routes.empty?
table = Text::Table.new
- table.head = %w[Verb Path Controller#action]
+ table.head = %w[As Verb Path Controller#action]
routes.each do |route|
- table.rows << [route.method, route.path, route.to]
+ table.rows << [route.as, route.method, route.path, route.to]
end
table
end
def self.all_routes_valid