Created
April 24, 2012 11:56
-
-
Save bogdan/2479082 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "diffbench" | |
$:.unshift "../railties/lib" | |
$:.unshift "../activesupport/lib" | |
require "active_support" | |
require 'rails' | |
class App < Rails::Application | |
# Enable the asset pipeline | |
config.assets.enabled = true | |
self.routes.draw do | |
resources :inboxes do | |
resources :messages do | |
resources :attachments | |
end | |
end | |
namespace :admin do | |
resources :users | |
end | |
scope "/returns/:return" do | |
resources :objects | |
end | |
resources :returns | |
scope "(/optional/:optional_id)" do | |
resources :things | |
end | |
match "/other_optional/(:optional_id)" => "foo#foo", :as => :foo | |
match 'books/*section/:title' => 'books#show', :as => :book | |
end | |
end | |
def url_helpers_module | |
routes = App.routes | |
Module.new do | |
extend ActiveSupport::Concern | |
include ActionDispatch::Routing::UrlFor | |
# Define url_for in the singleton level so one can do: | |
# Rails.application.routes.url_helpers.url_for(args) | |
@_routes = routes | |
class << self | |
delegate :url_for, :optimize_routes_generation?, :url_options, :to => '@_routes' | |
def url_options | |
{} | |
end | |
end | |
# Make named_routes available in the module singleton | |
# as well, so one can do: | |
# Rails.application.routes.url_helpers.posts_path | |
extend routes.named_routes.module | |
# Any class that includes this module will get all | |
# named routes... | |
include routes.named_routes.module | |
# plus a singleton class method called _routes ... | |
included do | |
singleton_class.send(:redefine_method, :_routes) { routes } | |
end | |
# And an instance method _routes. Note that | |
# UrlFor (included in this module) add extra | |
# conveniences for working with @_routes. | |
define_method(:_routes) { @_routes || routes } | |
end | |
end | |
url_helpers = url_helpers_module | |
DiffBench.bm do | |
report "Url helper module builder" do | |
1000.times do | |
url_helpers_module | |
end | |
end | |
report "simple URL generation" do | |
1000.times do | |
url_helpers.inbox_path(1) | |
end | |
end | |
report "URL generation with optional parameters" do | |
1000.times do | |
url_helpers.inbox_message_path(inbox_id: 1, id: 2) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment