Skip to content

Commit

Permalink
Make polymorphic_url work with symbols again and refactor it [#1384 s…
Browse files Browse the repository at this point in the history
…tatus:resolved]

Signed-off-by: Joshua Peek <josh@joshpeek.com>
  • Loading branch information
josevalim authored and josh committed Oct 28, 2009
1 parent f51ac9e commit 427a738
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 42 deletions.
41 changes: 7 additions & 34 deletions actionpack/lib/action_controller/polymorphic_routes.rb
Expand Up @@ -80,9 +80,8 @@ def polymorphic_url(record_or_hash_or_array, options = {})
record_or_hash_or_array = record_or_hash_or_array[0] if record_or_hash_or_array.size == 1
end

record = extract_record(record_or_hash_or_array)
record = record.to_model if record.respond_to?(:to_model)
namespace = extract_namespace(record_or_hash_or_array)
record = extract_record(record_or_hash_or_array)
record = record.to_model if record.respond_to?(:to_model)

args = case record_or_hash_or_array
when Hash; [ record_or_hash_or_array ]
Expand All @@ -105,8 +104,7 @@ def polymorphic_url(record_or_hash_or_array, options = {})
end

args.delete_if {|arg| arg.is_a?(Symbol) || arg.is_a?(String)}

named_route = build_named_route_call(record_or_hash_or_array, namespace, inflection, options)
named_route = build_named_route_call(record_or_hash_or_array, inflection, options)

url_options = options.except(:action, :routing_type)
unless url_options.empty?
Expand Down Expand Up @@ -138,18 +136,6 @@ def #{action}_polymorphic_path(record_or_hash, options = {}) # def edit_p
EOT
end

def formatted_polymorphic_url(record_or_hash, options = {})
ActiveSupport::Deprecation.warn("formatted_polymorphic_url has been deprecated. Please pass :format to the polymorphic_url method instead", caller)
options[:format] = record_or_hash.pop if Array === record_or_hash
polymorphic_url(record_or_hash, options)
end

def formatted_polymorphic_path(record_or_hash, options = {})
ActiveSupport::Deprecation.warn("formatted_polymorphic_path has been deprecated. Please pass :format to the polymorphic_path method instead", caller)
options[:format] = record_or_hash.pop if record_or_hash === Array
polymorphic_url(record_or_hash, options.merge(:routing_type => :path))
end

private
def action_prefix(options)
options[:action] ? "#{options[:action]}_" : ''
Expand All @@ -159,7 +145,7 @@ def routing_type(options)
options[:routing_type] || :url
end

def build_named_route_call(records, namespace, inflection, options = {})
def build_named_route_call(records, inflection, options = {})
unless records.is_a?(Array)
record = extract_record(records)
route = ''
Expand All @@ -169,7 +155,7 @@ def build_named_route_call(records, namespace, inflection, options = {})
if parent.is_a?(Symbol) || parent.is_a?(String)
string << "#{parent}_"
else
string << "#{RecordIdentifier.__send__("plural_class_name", parent)}".singularize
string << RecordIdentifier.__send__("plural_class_name", parent).singularize
string << "_"
end
end
Expand All @@ -178,12 +164,12 @@ def build_named_route_call(records, namespace, inflection, options = {})
if record.is_a?(Symbol) || record.is_a?(String)
route << "#{record}_"
else
route << "#{RecordIdentifier.__send__("plural_class_name", record)}"
route << RecordIdentifier.__send__("plural_class_name", record)
route = route.singularize if inflection == :singular
route << "_"
end

action_prefix(options) + namespace + route + routing_type(options).to_s
action_prefix(options) + route + routing_type(options).to_s
end

def extract_record(record_or_hash_or_array)
Expand All @@ -193,18 +179,5 @@ def extract_record(record_or_hash_or_array)
else record_or_hash_or_array
end
end

# Remove the first symbols from the array and return the url prefix
# implied by those symbols.
def extract_namespace(record_or_hash_or_array)
return "" unless record_or_hash_or_array.is_a?(Array)

namespace_keys = []
while (key = record_or_hash_or_array.first) && key.is_a?(String) || key.is_a?(Symbol)
namespace_keys << record_or_hash_or_array.shift
end

namespace_keys.map {|k| "#{k}_"}.join
end
end
end
14 changes: 6 additions & 8 deletions actionpack/test/activerecord/polymorphic_routes_test.rb
Expand Up @@ -98,14 +98,6 @@ def test_url_helper_with_url_options
end
end

def test_formatted_url_helper_is_deprecated
with_test_routes do
assert_deprecated do
formatted_polymorphic_url([@project, :pdf])
end
end
end

def test_format_option
with_test_routes do
@project.save
Expand Down Expand Up @@ -251,6 +243,12 @@ def test_with_array_containing_single_name
end
end

def test_with_array_containing_symbols
with_test_routes do
assert_equal "http://example.com/series/new", polymorphic_url([:new, :series])
end
end

def test_with_hash
with_test_routes do
@project.save
Expand Down

0 comments on commit 427a738

Please sign in to comment.