From c769ad85336713b7e5bdadd57d92a007783f8208 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sat, 12 May 2007 21:12:31 +0000 Subject: Removed deprecated parameters_for_method_reference concept (legacy from before named routes) [DHH] Added record identification with polymorphic routes for ActionController::Base#url_for and ActionView::Base#url_for [DHH] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6729 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/lib/action_controller/base.rb | 14 ++---- .../lib/action_controller/polymorphic_routes.rb | 52 ++++++++++++++++++++++ actionpack/lib/action_controller/routing.rb | 2 + 3 files changed, 58 insertions(+), 10 deletions(-) create mode 100644 actionpack/lib/action_controller/polymorphic_routes.rb (limited to 'actionpack/lib/action_controller') diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb index c1c9e57342..a9d502e6f1 100755 --- a/actionpack/lib/action_controller/base.rb +++ b/actionpack/lib/action_controller/base.rb @@ -560,7 +560,7 @@ module ActionController #:nodoc: when Hash @url.rewrite(rewrite_options(options)) else - raise ArgumentError, "Unrecognized url_for options: #{options.inspect}" + polymorphic_url(options, self) end end @@ -1015,7 +1015,7 @@ module ActionController #:nodoc: # When using redirect_to :back, if there is no referrer, # RedirectBackError will be raised. You may specify some fallback # behavior for this case by rescueing RedirectBackError. - def redirect_to(options = {}, *parameters_for_method_reference) #:doc: + def redirect_to(options = {}) #:doc: case options when %r{^\w+://.*} raise DoubleRenderError if performed? @@ -1031,14 +1031,8 @@ module ActionController #:nodoc: request.env["HTTP_REFERER"] ? redirect_to(request.env["HTTP_REFERER"]) : raise(RedirectBackError) else - if parameters_for_method_reference.empty? - redirect_to(url_for(options)) - response.redirected_to = options - else - # TOOD: Deprecate me! - redirect_to(url_for(options, *parameters_for_method_reference)) - response.redirected_to, response.redirected_to_method_params = options, parameters_for_method_reference - end + redirect_to(url_for(options)) + response.redirected_to = options end end diff --git a/actionpack/lib/action_controller/polymorphic_routes.rb b/actionpack/lib/action_controller/polymorphic_routes.rb new file mode 100644 index 0000000000..eeddc28d60 --- /dev/null +++ b/actionpack/lib/action_controller/polymorphic_routes.rb @@ -0,0 +1,52 @@ +module ActionController + module PolymorphicRoutes + extend self + + def polymorphic_url(record_or_hash, url_writer, options = {}) + record = extract_record(record_or_hash) + + case + when options[:action] == "new" + url_writer.send(action_prefix(options) + RecordIdentifier.singular_class_name(record) + routing_type(options)) + + when record.respond_to?(:new_record?) && record.new_record? + url_writer.send(RecordIdentifier.plural_class_name(record) + routing_type(options)) + + else + url_writer.send( + action_prefix(options) + RecordIdentifier.singular_class_name(record) + routing_type(options), record_or_hash + ) + end + end + + def polymorphic_path(record_or_hash, url_writer) + polymorphic_url(record_or_hash, url_writer, :routing_type => :path) + end + + %w( edit new formatted ).each do |action| + module_eval <<-EOT + def #{action}_polymorphic_url(record_or_hash, url_writer) + polymorphic_url(record_or_hash, url_writer, :action => "#{action}") + end + + def #{action}_polymorphic_path(record_or_hash, url_writer) + polymorphic_url(record_or_hash, url_writer, :action => "#{action}", :routing_type => :path) + end + EOT + end + + + private + def action_prefix(options) + options[:action] ? "#{options[:action]}_" : "" + end + + def routing_type(options) + "_#{options[:routing_type] || "url"}" + end + + def extract_record(record_or_hash) + record_or_hash.is_a?(Hash) ? record_or_hash[:id] : record_or_hash + end + end +end \ No newline at end of file diff --git a/actionpack/lib/action_controller/routing.rb b/actionpack/lib/action_controller/routing.rb index 9bbb51cfbf..49661850ea 100644 --- a/actionpack/lib/action_controller/routing.rb +++ b/actionpack/lib/action_controller/routing.rb @@ -1,5 +1,6 @@ require 'cgi' require 'uri' +require 'action_controller/polymorphic_routes' class Object def to_param @@ -255,6 +256,7 @@ module ActionController # A helper module to hold URL related helpers. module Helpers + include PolymorphicRoutes end class << self -- cgit v1.2.3