aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_controller
diff options
context:
space:
mode:
Diffstat (limited to 'actionpack/lib/action_controller')
-rwxr-xr-xactionpack/lib/action_controller/base.rb14
-rw-r--r--actionpack/lib/action_controller/polymorphic_routes.rb52
-rw-r--r--actionpack/lib/action_controller/routing.rb2
3 files changed, 58 insertions, 10 deletions
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 <tt>redirect_to :back</tt>, 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