The path plugin adds support for named paths. Using the path
class method, you can easily create *_path
instance methods for each named path. Those instance methods can then be called if you need to get the path for a form action, link, redirect, or anything else.
Additionally, you can call the path
class method with a class and a block, and it will register the class. You can then call the path
instance method with an instance of that class, and it will execute the block in the context of the route block scope with the arguments provided to path. You can call the url
instance method with the same arguments as the path
method to get the full URL.
Example:
plugin :path path :foo, '/foo' path :bar do |bar| "/bar/#{bar.id}" end path Baz do |baz, *paths| "/baz/#{baz.id}/#{paths.join('/')}" end path Quux do |quux, path| "/quux/#{quux.id}/#{path}" end path 'FooBar', class_name: true do |foobar| "/foobar/#{foobar.id}" end route do |r| r.post 'foo' do r.redirect foo_path # /foo end r.post 'bar' do bar_params = r.params['bar'] if bar_params.is_a?(Hash) bar = Bar.create(bar_params) r.redirect bar_path(bar) # /bar/1 end end r.post 'baz' do baz = Baz[1] r.redirect path(baz, 'c', 'd') # /baz/1/c/d end r.post 'quux' do quux = Quux[1] r.redirect url(quux, '/bar') # http://example.com/quux/1/bar end end
The path class method accepts the following options when not called with a class:
:add_script_name |
Prefix the path generated with SCRIPT_NAME. This defaults to the app’s :add_script_name option. |
:name |
Provide a different name for the method, instead of using |
:relative |
Generate paths relative to the current request instead of absolute paths by prepending an appropriate prefix. This implies :add_script_name. |
:url |
Create a url method in addition to the path method, which will prefix the string generated with the appropriate scheme, host, and port. If true, creates a |
:url_only |
Do not create a path method, just a url method. |
Note that if :add_script_name, :relative, :url, or :url_only is used, the path method will also create a _*_path
private method.
If the path class method is passed a string or symbol as the first argument, and the second argument is a hash with the :class_name option passed, the symbol/string is treated as a class name. This enables the use of class-based paths without forcing autoloads for the related classes. If the plugin is not registering classes by name, this will use the symbol or string to find the related class.
Classes and Modules
Constants
DEFAULT_PORTS | = | {'http' => 80, 'https' => 443}.freeze | ||
VALID_CONSTANT_NAME_REGEXP | = | /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/.freeze |
Regexp for valid constant names, to prevent code execution. |
Public Class methods
Initialize the path classes when loading the plugin. Options:
:by_name |
Register classes by name, which is friendlier when reloading code (defaults to true in development mode) |
# File lib/roda/plugins/path.rb 86 def self.configure(app, opts=OPTS) 87 app.instance_eval do 88 self.opts[:path_class_by_name] = opts.fetch(:by_name, ENV['RACK_ENV'] == 'development') 89 self.opts[:path_classes] ||= {} 90 self.opts[:path_class_methods] ||= {} 91 unless path_block(String) 92 path(String){|str| str} 93 end 94 end 95 end