Permalink
Browse files

Final pass at removing the router from a global constant

  • Loading branch information...
1 parent 36fd9ef commit fc4582fb6684ce72f5628629ea7d061659b790f8 Carlhuda committed Feb 26, 2010
@@ -1,5 +1,5 @@
-require "action_controller"
require "rails"
+require "action_controller"
require "action_view/railtie"
require "active_support/core_ext/class/subclasses"
@@ -8,6 +8,8 @@ class Railtie < Rails::Railtie
railtie_name :action_controller
require "action_controller/railties/log_subscriber"
+ require "action_controller/railties/url_helpers"
+
log_subscriber ActionController::Railties::LogSubscriber.new
initializer "action_controller.logger" do
@@ -27,5 +29,9 @@ class Railtie < Rails::Railtie
initializer "action_controller.set_helpers_path" do |app|
ActionController::Base.helpers_path = app.config.paths.app.helpers.to_a
end
+
+ initializer "action_controller.url_helpers" do |app|
+ ActionController::Base.extend ::ActionController::Railtie::UrlHelpers.with(app.routes)
+ end
end
-end
+end
@@ -0,0 +1,14 @@
+module ActionController
+ class Railtie
+ module UrlHelpers
+ def self.with(router)
+ Module.new do
+ define_method(:inherited) do |klass|
+ super
+ klass.send(:include, router.named_url_helpers)
+ end
+ end
+ end
+ end
+ end
+end
@@ -204,7 +204,6 @@ module Routing
autoload :DeprecatedMapper, 'action_dispatch/routing/deprecated_mapper'
autoload :Mapper, 'action_dispatch/routing/mapper'
autoload :Route, 'action_dispatch/routing/route'
- autoload :Routes, 'action_dispatch/routing/routes'
autoload :RouteSet, 'action_dispatch/routing/route_set'
autoload :UrlFor, 'action_dispatch/routing/url_for'
@@ -1,5 +0,0 @@
-# A singleton that stores the current route set
-# ActionDispatch::Routing::Routes = ActionDispatch::Routing::RouteSet.new
-
-# To preserve compatibility with pre-3.0 Rails action_controller/deprecated.rb
-# defines ActionDispatch::Routing::Routes as an alias
@@ -125,7 +125,7 @@ def assert_routing(path, options, defaults={}, extras={}, message=nil)
end
# A helper to make it easier to test different route configurations.
- # This method temporarily replaces ActionDispatch::Routing::Routes
+ # This method temporarily replaces @router
# with a new RouteSet instance.
#
# The new instance is yielded to the passed block. Typically the block
@@ -188,11 +188,9 @@ def reset!
unless defined? @named_routes_configured
# install the named routes in this session instance.
klass = singleton_class
- # ActionDispatch::Routing::Routes.install_helpers(klass)
# the helpers are made protected by default--we make them public for
# easier access during testing and troubleshooting.
- # klass.module_eval { public *ActionDispatch::Routing::Routes.named_routes.helpers }
@named_routes_configured = true
end
end
@@ -8,7 +8,7 @@ module AtomFeedHelper
# Full usage example:
#
# config/routes.rb:
- # ActionDispatch::Routing::Routes.draw do |map|
+ # Basecamp::Application.routes.draw do |map|
# map.resources :posts
# map.root :controller => "posts"
# end
@@ -24,6 +24,12 @@ def create_fixtures(*table_names, &block)
end
end
+class ActionController::TestCase
+ setup do
+ @router = Rails.application.routes
+ end
+end
+
begin
require_library_or_gem 'ruby-debug'
Debugger.start
@@ -0,0 +1,42 @@
+require 'isolation/abstract_unit'
+
+module ApplicationTests
+ class UrlGenerationTest < Test::Unit::TestCase
+ include ActiveSupport::Testing::Isolation
+
+ def app
+ Rails.application
+ end
+
+ test "it works" do
+ boot_rails
+ require "rails"
+ require "action_controller/railtie"
+
+ class MyApp < Rails::Application
+ config.action_controller.session = { :key => "_myapp_session", :secret => "3b7cd727ee24e8444053437c36cc66c4" }
+ end
+
+ MyApp.initialize!
+
+ class ::ApplicationController < ActionController::Base
+ end
+
+ class ::OmgController < ::ApplicationController
+ def index
+ render :text => omg_path
+ end
+ end
+
+ MyApp.routes.draw do
+ match "/" => "omg#index", :as => :omg
+ end
+
+ require 'rack/test'
+ extend Rack::Test::Methods
+
+ get "/"
+ assert_equal "/", last_response.body
+ end
+ end
+end

1 comment on commit fc4582f

Anyway we can make something like "ApplicationController.helper app.routes.named_route_helpers" work?

Please sign in to comment.