diff options
Diffstat (limited to 'railties/lib/rails')
-rw-r--r-- | railties/lib/rails/application.rb | 13 | ||||
-rw-r--r-- | railties/lib/rails/application/configuration.rb | 1 | ||||
-rw-r--r-- | railties/lib/rails/application/routes_reloader.rb | 55 | ||||
-rw-r--r-- | railties/lib/rails/commands.rb | 17 | ||||
-rw-r--r-- | railties/lib/rails/engine.rb | 32 | ||||
-rw-r--r-- | railties/lib/rails/railtie/configuration.rb | 19 | ||||
-rw-r--r-- | railties/lib/rails/test_unit/railtie.rb | 2 |
7 files changed, 110 insertions, 29 deletions
diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index 0e85e6d1d5..aafbbc29ee 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -39,6 +39,7 @@ module Rails autoload :Configuration, 'rails/application/configuration' autoload :Finisher, 'rails/application/finisher' autoload :Railties, 'rails/application/railties' + autoload :RoutesReloader, 'rails/application/routes_reloader' class << self def inherited(base) @@ -81,17 +82,7 @@ module Rails end def routes_reloader - @routes_reloader ||= ActiveSupport::FileUpdateChecker.new([]){ reload_routes! } - end - - def reload_routes! - _routes = self.routes - _routes.disable_clear_and_finalize = true - _routes.clear! - routes_reloader.paths.each { |path| load(path) } - ActiveSupport.on_load(:action_controller) { _routes.finalize! } - ensure - _routes.disable_clear_and_finalize = false + @routes_reloader ||= RoutesReloader.new end def initialize! diff --git a/railties/lib/rails/application/configuration.rb b/railties/lib/rails/application/configuration.rb index a0ecbc0fc8..f902c3ded2 100644 --- a/railties/lib/rails/application/configuration.rb +++ b/railties/lib/rails/application/configuration.rb @@ -24,6 +24,7 @@ module Rails @time_zone = "UTC" @middleware = app_middleware @asset_path = '/' + @generators = app_generators end def asset_path=(value) diff --git a/railties/lib/rails/application/routes_reloader.rb b/railties/lib/rails/application/routes_reloader.rb new file mode 100644 index 0000000000..23b72a0ec6 --- /dev/null +++ b/railties/lib/rails/application/routes_reloader.rb @@ -0,0 +1,55 @@ +module Rails + class Application + class RoutesReloader < ::ActiveSupport::FileUpdateChecker + def initialize + super([]) { reload! } + end + + def blocks + @blocks ||= {} + end + private + def reload! + clear! + load_blocks + load_paths + finalize! + ensure + revert + end + + def clear! + routers.each do |routes| + routes.disable_clear_and_finalize = true + routes.clear! + end + end + + def load_blocks + blocks.each do |routes, block| + routes.draw(&block) if block + end + end + + def load_paths + paths.each { |path| load(path) } + end + + def finalize! + routers.each do |routes| + ActiveSupport.on_load(:action_controller) { routes.finalize! } + end + end + + def revert + routers.each do |routes| + routes.disable_clear_and_finalize = false + end + end + + def routers + blocks.keys + end + end + end +end diff --git a/railties/lib/rails/commands.rb b/railties/lib/rails/commands.rb index 60a93c9848..60b9a55d80 100644 --- a/railties/lib/rails/commands.rb +++ b/railties/lib/rails/commands.rb @@ -11,7 +11,17 @@ command = ARGV.shift command = aliases[command] || command case command -when 'generate', 'destroy', 'plugin', 'benchmarker', 'profiler' +when 'generate', 'destroy', 'plugin' + require APP_PATH + Rails.application.require_environment! + + if defined?(ENGINE_PATH) + engine = Rails.application.railties.engines.find { |r| r.root.to_s == ENGINE_PATH } + Rails.application = engine + end + require "rails/commands/#{command}" + +when 'benchmarker', 'profiler' require APP_PATH Rails.application.require_environment! require "rails/commands/#{command}" @@ -23,8 +33,13 @@ when 'console' Rails::Console.start(Rails.application) when 'server' + # try to guess application's path if there is no config.ru file in current dir + # it allows to run script/rails server from other directories + Dir.chdir(File.expand_path('../../', APP_PATH)) unless File.exists?(File.expand_path("config.ru")) + require 'rails/commands/server' Rails::Server.new.tap { |server| + # we need to require application after the server sets environment require APP_PATH Dir.chdir(Rails.application.root) server.start diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index 0620b8608e..9ae235b818 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -333,21 +333,22 @@ module Rails def namespace(mod) engine_name(generate_railtie_name(mod)) - _railtie = self name = engine_name - mod.singleton_class.instance_eval do - define_method(:_railtie) do - _railtie - end - - define_method(:table_name_prefix) do - "#{name}_" - end - end - self.routes.default_scope = {:module => name} - self.namespaced = true + + unless mod.respond_to?(:_railtie) + _railtie = self + mod.singleton_class.instance_eval do + define_method(:_railtie) do + _railtie + end + + define_method(:table_name_prefix) do + "#{name}_" + end + end + end end def namespaced? @@ -398,8 +399,10 @@ module Rails } end - def routes + def routes(&block) @routes ||= ActionDispatch::Routing::RouteSet.new + self.routes_draw_block = block if block_given? + @routes end def initializers @@ -446,6 +449,7 @@ module Rails end initializer :add_routing_paths do |app| + app.routes_reloader.blocks[routes] = routes_draw_block paths.config.routes.to_a.each do |route| app.routes_reloader.paths.unshift(route) if File.exists?(route) end @@ -498,6 +502,8 @@ module Rails end protected + attr_accessor :routes_draw_block + def find_root_with_flag(flag, default=nil) root_path = self.class.called_from diff --git a/railties/lib/rails/railtie/configuration.rb b/railties/lib/rails/railtie/configuration.rb index f09e3940cc..e0e4324a4a 100644 --- a/railties/lib/rails/railtie/configuration.rb +++ b/railties/lib/rails/railtie/configuration.rb @@ -17,6 +17,19 @@ module Rails @@app_middleware ||= Rails::Configuration::MiddlewareStackProxy.new end + # This allows you to modify application's generators from Railties. + # + # Values set on app_generators will become defaults for applicaiton, unless + # application overwrites them. + def app_generators + @@app_generators ||= Rails::Configuration::Generators.new + if block_given? + yield @@app_generators + else + @@app_generators + end + end + # Holds generators configuration: # # config.generators do |g| @@ -30,11 +43,11 @@ module Rails # config.generators.colorize_logging = false # def generators - @@generators ||= Rails::Configuration::Generators.new + @generators ||= Rails::Configuration::Generators.new if block_given? - yield @@generators + yield @generators else - @@generators + @generators end end diff --git a/railties/lib/rails/test_unit/railtie.rb b/railties/lib/rails/test_unit/railtie.rb index e3fafc4b9d..2b6170ebfb 100644 --- a/railties/lib/rails/test_unit/railtie.rb +++ b/railties/lib/rails/test_unit/railtie.rb @@ -1,6 +1,6 @@ module Rails class TestUnitRailtie < Rails::Railtie - config.generators do |c| + config.app_generators do |c| c.test_framework :test_unit, :fixture => true, :fixture_replacement => nil |