This project is archived and is in readonly mode.

#4657 ✓stale
Jury

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

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>

Attachments

Tags

Pages