Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Remove unnecessary meta programming from polymorphic_routes.rb

#4657

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

Reported by Jury · May 20th, 2010 @ 05:59 AM

State: stale
Milestone: none
Assigned to: nobody
Importance: Medium

Activity

  1. Jury
    Jury
    • Tag set to patch

    May 20th, 2010 @ 06:03 AM

  2. Jury
  3. Anil Wadghule
    Anil Wadghule

    I think comments for the meta programming code are just fine. Those clearly show what code is doing.

    May 20th, 2010 @ 06:52 AM

  4. Damien MATHIEU
    Damien MATHIEU

    I also think the comments makes it explicit enough. There's no need to do code repetition for this here.

    May 20th, 2010 @ 07:33 AM

  5. Sam Pohlenz
    Sam Pohlenz

    +1 for this patch. The expanded methods are much clearer IMO.

    May 20th, 2010 @ 07:44 AM

  6. Keith Tom
    Keith Tom

    +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...

    May 20th, 2010 @ 01:14 PM

  7. xds2000
    xds2000

    +1,with ruby style,this is cleaner and easy maintaining code.

    May 20th, 2010 @ 02:02 PM

  8. James B. Byrne
  9. James B. Byrne
    James B. Byrne

    I have no idea what Tag cleared means, but I certainly never intended to write more than +1.

    May 20th, 2010 @ 05:17 PM

  10. Josh Nesbitt
    Josh Nesbitt

    +1, Much cleaner considering the nature of the methods.

    May 20th, 2010 @ 05:58 PM

  11. Rizwan Reza
    Rizwan Reza
    • No changes were found…

    May 21st, 2010 @ 12:48 AM

  12. Jury
    Jury
    • Tag set to patch

    Just adding the patch keyword back since it looks like it got cleared.

    May 21st, 2010 @ 05:21 AM

  13. Evgeniy Dolzhenko
    Evgeniy Dolzhenko

    +1 (same LOC count with direct solution - no win from metaprogramming)

    June 3rd, 2010 @ 06:45 AM

  14. Santiago Pastorino
    Santiago Pastorino
    • 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.

    February 2nd, 2011 @ 04:51 PM

  15. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 04:51 PM