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 f5f7c40 commit 7ba8025
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 21 deletions.
27 changes: 6 additions & 21 deletions actionpack/lib/action_controller/polymorphic_routes.rb
Expand Up @@ -76,8 +76,7 @@ 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)
namespace = extract_namespace(record_or_hash_or_array)
record = extract_record(record_or_hash_or_array)

args = case record_or_hash_or_array
when Hash; [ record_or_hash_or_array ]
Expand All @@ -98,8 +97,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 @@ -153,7 +151,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 @@ -163,7 +161,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 @@ -172,12 +170,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 @@ -187,18 +185,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
4 changes: 4 additions & 0 deletions actionpack/test/controller/polymorphic_routes_test.rb
Expand Up @@ -290,4 +290,8 @@ def test_with_array_containing_single_name_irregular_plural
polymorphic_url([:taxes])
end

def test_with_array_containing_symbols
expects(:new_article_url).with()
polymorphic_url([:new, :article])
end
end

0 comments on commit 7ba8025

Please sign in to comment.