This project is archived and is in readonly mode.
Remove unnecessary meta programming from polymorphic_routes.rb
Reported by Jury | May 20th, 2010 @ 05:59 AM
polymorphic_routes.rb contains the following meta programming code meant to reduce typing (only ever so slightly) but at the high cost of making this code less obvious to other maintainers:
%w(edit new).each do |action|
module_eval <<-EOT, __FILE__, __LINE__ + 1
def #{action}_polymorphic_url(record_or_hash, options = {}) # def edit_polymorphic_url(record_or_hash, options = {})
polymorphic_url( # polymorphic_url(
record_or_hash, # record_or_hash,
options.merge(:action => "#{action}")) # options.merge(:action => "edit"))
end # end
#
def #{action}_polymorphic_path(record_or_hash, options = {}) # def edit_polymorphic_path(record_or_hash, options = {})
polymorphic_url( # polymorphic_url(
record_or_hash, # record_or_hash,
options.merge(:action => "#{action}", :routing_type => :path)) # options.merge(:action => "edit", :routing_type => :path))
end # end
EOT
end
As this is a fairly trivial bit of code made much more complicated, I've replaced this with actual method declarations:
def edit_polymorphic_url(record_or_hash, options = {})
polymorphic_url(record_or_hash, options.merge(:action => "edit"))
end
def edit_polymorphic_path(record_or_hash, options = {})
polymorphic_url(record_or_hash, options.merge(:action => "edit", :routing_type => :path))
end
def new_polymorphic_url(record_or_hash, options = {})
polymorphic_url(record_or_hash, options.merge(:action => "new"))
end
def new_polymorphic_path(record_or_hash, options = {})
polymorphic_url(record_or_hash, options.merge(:action => "new", :routing_type => :path))
end
Comments and changes to this ticket
-
Jury May 20th, 2010 @ 06:03 AM
- Tag set to patch
-
Jury May 20th, 2010 @ 06:14 AM
- no changes were found...
-
Anil Wadghule May 20th, 2010 @ 06:52 AM
I think comments for the meta programming code are just fine. Those clearly show what code is doing.
-
Damien MATHIEU May 20th, 2010 @ 07:33 AM
I also think the comments makes it explicit enough. There's no need to do code repetition for this here.
-
Keith Tom May 20th, 2010 @ 01:14 PM
+1 also. It's cleaner and I don't see an advantage to keeping the meta programming; maybe if it was for more than just 2 methods...
-
James B. Byrne May 20th, 2010 @ 05:17 PM
I have no idea what Tag cleared means, but I certainly never intended to write more than +1.
-
Rizwan Reza May 21st, 2010 @ 12:48 AM
- no changes were found...
-
Jury May 21st, 2010 @ 05:21 AM
- Tag set to patch
Just adding the patch keyword back since it looks like it got cleared.
-
Evgeniy Dolzhenko June 3rd, 2010 @ 06:45 AM
+1 (same LOC count with direct solution - no win from metaprogramming)
-
Santiago Pastorino February 2nd, 2011 @ 04:51 PM
- State changed from new to open
- Importance changed from to Medium
This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.
Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.
-
Santiago Pastorino February 2nd, 2011 @ 04:51 PM
- State changed from open to stale
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>