2010-02-25 18:05:10 -05:00
|
|
|
module ActionController
|
2012-03-06 14:34:14 -05:00
|
|
|
# Includes +url_for+ into the host class. The class has to provide a +RouteSet+ by implementing
|
|
|
|
# the <tt>_routes</tt> method. Otherwise, an exception will be raised.
|
|
|
|
#
|
|
|
|
# In addition to <tt>AbstractController::UrlFor</tt>, this module accesses the HTTP layer to define
|
|
|
|
# url options like the +host+. In order to do so, this module requires the host class
|
|
|
|
# to implement +env+ and +request+, which need to be a Rack-compatible.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# class RootUrl
|
|
|
|
# include ActionController::UrlFor
|
|
|
|
# include Rails.application.routes.url_helpers
|
|
|
|
#
|
|
|
|
# delegate :env, :request, :to => :controller
|
|
|
|
#
|
|
|
|
# def initialize(controller)
|
|
|
|
# @controller = controller
|
|
|
|
# @url = root_path # named route from the application.
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
2010-02-25 18:05:10 -05:00
|
|
|
module UrlFor
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2010-08-03 13:36:53 -04:00
|
|
|
include AbstractController::UrlFor
|
2010-02-25 18:05:10 -05:00
|
|
|
|
2010-02-26 18:20:41 -05:00
|
|
|
def url_options
|
2012-03-02 12:47:15 -05:00
|
|
|
@_url_options ||= super.reverse_merge(
|
|
|
|
:host => request.host,
|
|
|
|
:port => request.optional_port,
|
|
|
|
:protocol => request.protocol,
|
|
|
|
:_path_segments => request.symbolized_path_parameters
|
|
|
|
).freeze
|
2010-09-27 15:14:18 -04:00
|
|
|
|
2012-03-02 12:47:15 -05:00
|
|
|
if _routes.equal?(env["action_dispatch.routes"])
|
|
|
|
@_url_options.dup.tap do |options|
|
|
|
|
options[:script_name] = request.script_name.dup
|
|
|
|
options.freeze
|
2010-09-27 09:11:19 -04:00
|
|
|
end
|
2012-03-02 12:47:15 -05:00
|
|
|
else
|
|
|
|
@_url_options
|
2010-09-27 09:11:19 -04:00
|
|
|
end
|
2010-02-25 18:05:10 -05:00
|
|
|
end
|
|
|
|
end
|
2010-07-01 18:29:20 -04:00
|
|
|
end
|