From 74598fe7e9097484c4029e2731e462c6c82836f9 Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Sat, 25 Sep 2010 19:02:14 +0200 Subject: Do not overwrite _railtie method on namespace while creating isolated engine or application. In order to run Engine as standalone application, you will need Rails::Application instance in the same namespace that engine one. It's very important to leave _railtie bound to whatever used "namespace" method first. --- railties/lib/rails/engine.rb | 25 +++++++++++++------------ railties/test/railties/engine_test.rb | 20 ++++++++++++++++++++ 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index 0620b8608e..ababf6a242 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? diff --git a/railties/test/railties/engine_test.rb b/railties/test/railties/engine_test.rb index a9dd7d4c1b..17bffe05e1 100644 --- a/railties/test/railties/engine_test.rb +++ b/railties/test/railties/engine_test.rb @@ -643,5 +643,25 @@ module RailtiesTest Bukkits::Engine.load_seed assert Bukkits::Engine.config.bukkits_seeds_loaded end + + test "using namespace more than once on one module should not overwrite _railtie method" do + @plugin.write "lib/bukkits.rb", <<-RUBY + module AppTemplate + class Engine < ::Rails::Engine + namespace(AppTemplate) + end + end + RUBY + + add_to_config "namespace AppTemplate" + + app_file "config/routes.rb", <<-RUBY + AppTemplate::Application.routes.draw do end + RUBY + + boot_rails + + assert_equal AppTemplate._railtie, AppTemplate::Engine + end end end -- cgit v1.2.3 From 22b11a41cc764bc0f7b0c0f518a5289230428597 Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Sat, 25 Sep 2010 19:22:32 +0200 Subject: Allow mounting engines at '/' Without that commit script_name always become '/', which results in paths like //posts/1 instead of /posts/1 --- .../lib/action_dispatch/routing/route_set.rb | 2 +- actionpack/test/dispatch/prefix_generation_test.rb | 93 ++++++++++++++++++---- 2 files changed, 78 insertions(+), 17 deletions(-) diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index 5d18dfe369..99a3019f3a 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -511,7 +511,7 @@ module ActionDispatch end script_name = options.delete(:script_name) - path = (script_name.blank? ? _generate_prefix(options) : script_name).to_s + path = (script_name.blank? ? _generate_prefix(options) : script_name.chomp('/')).to_s path_options = options.except(*RESERVED_OPTIONS) path_options = yield(path_options) if block_given? diff --git a/actionpack/test/dispatch/prefix_generation_test.rb b/actionpack/test/dispatch/prefix_generation_test.rb index 26d76557dd..18f28deee4 100644 --- a/actionpack/test/dispatch/prefix_generation_test.rb +++ b/actionpack/test/dispatch/prefix_generation_test.rb @@ -1,8 +1,23 @@ require 'abstract_unit' +require 'rack/test' module TestGenerationPrefix + class Post + extend ActiveModel::Naming + + def to_param + "1" + end + + def self.model_name + klass = "Post" + def klass.name; self end + + ActiveModel::Name.new(klass) + end + end + class WithMountedEngine < ActionDispatch::IntegrationTest - require 'rack/test' include Rack::Test::Methods class BlogEngine @@ -55,21 +70,6 @@ module TestGenerationPrefix # force draw RailsApplication.routes - class Post - extend ActiveModel::Naming - - def to_param - "1" - end - - def self.model_name - klass = "Post" - def klass.name; self end - - ActiveModel::Name.new(klass) - end - end - class ::InsideEngineGeneratingController < ActionController::Base include BlogEngine.routes.url_helpers include RailsApplication.routes.mounted_helpers @@ -253,4 +253,65 @@ module TestGenerationPrefix assert_equal "http://www.example.com/awesome/blog/posts/1", path end end + + class EngineMountedAtRoot < ActionDispatch::IntegrationTest + include Rack::Test::Methods + + class BlogEngine + def self.routes + @routes ||= begin + routes = ActionDispatch::Routing::RouteSet.new + routes.draw do + match "/posts/:id", :to => "posts#show", :as => :post + end + + routes + end + end + + def self.call(env) + env['action_dispatch.routes'] = routes + routes.call(env) + end + end + + class RailsApplication + def self.routes + @routes ||= begin + routes = ActionDispatch::Routing::RouteSet.new + routes.draw do + mount BlogEngine => "/" + end + + routes + end + end + + def self.call(env) + env['action_dispatch.routes'] = routes + routes.call(env) + end + end + + # force draw + RailsApplication.routes + + class ::PostsController < ActionController::Base + include BlogEngine.routes.url_helpers + include RailsApplication.routes.mounted_helpers + + def show + render :text => post_path(:id => params[:id]) + end + end + + def app + RailsApplication + end + + test "generating path inside engine" do + get "/posts/1" + assert_equal "/posts/1", last_response.body + end + end end -- cgit v1.2.3 From ec5d846ac6137e60d81257041e4fde82c0480b32 Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Sun, 26 Sep 2010 00:17:06 +0200 Subject: Properly reload routes defined in class definition Sometimes it's easier to define routes inside Engine or Application class definition (e.g. one file applications). The problem with such case is that if there is a plugin that has config/routes.rb file, it will trigger routes reload on application. Since routes definition for application is not in config/routes.rb file routes_reloader will fail to reload application's routes properly. With this commit you can pass routes definition as a block to routes method, which will allow to properly reload it: class MyApp::Application < Rails::Application routes do resources :users end end --- .../lib/action_dispatch/routing/route_set.rb | 1 + railties/lib/rails/application.rb | 21 +++++++++--- railties/lib/rails/engine.rb | 7 +++- railties/test/railties/engine_test.rb | 40 ++++++++++++++++++++++ 4 files changed, 63 insertions(+), 6 deletions(-) diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index 99a3019f3a..32f41934f1 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -1,6 +1,7 @@ require 'rack/mount' require 'forwardable' require 'active_support/core_ext/object/to_query' +require 'active_support/core_ext/hash/slice' module ActionDispatch module Routing diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index 0e85e6d1d5..2db131261c 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -85,13 +85,24 @@ module Rails end def reload_routes! - _routes = self.routes - _routes.disable_clear_and_finalize = true - _routes.clear! + routes_to_reload.each do |_routes, draw_block| + _routes = self.routes + _routes.disable_clear_and_finalize = true + _routes.clear! + _routes.draw(&draw_block) if draw_block + end routes_reloader.paths.each { |path| load(path) } - ActiveSupport.on_load(:action_controller) { _routes.finalize! } + routes_to_reload.each do |_routes, draw_block| + ActiveSupport.on_load(:action_controller) { _routes.finalize! } + end ensure - _routes.disable_clear_and_finalize = false + routes_to_reload.each do |_routes, draw_block| + _routes.disable_clear_and_finalize = false + end + end + + def routes_to_reload + @routes_to_reload ||= {} end def initialize! diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index ababf6a242..aa82a82b19 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -399,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 @@ -447,6 +449,7 @@ module Rails end initializer :add_routing_paths do |app| + app.routes_to_reload[self.routes] = routes_draw_block paths.config.routes.to_a.each do |route| app.routes_reloader.paths.unshift(route) if File.exists?(route) end @@ -499,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/test/railties/engine_test.rb b/railties/test/railties/engine_test.rb index 17bffe05e1..2b9d728b0c 100644 --- a/railties/test/railties/engine_test.rb +++ b/railties/test/railties/engine_test.rb @@ -663,5 +663,45 @@ module RailtiesTest assert_equal AppTemplate._railtie, AppTemplate::Engine end + + test "properly reload routes" do + # when routes are inside application class definition + # they should not be reloaded when engine's routes + # file has changed + add_to_config <<-RUBY + routes do + mount lambda{|env| [200, {}, ["foo"]]} => "/foo" + mount Bukkits::Engine => "/bukkits" + end + RUBY + + FileUtils.rm(File.join(app_path, "config/routes.rb")) + + @plugin.write "config/routes.rb", <<-RUBY + Bukkits::Engine.routes.draw do + mount lambda{|env| [200, {}, ["bar"]]} => "/bar" + end + RUBY + + @plugin.write "lib/bukkits.rb", <<-RUBY + module Bukkits + class Engine < ::Rails::Engine + namespace(Bukkits) + end + end + RUBY + + require 'rack/test' + extend Rack::Test::Methods + + boot_rails + + require "#{rails_root}/config/environment" + get "/foo" + assert_equal "foo", last_response.body + + get "/bukkits/bar" + assert_equal "bar", last_response.body + end end end -- cgit v1.2.3 From 57aa79e6bedeffcff899688546ef0a508948b4a5 Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Tue, 28 Sep 2010 23:42:29 +0200 Subject: Move routes_reloader to a class: RoutesReloader --- railties/lib/rails/application.rb | 24 ++-------------- railties/lib/rails/engine.rb | 2 +- railties/lib/rails/routes_reloader.rb | 54 +++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 23 deletions(-) create mode 100644 railties/lib/rails/routes_reloader.rb diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index 2db131261c..8170be2f65 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -1,6 +1,7 @@ require 'active_support/core_ext/hash/reverse_merge' require 'active_support/file_update_checker' require 'fileutils' +require 'rails/routes_reloader' require 'rails/plugin' require 'rails/engine' @@ -81,28 +82,7 @@ module Rails end def routes_reloader - @routes_reloader ||= ActiveSupport::FileUpdateChecker.new([]){ reload_routes! } - end - - def reload_routes! - routes_to_reload.each do |_routes, draw_block| - _routes = self.routes - _routes.disable_clear_and_finalize = true - _routes.clear! - _routes.draw(&draw_block) if draw_block - end - routes_reloader.paths.each { |path| load(path) } - routes_to_reload.each do |_routes, draw_block| - ActiveSupport.on_load(:action_controller) { _routes.finalize! } - end - ensure - routes_to_reload.each do |_routes, draw_block| - _routes.disable_clear_and_finalize = false - end - end - - def routes_to_reload - @routes_to_reload ||= {} + @routes_reloader ||= Rails::RoutesReloader.new end def initialize! diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index aa82a82b19..9ae235b818 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -449,7 +449,7 @@ module Rails end initializer :add_routing_paths do |app| - app.routes_to_reload[self.routes] = routes_draw_block + 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 diff --git a/railties/lib/rails/routes_reloader.rb b/railties/lib/rails/routes_reloader.rb new file mode 100644 index 0000000000..9cfc38630d --- /dev/null +++ b/railties/lib/rails/routes_reloader.rb @@ -0,0 +1,54 @@ +module Rails + 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 + -- cgit v1.2.3 From f851352318c7468db310e5fbea0d86dc8b731c6d Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Wed, 29 Sep 2010 17:41:30 +0200 Subject: Added config.app_generators to allow configuring application's generators from railties. With config.generators becomes a way to configure generators for current instance only. For example: module Blog class Engine < Rails::Engine config.generators do |g| g.orm :active_record end config.app_generators do |g| g.test_framework :rspec end end end such definition sets :active_record as orm for engine and :rspec as test_framework for application. The values set with app_generators can be overwritten in application using config.generators as you would normally do: module MyApp class Application < Rails::Application config.generators do |g| g.test_framework :test_unit end end end --- activerecord/lib/active_record/railtie.rb | 2 +- railties/lib/rails/application/configuration.rb | 1 + railties/lib/rails/railtie/configuration.rb | 19 ++++++++++-- railties/lib/rails/test_unit/railtie.rb | 2 +- railties/test/railties/engine_test.rb | 39 +++++++++++++++++++++++++ 5 files changed, 58 insertions(+), 5 deletions(-) diff --git a/activerecord/lib/active_record/railtie.rb b/activerecord/lib/active_record/railtie.rb index 94dda4e413..868fd6c3ff 100644 --- a/activerecord/lib/active_record/railtie.rb +++ b/activerecord/lib/active_record/railtie.rb @@ -13,7 +13,7 @@ module ActiveRecord class Railtie < Rails::Railtie config.active_record = ActiveSupport::OrderedOptions.new - config.generators.orm :active_record, :migration => true, + config.app_generators.orm :active_record, :migration => true, :timestamps => true config.app_middleware.insert_after "::ActionDispatch::Callbacks", 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/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 diff --git a/railties/test/railties/engine_test.rb b/railties/test/railties/engine_test.rb index 2b9d728b0c..c75639d740 100644 --- a/railties/test/railties/engine_test.rb +++ b/railties/test/railties/engine_test.rb @@ -703,5 +703,44 @@ module RailtiesTest get "/bukkits/bar" assert_equal "bar", last_response.body end + + test "setting generators for engine and overriding app generator's" do + @plugin.write "lib/bukkits.rb", <<-RUBY + module Bukkits + class Engine < ::Rails::Engine + config.generators do |g| + g.orm :datamapper + g.template_engine :haml + g.test_framework :rspec + end + + config.app_generators do |g| + g.orm :mongoid + g.template_engine :liquid + g.test_framework :shoulda + end + end + end + RUBY + + add_to_config <<-RUBY + config.generators do |g| + g.test_framework :test_unit + end + RUBY + + boot_rails + require "#{rails_root}/config/environment" + + app_generators = Rails.application.config.generators.options[:rails] + assert_equal :mongoid , app_generators[:orm] + assert_equal :liquid , app_generators[:template_engine] + assert_equal :test_unit, app_generators[:test_framework] + + generators = Bukkits::Engine.config.generators.options[:rails] + assert_equal :datamapper, generators[:orm] + assert_equal :haml , generators[:template_engine] + assert_equal :rspec , generators[:test_framework] + end end end -- cgit v1.2.3 From 6648babdedb8cb9494dec69d2f7efac3da1d239c Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Wed, 29 Sep 2010 17:46:59 +0200 Subject: Allow running generators for Engine with usage of other application. After that commit, developers can set ENGINE_PATH in ENGINE/scripts/rails file and load application's ./script/rails (most of the time it will be dummy application used for testing). When running ./script/rails g it will use application to boot up, but then it will use Engine's root and configuration for generators. --- railties/lib/rails/commands.rb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/railties/lib/rails/commands.rb b/railties/lib/rails/commands.rb index 60a93c9848..acabdcac93 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}" -- cgit v1.2.3 From 3049e645e5037cd923d0bad3c41c105dd9d791f8 Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Wed, 29 Sep 2010 20:05:34 +0200 Subject: Moved Rails::RoutesReloader to Rails::Application::RoutesReloader --- railties/lib/rails/application.rb | 4 +- railties/lib/rails/application/routes_reloader.rb | 55 +++++++++++++++++++++++ railties/lib/rails/routes_reloader.rb | 54 ---------------------- 3 files changed, 57 insertions(+), 56 deletions(-) create mode 100644 railties/lib/rails/application/routes_reloader.rb delete mode 100644 railties/lib/rails/routes_reloader.rb diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index 8170be2f65..aafbbc29ee 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -1,7 +1,6 @@ require 'active_support/core_ext/hash/reverse_merge' require 'active_support/file_update_checker' require 'fileutils' -require 'rails/routes_reloader' require 'rails/plugin' require 'rails/engine' @@ -40,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) @@ -82,7 +82,7 @@ module Rails end def routes_reloader - @routes_reloader ||= Rails::RoutesReloader.new + @routes_reloader ||= RoutesReloader.new end def initialize! 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/routes_reloader.rb b/railties/lib/rails/routes_reloader.rb deleted file mode 100644 index 9cfc38630d..0000000000 --- a/railties/lib/rails/routes_reloader.rb +++ /dev/null @@ -1,54 +0,0 @@ -module Rails - 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 - -- cgit v1.2.3 From 9f569c60adb3505cd7ca1723481199bf26619038 Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Wed, 29 Sep 2010 22:31:58 +0200 Subject: Try to guess application's directory while trying to run the server, but only if config.ru is not present in current dir --- railties/lib/rails/commands.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/railties/lib/rails/commands.rb b/railties/lib/rails/commands.rb index acabdcac93..60b9a55d80 100644 --- a/railties/lib/rails/commands.rb +++ b/railties/lib/rails/commands.rb @@ -33,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 -- cgit v1.2.3