Class: Jets::Router
- Inherits:
-
Object
- Object
- Jets::Router
- Defined in:
- lib/jets/router.rb,
lib/jets/router/scope.rb
Defined Under Namespace
Classes: Scope
Constant Summary collapse
- @@drawn_router =
nil
Instance Attribute Summary collapse
-
#routes ⇒ Object
readonly
Returns the value of attribute routes.
Class Method Summary collapse
-
.all_paths ⇒ Object
Returns all paths including subpaths.
- .all_routes_valid ⇒ Object
-
.draw ⇒ Object
Class methods.
- .drawn_router ⇒ Object
- .has_controller?(name) ⇒ Boolean
- .invalid_routes ⇒ Object
- .routes ⇒ Object
- .routes_help ⇒ Object
Instance Method Summary collapse
- #add_namespace(path) ⇒ Object
-
#all_paths ⇒ Object
Useful for creating API Gateway Resources.
- #api_mode? ⇒ Boolean
- #create_route(options) ⇒ Object
- #draw(&block) ⇒ Object
-
#initialize ⇒ Router
constructor
A new instance of Router.
- #namespace(ns, &block) ⇒ Object
-
#ordered_routes ⇒ Object
Useful for RouterMatcher.
-
#resources(name) ⇒ Object
resources macro expands to all the routes.
-
#root(to) ⇒ Object
root “posts#index”.
- #scope(options = {}) ⇒ Object
Constructor Details
#initialize ⇒ Router
Returns a new instance of Router.
8 9 10 |
# File 'lib/jets/router.rb', line 8 def initialize @routes = [] end |
Instance Attribute Details
#routes ⇒ Object (readonly)
Returns the value of attribute routes.
7 8 9 |
# File 'lib/jets/router.rb', line 7 def routes @routes end |
Class Method Details
.all_paths ⇒ Object
Returns all paths including subpaths. Example: Input: [“posts/:id/edit”] Output: [“posts”, “posts/:id”, “posts/:id/edit”]
141 142 143 |
# File 'lib/jets/router.rb', line 141 def self.all_paths drawn_router.all_paths end |
.all_routes_valid ⇒ Object
156 157 158 |
# File 'lib/jets/router.rb', line 156 def self.all_routes_valid invalid_routes.empty? end |
.draw ⇒ Object
Class methods
121 122 123 |
# File 'lib/jets/router.rb', line 121 def self.draw drawn_router end |
.drawn_router ⇒ Object
126 127 128 129 130 131 |
# File 'lib/jets/router.rb', line 126 def self.drawn_router return @@drawn_router if @@drawn_router router = Jets.application.routes @@drawn_router = router end |
.has_controller?(name) ⇒ Boolean
116 117 118 |
# File 'lib/jets/router.rb', line 116 def self.has_controller?(name) routes.detect { |r| r.controller_name == name } end |
.invalid_routes ⇒ Object
160 161 162 |
# File 'lib/jets/router.rb', line 160 def self.invalid_routes routes.select { |r| !r.valid? } end |
.routes ⇒ Object
133 134 135 |
# File 'lib/jets/router.rb', line 133 def self.routes drawn_router.routes end |
.routes_help ⇒ Object
145 146 147 148 149 150 151 152 153 154 |
# File 'lib/jets/router.rb', line 145 def self.routes_help return "Your routes table is empty." if routes.empty? table = Text::Table.new table.head = %w[Verb Path Controller#action] routes.each do |route| table.rows << [route.method, route.path, route.to] end table end |
Instance Method Details
#add_namespace(path) ⇒ Object
31 32 33 34 35 36 |
# File 'lib/jets/router.rb', line 31 def add_namespace(path) return path unless @scope ns = @scope.full_namespace return path unless ns "#{ns}/#{path}" end |
#all_paths ⇒ Object
Useful for creating API Gateway Resources
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/jets/router.rb', line 83 def all_paths results = [] paths = routes.map(&:path) paths.each do |p| sub_paths = [] parts = p.split('/') until parts.empty? parts.pop sub_path = parts.join('/') sub_paths << sub_path unless sub_path == '' end results += sub_paths end @all_paths = (results + paths).sort.uniq end |
#api_mode? ⇒ Boolean
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/jets/router.rb', line 63 def api_mode? if Jets.config.key?(:api_mode) || Jets.config.key?(:api_generator) puts <<~EOL.colorize(:yellow) DEPRECATED: Jets.config.api_generator Instead, please update your config/application.rb to use: Jets.config.mode = 'api' You can also run: jets upgrade EOL end api_mode = Jets.config.mode == 'api' || Jets.config.api_mode || Jets.config.api_generator api_mode end |
#create_route(options) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/jets/router.rb', line 23 def create_route() # 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 [:path] = add_namespace([:path]) @routes << Route.new() end |
#draw(&block) ⇒ Object
12 13 14 |
# File 'lib/jets/router.rb', line 12 def draw(&block) instance_eval(&block) end |
#namespace(ns, &block) ⇒ Object
38 39 40 |
# File 'lib/jets/router.rb', line 38 def namespace(ns, &block) scope(namespace: ns, &block) end |
#ordered_routes ⇒ Object
Useful for RouterMatcher
Precedence:
-
Routes with no captures get highest precedence: posts/new
-
Then consider the routes with captures: post/:id
-
Last consider the routes with wildcards: *catchall
Within these 2 groups we consider the routes with the longest path first since posts/:id and posts/:id/edit can both match.
108 109 110 111 112 113 114 |
# File 'lib/jets/router.rb', line 108 def ordered_routes length = Proc.new { |r| r.path.length * -1 } capture_routes = routes.select { |r| r.path.include?(':') }.sort_by(&length) wildcard_routes = routes.select { |r| r.path.include?('*') }.sort_by(&length) simple_routes = (routes - capture_routes - wildcard_routes).sort_by(&length) simple_routes + capture_routes + wildcard_routes end |
#resources(name) ⇒ Object
resources macro expands to all the routes
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/jets/router.rb', line 51 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" end |