lib/roda/plugins/path.rb in roda-3.32.0 vs lib/roda/plugins/path.rb in roda-3.33.0
- old
+ new
@@ -8,11 +8,12 @@
# 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.
+ # 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'
@@ -44,15 +45,15 @@
# r.redirect path(baz, 'c', 'd') # /baz/1/c/d
# end
#
# r.post 'quux' do
# bar = Quux[1]
- # r.redirect path(quux, '/bar') # /quux/1/bar
+ # r.redirect url(quux, '/bar') # http://example.com/quux/1/bar
# end
# end
#
- # The path method accepts the following options when not called with a class:
+ # 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 <tt>*_path</tt>.
# :relative :: Generate paths relative to the current request instead of absolute paths by prepending
@@ -60,11 +61,11 @@
# :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 <tt>*_url</tt>
# method. If a Symbol or String, uses the value as the url method name.
# :url_only :: Do not create a path method, just a url method.
#
- # Note that if :add_script_name, :url, or :url_only is used, the path method will also create a
+ # Note that if :add_script_name, :relative, :url, or :url_only is used, the path method will also create a
# <tt>_*_path</tt> private method.
module Path
DEFAULT_PORTS = {'http' => 80, 'https' => 443}.freeze
# Initialize the path classes when loading the plugin. Options:
@@ -163,18 +164,12 @@
else
"#{name}_url"
end
url_block = lambda do |*a, &blk|
- r = request
- scheme = r.scheme
- port = r.port
- uri = ["#{scheme}://#{r.host}#{":#{port}" unless DEFAULT_PORTS[scheme] == port}"]
- uri << request.script_name.to_s if add_script_name
# Allow calling private _method to get path
- uri << send(_meth, *a, &blk)
- File.join(uri)
+ "#{_base_url}#{request.script_name if add_script_name}#{send(_meth, *a, &blk)}"
end
define_method(url_meth, &url_block)
end
@@ -204,9 +199,24 @@
end
path = send(meth, obj, *args, &block)
path = request.script_name.to_s + path if opts[:add_script_name]
path
+ end
+
+ # Similar to #path, but returns a complete URL.
+ def url(*args, &block)
+ "#{_base_url}#{path(*args, &block)}"
+ end
+
+ private
+
+ # The string to prepend to the path to make the path a URL.
+ def _base_url
+ r = @_request
+ scheme = r.scheme
+ port = r.port
+ "#{scheme}://#{r.host}#{":#{port}" unless DEFAULT_PORTS[scheme] == port}"
end
end
end
register_plugin(:path, Path)