Skip to content

Commit

Permalink
Allow polymorphic_url helper to take url options. [#880 state:resolved]
Browse files Browse the repository at this point in the history
All *_polymorphic_url, *_polymorphic_path helpers can now accept
an options hash which will be passed on to the named route
making it possible to generate polymorphic routes with additional
url parameters.

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information
tarmo authored and lifo committed Aug 21, 2008
1 parent 654c412 commit 98fb161
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
2 changes: 2 additions & 0 deletions actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*Edge*

* Allow polymorphic_url helper to take url options. #880 [Tarmo Tänav]

* Switched integration test runner to use Rack processor instead of CGI [Josh Peek]

* Made AbstractRequest.if_modified_sense return nil if the header could not be parsed [Jamis Buck]
Expand Down
16 changes: 11 additions & 5 deletions actionpack/lib/action_controller/polymorphic_routes.rb
Expand Up @@ -102,6 +102,12 @@ def polymorphic_url(record_or_hash_or_array, options = {})
args << format if format

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

url_options = options.except(:action, :routing_type, :format)
unless url_options.empty?
args.last.kind_of?(Hash) ? args.last.merge!(url_options) : args << url_options
end

send!(named_route, *args)
end

Expand All @@ -114,19 +120,19 @@ def polymorphic_path(record_or_hash_or_array, options = {})

%w(edit new formatted).each do |action|
module_eval <<-EOT, __FILE__, __LINE__
def #{action}_polymorphic_url(record_or_hash)
polymorphic_url(record_or_hash, :action => "#{action}")
def #{action}_polymorphic_url(record_or_hash, options = {})
polymorphic_url(record_or_hash, options.merge(:action => "#{action}"))
end
def #{action}_polymorphic_path(record_or_hash)
polymorphic_url(record_or_hash, :action => "#{action}", :routing_type => :path)
def #{action}_polymorphic_path(record_or_hash, options = {})
polymorphic_url(record_or_hash, options.merge(:action => "#{action}", :routing_type => :path))
end
EOT
end

private
def action_prefix(options)
options[:action] ? "#{options[:action]}_" : ""
options[:action] ? "#{options[:action]}_" : options[:format] ? "formatted_" : ""
end

def routing_type(options)
Expand Down
22 changes: 20 additions & 2 deletions actionpack/test/controller/polymorphic_routes_test.rb
Expand Up @@ -60,17 +60,35 @@ def test_url_helper_prefixed_with_edit
edit_polymorphic_url(@article)
end

def test_url_helper_prefixed_with_edit_with_url_options
@article.save
expects(:edit_article_url).with(@article, :param1 => '10')
edit_polymorphic_url(@article, :param1 => '10')
end

def test_url_helper_with_url_options
@article.save
expects(:article_url).with(@article, :param1 => '10')
polymorphic_url(@article, :param1 => '10')
end

def test_formatted_url_helper
expects(:formatted_article_url).with(@article, :pdf)
formatted_polymorphic_url([@article, :pdf])
end

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

def test_format_option_with_url_options
@article.save
expects(:formatted_article_url).with(@article, :pdf, :param1 => '10')
polymorphic_url(@article, :format => :pdf, :param1 => '10')
end

def test_id_and_format_option
@article.save
expects(:article_url).with(:id => @article, :format => :pdf)
Expand Down Expand Up @@ -147,7 +165,7 @@ def test_nesting_with_array_containing_singleton_resource_and_format
def test_nesting_with_array_containing_singleton_resource_and_format_option
@tag = Tag.new
@tag.save
expects(:article_response_tag_url).with(@article, @tag, :pdf)
expects(:formatted_article_response_tag_url).with(@article, @tag, :pdf)
polymorphic_url([@article, :response, @tag], :format => :pdf)
end

Expand Down

0 comments on commit 98fb161

Please sign in to comment.