Module: Padrino::Routing::ClassMethods
- Defined in:
- lib/padrino-core/application/routing.rb
Overview
Class methods responsible for enhanced routing for controllers.
Instance Method Summary collapse
-
#absolute_url(*args) ⇒ Object
Returns absolute url.
-
#add_filter(type, &block) ⇒ Object
Adds a filter hook to a request.
-
#after(*args, &block) ⇒ Object
Add an after filter hook.
-
#before(*args, &block) ⇒ Object
Add a before filter hook.
- #compiled_router ⇒ Object
-
#construct_filter(*args, &block) ⇒ Object
Creates a filter to process before/after the matching route.
-
#controller(*args) { ... } ⇒ Object
(also: #controllers)
Method to organize our routes in a better way.
- #deferred_routes ⇒ Object
- #delete(path, *args, &block) ⇒ Object
- #get(path, *args, &block) ⇒ Object
- #head(path, *args, &block) ⇒ Object
- #link(path, *args, &block) ⇒ Object
- #options(path, *args, &block) ⇒ Object
-
#parent(name = nil, options = {}) ⇒ Object
Provides many parents with shallowing.
- #patch(path, *args, &block) ⇒ Object
- #post(path, *args, &block) ⇒ Object
-
#process_path_for_parent_params(path, parent_params) ⇒ Object
Processes the existing path and prepends the ‘parent’ parameters onto the route Used for calculating path in route method.
- #put(path, *args, &block) ⇒ Object
- #rebase_url(url) ⇒ Object
-
#recognize_path(path) ⇒ Symbol, Hash
Recognize a given path.
- #reset_router! ⇒ Object
-
#router ⇒ Object
(also: #urls)
Using PathRouter, for features and configurations.
- #unlink(path, *args, &block) ⇒ Object
-
#url(*args) ⇒ Object
(also: #url_for)
Instance method for url generation.
Instance Method Details
#absolute_url(*args) ⇒ Object
Returns absolute url. By default adds ‘localhost’ before generated url. To change that ‘set :base_url, ’example.com’‘ in your app.
352 353 354 |
# File 'lib/padrino-core/application/routing.rb', line 352 def absolute_url(*args) base_url + url(*args) end |
#add_filter(type, &block) ⇒ Object
Adds a filter hook to a request.
174 175 176 |
# File 'lib/padrino-core/application/routing.rb', line 174 def add_filter(type, &block) filters[type] << block end |
#after(*args, &block) ⇒ Object
Add an after filter hook.
167 168 169 |
# File 'lib/padrino-core/application/routing.rb', line 167 def after(*args, &block) add_filter :after, &(args.empty? ? block : construct_filter(*args, &block)) end |
#before(*args, &block) ⇒ Object
Add a before filter hook.
158 159 160 |
# File 'lib/padrino-core/application/routing.rb', line 158 def before(*args, &block) add_filter :before, &(args.empty? ? block : construct_filter(*args, &block)) end |
#compiled_router ⇒ Object
274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/padrino-core/application/routing.rb', line 274 def compiled_router if @deferred_routes deferred_routes.each do |routes| routes.each do |(route, dest)| route.to(&dest) route.before_filters.flatten! route.after_filters.flatten! end end @deferred_routes = nil end router end |
#construct_filter(*args, &block) ⇒ Object
Creates a filter to process before/after the matching route.
219 220 221 222 223 224 225 226 227 |
# File 'lib/padrino-core/application/routing.rb', line 219 def construct_filter(*args, &block) = args.last.is_a?(Hash) ? args.pop : {} if except = .delete(:except) fail "You cannot use :except with other options specified" unless args.empty? && .empty? except = Array(except) = except.last.is_a?(Hash) ? except.pop : {} end Filter.new(!except, @_controller, , Array(except || args), &block) end |
#controller(*args) { ... } ⇒ Object Also known as: controllers
Method to organize our routes in a better way.
In a controller, before and after filters are scoped and don’t
affect other controllers or the main app.
In a controller, layouts are scoped and don’t affect other
controllers or the main app.
144 145 146 147 148 149 150 |
# File 'lib/padrino-core/application/routing.rb', line 144 def controller(*args, &block) if block_given? (*args) { instance_eval(&block) } else include(*args) if extensions.any? end end |
#deferred_routes ⇒ Object
288 289 290 |
# File 'lib/padrino-core/application/routing.rb', line 288 def deferred_routes @deferred_routes ||= ROUTE_PRIORITY.map{[]} end |
#delete(path, *args, &block) ⇒ Object
366 |
# File 'lib/padrino-core/application/routing.rb', line 366 def delete(path, *args, &block) route 'DELETE', path, *args, &block end |
#get(path, *args, &block) ⇒ Object
356 357 358 359 360 361 362 |
# File 'lib/padrino-core/application/routing.rb', line 356 def get(path, *args, &block) conditions = @conditions.dup route('GET', path, *args, &block) @conditions = conditions route('HEAD', path, *args, &block) end |
#head(path, *args, &block) ⇒ Object
367 |
# File 'lib/padrino-core/application/routing.rb', line 367 def head(path, *args, &block) route 'HEAD', path, *args, &block end |
#link(path, *args, &block) ⇒ Object
370 |
# File 'lib/padrino-core/application/routing.rb', line 370 def link(path, *args, &block) route 'LINK', path, *args, &block end |
#options(path, *args, &block) ⇒ Object
368 |
# File 'lib/padrino-core/application/routing.rb', line 368 def (path, *args, &block) route 'OPTIONS', path, *args, &block end |
#parent(name = nil, options = {}) ⇒ Object
Provides many parents with shallowing.
253 254 255 256 257 258 259 |
# File 'lib/padrino-core/application/routing.rb', line 253 def parent(name = nil, ={}) return super() unless name defaults = { :optional => false, :map => name.to_s } = defaults.merge() @_parent = Array(@_parent) unless @_parent.is_a?(Array) @_parent << Parent.new(name, ) end |
#patch(path, *args, &block) ⇒ Object
369 |
# File 'lib/padrino-core/application/routing.rb', line 369 def patch(path, *args, &block) route 'PATCH', path, *args, &block end |
#post(path, *args, &block) ⇒ Object
365 |
# File 'lib/padrino-core/application/routing.rb', line 365 def post(path, *args, &block) route 'POST', path, *args, &block end |
#process_path_for_parent_params(path, parent_params) ⇒ Object
Processes the existing path and prepends the ‘parent’ parameters onto the route Used for calculating path in route method.
388 389 390 391 392 393 394 395 396 397 |
# File 'lib/padrino-core/application/routing.rb', line 388 def process_path_for_parent_params(path, parent_params) parent_prefix = parent_params.flatten.compact.uniq.map do |param| map = (param.respond_to?(:map) && param.map ? param.map : param.to_s) part = "#{map}/:#{Inflections.singularize(param)}_id/" part = "(#{part})?" if param.respond_to?(:optional) && param.optional? part end [parent_prefix, path].flatten.join("") end |
#put(path, *args, &block) ⇒ Object
364 |
# File 'lib/padrino-core/application/routing.rb', line 364 def put(path, *args, &block) route 'PUT', path, *args, &block end |
#rebase_url(url) ⇒ Object
373 374 375 376 377 378 379 380 381 382 |
# File 'lib/padrino-core/application/routing.rb', line 373 def rebase_url(url) if url.start_with?('/') new_url = '' new_url << conform_uri(ENV['RACK_BASE_URI']) if ENV['RACK_BASE_URI'] new_url << conform_uri(uri_root) if defined?(uri_root) new_url << url else url.empty? ? '/' : url end end |
#recognize_path(path) ⇒ Symbol, Hash
Recognize a given path.
319 320 321 322 |
# File 'lib/padrino-core/application/routing.rb', line 319 def recognize_path(path) responses = @router.recognize_path(path) [responses[0], responses[1]] end |
#reset_router! ⇒ Object
292 293 294 295 |
# File 'lib/padrino-core/application/routing.rb', line 292 def reset_router! @deferred_routes = nil router.reset! end |
#router ⇒ Object Also known as: urls
Using PathRouter, for features and configurations.
268 269 270 271 |
# File 'lib/padrino-core/application/routing.rb', line 268 def router @router ||= PathRouter.new block_given? ? yield(@router) : @router end |
#unlink(path, *args, &block) ⇒ Object
371 |
# File 'lib/padrino-core/application/routing.rb', line 371 def unlink(path, *args, &block) route 'UNLINK', path, *args, &block end |
#url(*args) ⇒ Object Also known as: url_for
Instance method for url generation.
340 341 342 343 344 345 |
# File 'lib/padrino-core/application/routing.rb', line 340 def url(*args) params = args.last.is_a?(Hash) ? args.pop : {} fragment = params.delete(:fragment) || params.delete(:anchor) path = make_path_with_params(args, value_to_param(params)) rebase_url(fragment ? path << '#' << fragment.to_s : path) end |