From 7db80f87e9c194713c2016820e39af6043ddf8d0 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Tue, 9 Mar 2010 20:50:35 -0600 Subject: Move AC::UrlRewriter onto route set --- .../lib/action_dispatch/routing/route_set.rb | 56 ++++++++++++++++++++++ 1 file changed, 56 insertions(+) (limited to 'actionpack/lib/action_dispatch/routing/route_set.rb') diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index e99e39269b..15c7cd00ba 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -410,6 +410,38 @@ module ActionDispatch Generator.new(options, recall, @set, extras).generate end + RESERVED_OPTIONS = [:anchor, :params, :only_path, :host, :protocol, :port, :trailing_slash, :skip_relative_url_root] + + # TODO: Terrible method name + def rewrite(options) + handle_positional_args(options) + + rewritten_url = "" + + path_segments = options.delete(:_path_segments) + + unless options[:only_path] + rewritten_url << (options[:protocol] || "http") + rewritten_url << "://" unless rewritten_url.match("://") + rewritten_url << rewrite_authentication(options) + + raise "Missing host to link to! Please provide :host parameter or set default_url_options[:host]" unless options[:host] + + rewritten_url << options[:host] + rewritten_url << ":#{options.delete(:port)}" if options.key?(:port) + end + + path_options = options.except(*RESERVED_OPTIONS) + path_options = yield(path_options) if block_given? + path = generate(path_options, path_segments || {}) + + # ROUTES TODO: This can be called directly, so script_name should probably be set in the router + rewritten_url << (options[:trailing_slash] ? path.sub(/\?|\z/) { "/" + $& } : path) + rewritten_url << "##{Rack::Utils.escape(options[:anchor].to_param.to_s)}" if options[:anchor] + + rewritten_url + end + def call(env) @set.call(env) end @@ -435,6 +467,30 @@ module ActionDispatch raise ActionController::RoutingError, "No route matches #{path.inspect}" end + + private + def handle_positional_args(options) + return unless args = options.delete(:_positional_args) + + keys = options.delete(:_positional_keys) + keys -= options.keys if args.size < keys.size - 1 # take format into account + + args = args.zip(keys).inject({}) do |h, (v, k)| + h[k] = v + h + end + + # Tell url_for to skip default_url_options + options.merge!(args) + end + + def rewrite_authentication(options) + if options[:user] && options[:password] + "#{Rack::Utils.escape(options.delete(:user))}:#{Rack::Utils.escape(options.delete(:password))}@" + else + "" + end + end end end end -- cgit v1.2.3 From e38ea982ff8fea5b616297b458ca2c669de0d659 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Tue, 9 Mar 2010 20:57:43 -0600 Subject: Unused RouteSet#url_for is hogging a good method name --- actionpack/lib/action_dispatch/routing/route_set.rb | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing/route_set.rb') diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index 15c7cd00ba..aa1e25aa8b 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -235,31 +235,22 @@ module ActionDispatch named_routes.install(destinations, regenerate_code) end - def url_for - @url_for ||= begin - router = self - Module.new do - extend ActiveSupport::Concern - include UrlFor - - define_method(:_router) { router } - end - end - end - def url_helpers @url_helpers ||= begin router = self Module.new do extend ActiveSupport::Concern - include router.url_for + include UrlFor # ROUTES TODO: install_helpers isn't great... can we make a module with the stuff that # we can include? + # Yes plz - JP included do router.install_helpers(self) end + + define_method(:_router) { router } end end end -- cgit v1.2.3 From 4d2470f7daad8cebd0a69f5ea0509a41af0596b8 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Tue, 9 Mar 2010 21:00:24 -0600 Subject: RouteSet#rewrite => url_for --- actionpack/lib/action_dispatch/routing/route_set.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing/route_set.rb') diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index aa1e25aa8b..2b9fc9e0de 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -403,8 +403,7 @@ module ActionDispatch RESERVED_OPTIONS = [:anchor, :params, :only_path, :host, :protocol, :port, :trailing_slash, :skip_relative_url_root] - # TODO: Terrible method name - def rewrite(options) + def url_for(options) handle_positional_args(options) rewritten_url = "" -- cgit v1.2.3 From 1cc2a61ea655fea246f4c3e22a9e4a494e99d952 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Tue, 9 Mar 2010 21:20:13 -0600 Subject: Allow default_url_options to be set on route set --- actionpack/lib/action_dispatch/routing/route_set.rb | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'actionpack/lib/action_dispatch/routing/route_set.rb') diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index 2b9fc9e0de..26f37161a7 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -186,6 +186,7 @@ module ActionDispatch attr_accessor :routes, :named_routes attr_accessor :disable_clear_and_finalize, :resources_path_names + attr_accessor :default_url_options def self.default_resources_path_names { :new => 'new', :edit => 'edit' } @@ -196,6 +197,7 @@ module ActionDispatch self.named_routes = NamedRouteCollection.new self.resources_path_names = self.class.default_resources_path_names.dup self.controller_namespaces = Set.new + self.default_url_options = {} @disable_clear_and_finalize = false clear! @@ -404,6 +406,8 @@ module ActionDispatch RESERVED_OPTIONS = [:anchor, :params, :only_path, :host, :protocol, :port, :trailing_slash, :skip_relative_url_root] def url_for(options) + options = default_url_options.merge(options || {}) + handle_positional_args(options) rewritten_url = "" -- cgit v1.2.3 From dcd110c724aa5111e67e7264e6bc76ebdda6d50f Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Tue, 9 Mar 2010 21:25:09 -0600 Subject: skip_relative_url_root url_for option is dead --- actionpack/lib/action_dispatch/routing/route_set.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'actionpack/lib/action_dispatch/routing/route_set.rb') diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index 26f37161a7..722be432c7 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -403,7 +403,7 @@ module ActionDispatch Generator.new(options, recall, @set, extras).generate end - RESERVED_OPTIONS = [:anchor, :params, :only_path, :host, :protocol, :port, :trailing_slash, :skip_relative_url_root] + RESERVED_OPTIONS = [:anchor, :params, :only_path, :host, :protocol, :port, :trailing_slash] def url_for(options) options = default_url_options.merge(options || {}) -- cgit v1.2.3