Module: Tennpipes::Routing::ClassMethods
- Defined in:
- lib/tennpipes-base/application/routing.rb
Overview
Class methods responsible for enhanced routing for controllers.
Instance Method Summary collapse
-
#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
#add_filter(type, &block) ⇒ Object
Adds a filter hook to a request.
174 175 176 |
# File 'lib/tennpipes-base/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/tennpipes-base/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/tennpipes-base/application/routing.rb', line 158 def before(*args, &block) add_filter :before, &(args.empty? ? block : construct_filter(*args, &block)) end |
#compiled_router ⇒ Object
273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/tennpipes-base/application/routing.rb', line 273 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 |
# File 'lib/tennpipes-base/application/routing.rb', line 219 def construct_filter(*args, &block) = args. if except = .delete(:except) fail "You cannot use :except with other options specified" unless args.empty? && .empty? = Array(except). 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/tennpipes-base/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
287 288 289 |
# File 'lib/tennpipes-base/application/routing.rb', line 287 def deferred_routes @deferred_routes ||= ROUTE_PRIORITY.map{[]} end |
#delete(path, *args, &block) ⇒ Object
357 |
# File 'lib/tennpipes-base/application/routing.rb', line 357 def delete(path, *args, &block) route 'DELETE', path, *args, &block end |
#get(path, *args, &block) ⇒ Object
347 348 349 350 351 352 353 |
# File 'lib/tennpipes-base/application/routing.rb', line 347 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
358 |
# File 'lib/tennpipes-base/application/routing.rb', line 358 def head(path, *args, &block) route 'HEAD', path, *args, &block end |
#link(path, *args, &block) ⇒ Object
361 |
# File 'lib/tennpipes-base/application/routing.rb', line 361 def link(path, *args, &block) route 'LINK', path, *args, &block end |
#options(path, *args, &block) ⇒ Object
359 |
# File 'lib/tennpipes-base/application/routing.rb', line 359 def (path, *args, &block) route 'OPTIONS', path, *args, &block end |
#parent(name = nil, options = {}) ⇒ Object
Provides many parents with shallowing.
252 253 254 255 256 257 258 |
# File 'lib/tennpipes-base/application/routing.rb', line 252 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
360 |
# File 'lib/tennpipes-base/application/routing.rb', line 360 def patch(path, *args, &block) route 'PATCH', path, *args, &block end |
#post(path, *args, &block) ⇒ Object
356 |
# File 'lib/tennpipes-base/application/routing.rb', line 356 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.
379 380 381 382 383 384 385 386 387 388 |
# File 'lib/tennpipes-base/application/routing.rb', line 379 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}/:#{param.to_s.singularize}_id/" part = "(#{part})?" if param.respond_to?(:optional) && param.optional? part end [parent_prefix, path].flatten.join("") end |
#put(path, *args, &block) ⇒ Object
355 |
# File 'lib/tennpipes-base/application/routing.rb', line 355 def put(path, *args, &block) route 'PUT', path, *args, &block end |
#rebase_url(url) ⇒ Object
364 365 366 367 368 369 370 371 372 373 |
# File 'lib/tennpipes-base/application/routing.rb', line 364 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.blank? ? '/' : url end end |
#recognize_path(path) ⇒ Symbol, Hash
Recognize a given path.
318 319 320 321 |
# File 'lib/tennpipes-base/application/routing.rb', line 318 def recognize_path(path) responses = @router.recognize_path(path) [responses[0], responses[1]] end |
#reset_router! ⇒ Object
291 292 293 294 |
# File 'lib/tennpipes-base/application/routing.rb', line 291 def reset_router! @deferred_routes = nil router.reset! end |
#router ⇒ Object Also known as: urls
Using PathRouter, for features and configurations.
267 268 269 270 |
# File 'lib/tennpipes-base/application/routing.rb', line 267 def router @router ||= PathRouter.new block_given? ? yield(@router) : @router end |
#unlink(path, *args, &block) ⇒ Object
362 |
# File 'lib/tennpipes-base/application/routing.rb', line 362 def unlink(path, *args, &block) route 'UNLINK', path, *args, &block end |
#url(*args) ⇒ Object Also known as: url_for
Instance method for url generation.
339 340 341 342 343 344 |
# File 'lib/tennpipes-base/application/routing.rb', line 339 def url(*args) params = args. fragment = params.delete(:fragment) || params.delete(:anchor) path = make_path_with_params(args, value_to_param(params.symbolize_keys)) rebase_url(fragment ? path << '#' << fragment.to_s : path) end |