Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #11363 (closed defect: fixed)

Opened 9 months ago

Last modified 9 months ago

[PATCH] polymorphic_path modifies input array

Reported by: thomas.lee Assigned to: core
Priority: normal Milestone: 2.x
Component: ActionPack Version: edge
Severity: normal Keywords: polymorphic url_for
Cc:

Description

polymorphic_path modifies the array passed in to it, resulting in confusion in code like the following:

def my_link(*args)
  url = url_for(args)
  link_to args.last.name, url
end

The call to url_for here will actually remove elements from args. As a result of this, the final call to args.last will have unexpected results.

Attachments

safe-polymorphic-url.patch (1.4 kB) - added by thomas.lee on 03/17/08 07:13:19.
Simplified version of the original patch.

Change History

03/17/08 07:13:19 changed by thomas.lee

  • attachment safe-polymorphic-url.patch added.

Simplified version of the original patch.

03/17/08 07:14:50 changed by thomas.lee

  • component changed from ActiveRecord to ActionPack.

03/17/08 07:24:08 changed by thechrisoshow

Oo that's a nasty gotcha.

+1 - tests pass

03/17/08 11:21:57 changed by lifofifo

respond_to?(:dup) is always better than .is_a?(Array)

03/17/08 13:35:25 changed by thomas.lee

lilofifo: I wish that were the case. Calling #dup blindly on input values creates duplicates of things that shouldn't be duplicated due to the semantics expected by other tests (e.g. ActiveRecord::Base instances, HWIAs). Try it and see: you'll get a bunch of test failures because object_ids don't match up.

Whether the tests are wrong or polymorphic_url is wrong is another matter, but as far as keeping the existing tests happy, this is the cleanest way I can see to do it.

03/17/08 23:45:49 changed by david

  • status changed from new to closed.
  • resolution set to fixed.

(In [9053]) Fixed that polymorphic routes would modify the input array (closes #11363) [thomas.lee]