From e0bdc4f446686a498c3117e27ed8561f5c6d34f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sun, 24 Jan 2010 11:06:06 +0100 Subject: Ensure namespaced controllers in engines work. --- .../lib/action_dispatch/routing/route_set.rb | 28 +++++-------------- railties/lib/rails/engine.rb | 10 +++++++ railties/lib/rails/tasks/routes.rake | 1 + railties/test/plugins/vendored_test.rb | 32 +++++++++++++++++++++- 4 files changed, 49 insertions(+), 22 deletions(-) diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index 660d28dbec..c49ac70562 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -222,19 +222,18 @@ module ActionDispatch end end - attr_accessor :routes, :named_routes - attr_accessor :disable_clear_and_finalize + attr_accessor :routes, :named_routes, :controller_namespaces + attr_accessor :disable_clear_and_finalize, :resources_path_names def self.default_resources_path_names { :new => 'new', :edit => 'edit' } end - attr_accessor :resources_path_names - def initialize self.routes = [] self.named_routes = NamedRouteCollection.new - self.resources_path_names = self.class.default_resources_path_names + self.resources_path_names = self.class.default_resources_path_names.dup + self.controller_namespaces = Set.new @disable_clear_and_finalize = false end @@ -281,32 +280,19 @@ module ActionDispatch def controller_constraints @controller_constraints ||= begin - source = controller_namespaces.map { |ns| "#{Regexp.escape(ns)}/#{CONTROLLER_REGEXP.source}" } + namespaces = controller_namespaces + in_memory_controller_namespaces + source = namespaces.map { |ns| "#{Regexp.escape(ns)}/#{CONTROLLER_REGEXP.source}" } source << CONTROLLER_REGEXP.source Regexp.compile(source.sort.reverse.join('|')) end end - def controller_namespaces + def in_memory_controller_namespaces namespaces = Set.new - - # Find any nested controllers already in memory ActionController::Base.subclasses.each do |klass| controller_name = klass.underscore namespaces << controller_name.split('/')[0...-1].join('/') end - - # TODO: Move this into Railties - if defined?(Rails.application) - # Find namespaces in controllers/ directory - Rails.application.config.controller_paths.each do |load_path| - load_path = File.expand_path(load_path) - Dir["#{load_path}/**/*_controller.rb"].collect do |path| - namespaces << File.dirname(path).sub(/#{load_path}\/?/, '') - end - end - end - namespaces.delete('') namespaces end diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index cc878ac8f2..6f724963b2 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -73,6 +73,16 @@ module Rails end end + initializer :add_routing_namespaces do |app| + config.paths.app.controllers.to_a.each do |load_path| + load_path = File.expand_path(load_path) + Dir["#{load_path}/*/*_controller.rb"].collect do |path| + namespace = File.dirname(path).sub(/#{load_path}\/?/, '') + app.routes.controller_namespaces << namespace unless namespace.empty? + end + end + end + initializer :add_locales do config.i18n.load_path.unshift(*config.paths.config.locales.to_a) end diff --git a/railties/lib/rails/tasks/routes.rake b/railties/lib/rails/tasks/routes.rake index 2395d73b2f..e8da72db4d 100644 --- a/railties/lib/rails/tasks/routes.rake +++ b/railties/lib/rails/tasks/routes.rake @@ -1,5 +1,6 @@ desc 'Print out all defined routes in match order, with names. Target specific controller with CONTROLLER=x.' task :routes => :environment do + Rails::Application.reload_routes! all_routes = ENV['CONTROLLER'] ? ActionController::Routing::Routes.routes.select { |route| route.defaults[:controller] == ENV['CONTROLLER'] } : ActionController::Routing::Routes.routes routes = all_routes.collect do |route| name = ActionController::Routing::Routes.named_routes.routes.index(route).to_s diff --git a/railties/test/plugins/vendored_test.rb b/railties/test/plugins/vendored_test.rb index 6207cd13c1..1fc8766def 100644 --- a/railties/test/plugins/vendored_test.rb +++ b/railties/test/plugins/vendored_test.rb @@ -17,6 +17,10 @@ module PluginsTest require "#{app_path}/config/environment" end + def app + @app ||= Rails.application + end + test "it loads the plugin's init.rb file" do boot_rails assert_equal "loaded", BUKKITS @@ -202,7 +206,6 @@ en: YAML boot_rails - require "#{app_path}/config/environment" assert_equal %W( #{RAILS_FRAMEWORK_ROOT}/activesupport/lib/active_support/locale/en.yml @@ -217,6 +220,33 @@ YAML assert_equal "2", I18n.t(:foo) assert_equal "1", I18n.t(:bar) end + + test "namespaced controllers with namespaced routes" do + @plugin.write "config/routes.rb", <<-RUBY + ActionController::Routing::Routes.draw do + namespace :admin do + match "index", :to => "admin/foo#index" + end + end + RUBY + + @plugin.write "app/controllers/admin/foo_controller.rb", <<-RUBY + class Admin::FooController < ApplicationController + def index + render :text => "Rendered from namespace" + end + end + RUBY + + boot_rails + + require 'rack/test' + extend Rack::Test::Methods + + get "/admin/index" + assert_equal 200, last_response.status + assert_equal "Rendered from namespace", last_response.body + end end class VendoredOrderingTest < Test::Unit::TestCase -- cgit v1.2.3