Skip to content

Commit

Permalink
Fixed that formatted_polymorphic_route should be able to take the :fo…
Browse files Browse the repository at this point in the history
…rmat as part of a single hash or as the option hash (references #8741)
  • Loading branch information
David Heinemeier Hansson committed Apr 11, 2008
1 parent f46fd6f commit e89093a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
12 changes: 11 additions & 1 deletion actionpack/lib/action_controller/polymorphic_routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def polymorphic_url(record_or_hash_or_array, options = {})
end

record = extract_record(record_or_hash_or_array)
format = (options[:action].to_s == "formatted" and record_or_hash_or_array.pop)
format = extract_format(record_or_hash_or_array, options)
namespace = extract_namespace(record_or_hash_or_array)

args = case record_or_hash_or_array
Expand Down Expand Up @@ -152,6 +152,16 @@ def extract_record(record_or_hash_or_array)
end
end

def extract_format(record_or_hash_or_array, options)
if options[:action].to_s == "formatted" && record_or_hash_or_array.is_a?(Array)
record_or_hash_or_array.pop
elsif options[:format]
options[:format]
else
nil
end
end

def extract_namespace(record_or_hash_or_array)
returning "" do |namespace|
if record_or_hash_or_array.is_a?(Array)
Expand Down
11 changes: 8 additions & 3 deletions actionpack/test/controller/polymorphic_routes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,18 @@ def test_formatted_url_helper
formatted_polymorphic_url([@article, :pdf])
end

# TODO: should this work?
def xtest_format_option
def test_format_option
@article.save
expects(:article_url).with(@article, :format => :pdf)
expects(:article_url).with(@article, :pdf)
polymorphic_url(@article, :format => :pdf)
end

def test_id_and_format_option
@article.save
expects(:article_url).with(:id => @article, :format => :pdf)
polymorphic_url(:id => @article, :format => :pdf)
end

def test_with_nested
@response.save
expects(:article_response_url).with(@article, @response)
Expand Down

0 comments on commit e89093a

Please sign in to comment.