aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/rails
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2009-10-14 17:24:17 -0700
committerJeremy Kemper <jeremy@bitsweat.net>2009-10-14 17:24:17 -0700
commit5ad0e315e41d73aa5dcd8973f38dd99a5384e5ac (patch)
treec56a5b7dbafee9719b1aabc2fc5da87f17ebf554 /railties/lib/rails
parent4cbd3f050b5db2a1164b1071753d72bea7234ff0 (diff)
parentbf9819f73d74e19052b7b8a7a9885972a27e8876 (diff)
downloadrails-5ad0e315e41d73aa5dcd8973f38dd99a5384e5ac.tar.gz
rails-5ad0e315e41d73aa5dcd8973f38dd99a5384e5ac.tar.bz2
rails-5ad0e315e41d73aa5dcd8973f38dd99a5384e5ac.zip
Merge branch 'master' into arel
Diffstat (limited to 'railties/lib/rails')
-rw-r--r--railties/lib/rails/application.rb10
-rw-r--r--railties/lib/rails/configuration.rb261
-rw-r--r--railties/lib/rails/generators/rails/app/app_generator.rb1
-rw-r--r--railties/lib/rails/generators/rails/app/templates/Gemfile10
-rw-r--r--railties/lib/rails/paths.rb12
5 files changed, 162 insertions, 132 deletions
diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb
index d54120f850..a0e5d6a5a5 100644
--- a/railties/lib/rails/application.rb
+++ b/railties/lib/rails/application.rb
@@ -18,6 +18,10 @@ module Rails
@plugin_loader ||= config.plugin_loader.new(self)
end
+ def root
+ config.root
+ end
+
def routes
ActionController::Routing::Routes
end
@@ -102,7 +106,7 @@ module Rails
# Create tmp directories
initializer :ensure_tmp_directories_exist do
%w(cache pids sessions sockets).each do |dir_to_make|
- FileUtils.mkdir_p(File.join(config.root_path, 'tmp', dir_to_make))
+ FileUtils.mkdir_p(File.join(config.root, 'tmp', dir_to_make))
end
end
@@ -346,7 +350,7 @@ module Rails
# Loads all plugins in <tt>config.plugin_paths</tt>. <tt>plugin_paths</tt>
# defaults to <tt>vendor/plugins</tt> but may also be set to a list of
# paths, such as
- # config.plugin_paths = ["#{RAILS_ROOT}/lib/plugins", "#{RAILS_ROOT}/vendor/plugins"]
+ # config.plugin_paths = ["#{config.root}/lib/plugins", "#{config.root}/vendor/plugins"]
#
# In the default implementation, as each plugin discovered in <tt>plugin_paths</tt> is initialized:
# * its +lib+ directory, if present, is added to the load path (immediately after the applications lib directory)
@@ -394,7 +398,7 @@ module Rails
initializer :load_application_initializers do
if config.gems_dependencies_loaded
- Dir["#{configuration.root_path}/config/initializers/**/*.rb"].sort.each do |initializer|
+ Dir["#{configuration.root}/config/initializers/**/*.rb"].sort.each do |initializer|
load(initializer)
end
end
diff --git a/railties/lib/rails/configuration.rb b/railties/lib/rails/configuration.rb
index 4a70a4800e..322590f108 100644
--- a/railties/lib/rails/configuration.rb
+++ b/railties/lib/rails/configuration.rb
@@ -1,8 +1,11 @@
+require 'rails/plugin/loader'
+require 'rails/plugin/locator'
+
module Rails
class Configuration
- attr_accessor :cache_classes, :load_paths, :eager_load_paths, :framework_paths,
+ attr_accessor :cache_classes, :load_paths,
:load_once_paths, :gems_dependencies_loaded, :after_initialize_blocks,
- :frameworks, :framework_root_path, :root_path, :plugin_paths, :plugins,
+ :frameworks, :framework_root_path, :root, :plugin_paths, :plugins,
:plugin_loader, :plugin_locators, :gems, :loaded_plugins, :reload_plugins,
:i18n, :gems, :whiny_nils, :consider_all_requests_local,
:action_controller, :active_record, :action_view, :active_support,
@@ -13,31 +16,13 @@ module Rails
:eager_load_paths, :dependency_loading, :paths, :serve_static_assets
def initialize
- set_root_path!
-
- @framework_paths = []
@load_once_paths = []
@after_initialize_blocks = []
@loaded_plugins = []
@dependency_loading = true
- @eager_load_paths = default_eager_load_paths
- @load_paths = default_load_paths
- @plugin_paths = default_plugin_paths
- @frameworks = default_frameworks
- @plugin_loader = default_plugin_loader
- @plugin_locators = default_plugin_locators
- @gems = default_gems
- @i18n = default_i18n
- @log_path = default_log_path
- @log_level = default_log_level
- @cache_store = default_cache_store
- @view_path = default_view_path
- @controller_paths = default_controller_paths
- @routes_configuration_file = default_routes_configuration_file
- @database_configuration_file = default_database_configuration_file
- @serve_static_assets = default_serve_static_assets
-
- for framework in default_frameworks
+ @serve_static_assets = true
+
+ for framework in frameworks
self.send("#{framework}=", Rails::OrderedOptions.new)
end
self.active_support = Rails::OrderedOptions.new
@@ -47,38 +32,60 @@ module Rails
@after_initialize_blocks << blk if blk
end
- def set_root_path!
- raise 'RAILS_ROOT is not set' unless defined?(RAILS_ROOT)
- raise 'RAILS_ROOT is not a directory' unless File.directory?(RAILS_ROOT)
+ def root
+ @root ||= begin
+ if defined?(RAILS_ROOT)
+ root = RAILS_ROOT
+ else
+ call_stack = caller.map { |p| p.split(':').first }
+ root_path = call_stack.detect { |p| p !~ %r[railties/lib/rails] }
+ root_path = File.dirname(root_path)
- self.root_path =
- # Pathname is incompatible with Windows, but Windows doesn't have
- # real symlinks so File.expand_path is safe.
- if RUBY_PLATFORM =~ /(:?mswin|mingw)/
- File.expand_path(RAILS_ROOT)
+ while root_path && File.directory?(root_path) && !File.exist?("#{root_path}/config.ru")
+ parent = File.dirname(root_path)
+ root_path = parent != root_path && parent
+ end
- # Otherwise use Pathname#realpath which respects symlinks.
- else
- Pathname.new(RAILS_ROOT).realpath.to_s
+ Object.class_eval("RAILS_ROOT = ''")
+
+ root = File.exist?("#{root_path}/config.ru") ? root_path : Dir.pwd
end
- @paths = Rails::Application::Root.new(root_path)
- @paths.app "app", :load_path => true
- @paths.app.metals "app/metal", :eager_load => true
- @paths.app.models "app/models", :eager_load => true
- @paths.app.controllers "app/controllers", builtin_directories, :eager_load => true
- @paths.app.helpers "app/helpers", :eager_load => true
- @paths.app.services "app/services", :load_path => true
- @paths.lib "lib", :load_path => true
- @paths.vendor "vendor", :load_path => true
- @paths.vendor.plugins "vendor/plugins"
- @paths.tmp "tmp"
- @paths.tmp.cache "tmp/cache"
- @paths.config "config"
- @paths.config.locales "config/locales"
- @paths.config.environments "config/environments", :glob => "#{RAILS_ENV}.rb"
-
- RAILS_ROOT.replace root_path
+ root = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ?
+ Pathname.new(root).expand_path.to_s :
+ Pathname.new(root).realpath.to_s
+
+ # TODO: Remove RAILS_ROOT
+ RAILS_ROOT.replace(root)
+ root
+ end
+ end
+
+ def root=(root)
+ Object.class_eval("RAILS_ROOT = ''") unless defined?(RAILS_ROOT)
+ RAILS_ROOT.replace(root)
+ @root = root
+ end
+
+ def paths
+ @paths ||= begin
+ paths = Rails::Application::Root.new(root)
+ paths.app "app", :load_path => true
+ paths.app.metals "app/metal", :eager_load => true
+ paths.app.models "app/models", :eager_load => true
+ paths.app.controllers "app/controllers", builtin_directories, :eager_load => true
+ paths.app.helpers "app/helpers", :eager_load => true
+ paths.app.services "app/services", :load_path => true
+ paths.lib "lib", :load_path => true
+ paths.vendor "vendor", :load_path => true
+ paths.vendor.plugins "vendor/plugins"
+ paths.tmp "tmp"
+ paths.tmp.cache "tmp/cache"
+ paths.config "config"
+ paths.config.locales "config/locales"
+ paths.config.environments "config/environments", :glob => "#{RAILS_ENV}.rb"
+ paths
+ end
end
# Enable threaded mode. Allows concurrent requests to controller actions and
@@ -105,7 +112,7 @@ module Rails
end
def framework_root_path
- defined?(::RAILS_FRAMEWORK_ROOT) ? ::RAILS_FRAMEWORK_ROOT : "#{root_path}/vendor/rails"
+ defined?(::RAILS_FRAMEWORK_ROOT) ? ::RAILS_FRAMEWORK_ROOT : "#{root}/vendor/rails"
end
def middleware
@@ -121,63 +128,69 @@ module Rails
YAML::load(ERB.new(IO.read(database_configuration_file)).result)
end
- def default_routes_configuration_file
- File.join(root_path, 'config', 'routes.rb')
+ def routes_configuration_file
+ @routes_configuration_file ||= File.join(root, 'config', 'routes.rb')
end
- def default_controller_paths
- paths = [File.join(root_path, 'app', 'controllers')]
- paths.concat builtin_directories
- paths
+ def controller_paths
+ @controller_paths ||= begin
+ paths = [File.join(root, 'app', 'controllers')]
+ paths.concat builtin_directories
+ paths
+ end
end
- def default_cache_store
- if File.exist?("#{root_path}/tmp/cache/")
- [ :file_store, "#{root_path}/tmp/cache/" ]
- else
- :memory_store
+ def cache_store
+ @cache_store ||= begin
+ if File.exist?("#{root}/tmp/cache/")
+ [ :file_store, "#{root}/tmp/cache/" ]
+ else
+ :memory_store
+ end
end
end
- def default_database_configuration_file
- File.join(root_path, 'config', 'database.yml')
+ def database_configuration_file
+ @database_configuration_file ||= File.join(root, 'config', 'database.yml')
end
- def default_view_path
- File.join(root_path, 'app', 'views')
+ def view_path
+ @view_path ||= File.join(root, 'app', 'views')
end
- def default_eager_load_paths
- %w(
+ def eager_load_paths
+ @eager_load_paths ||= %w(
app/metal
app/models
app/controllers
app/helpers
- ).map { |dir| "#{root_path}/#{dir}" }.select { |dir| File.directory?(dir) }
+ ).map { |dir| "#{root}/#{dir}" }.select { |dir| File.directory?(dir) }
end
- def default_load_paths
- paths = []
-
- # Add the old mock paths only if the directories exists
- paths.concat(Dir["#{root_path}/test/mocks/#{RAILS_ENV}"]) if File.exists?("#{root_path}/test/mocks/#{RAILS_ENV}")
-
- # Add the app's controller directory
- paths.concat(Dir["#{root_path}/app/controllers/"])
-
- # Followed by the standard includes.
- paths.concat %w(
- app
- app/metal
- app/models
- app/controllers
- app/helpers
- app/services
- lib
- vendor
- ).map { |dir| "#{root_path}/#{dir}" }.select { |dir| File.directory?(dir) }
-
- paths.concat builtin_directories
+ def load_paths
+ @load_paths ||= begin
+ paths = []
+
+ # Add the old mock paths only if the directories exists
+ paths.concat(Dir["#{root}/test/mocks/#{RAILS_ENV}"]) if File.exists?("#{root}/test/mocks/#{RAILS_ENV}")
+
+ # Add the app's controller directory
+ paths.concat(Dir["#{root}/app/controllers/"])
+
+ # Followed by the standard includes.
+ paths.concat %w(
+ app
+ app/metal
+ app/models
+ app/controllers
+ app/helpers
+ app/services
+ lib
+ vendor
+ ).map { |dir| "#{root}/#{dir}" }.select { |dir| File.directory?(dir) }
+
+ paths.concat builtin_directories
+ end
end
def builtin_directories
@@ -185,48 +198,48 @@ module Rails
(RAILS_ENV == 'development') ? Dir["#{RAILTIES_PATH}/builtin/*/"] : []
end
- def default_log_path
- File.join(root_path, 'log', "#{RAILS_ENV}.log")
+ def log_path
+ @log_path ||= File.join(root, 'log', "#{RAILS_ENV}.log")
end
- def default_log_level
- RAILS_ENV == 'production' ? :info : :debug
+ def log_level
+ @log_level ||= RAILS_ENV == 'production' ? :info : :debug
end
- def default_frameworks
- [ :active_record, :action_controller, :action_view, :action_mailer, :active_resource ]
+ def frameworks
+ @frameworks ||= [ :active_record, :action_controller, :action_view, :action_mailer, :active_resource ]
end
- def default_plugin_paths
- ["#{root_path}/vendor/plugins"]
+ def plugin_paths
+ @plugin_paths ||= ["#{root}/vendor/plugins"]
end
- def default_plugin_loader
- require 'rails/plugin/loader'
- Plugin::Loader
+ def plugin_loader
+ @plugin_loader ||= begin
+ Plugin::Loader
+ end
end
- def default_plugin_locators
- require 'rails/plugin/locator'
- locators = []
- locators << Plugin::GemLocator if defined? Gem
- locators << Plugin::FileSystemLocator
+ def plugin_locators
+ @plugin_locators ||= begin
+ locators = []
+ locators << Plugin::GemLocator if defined? Gem
+ locators << Plugin::FileSystemLocator
+ end
end
- def default_i18n
- i18n = Rails::OrderedOptions.new
- i18n.load_path = []
-
- if File.exist?(File.join(RAILS_ROOT, 'config', 'locales'))
- i18n.load_path << Dir[File.join(RAILS_ROOT, 'config', 'locales', '*.{rb,yml}')]
- i18n.load_path.flatten!
- end
+ def i18n
+ @i18n ||= begin
+ i18n = Rails::OrderedOptions.new
+ i18n.load_path = []
- i18n
- end
+ if File.exist?(File.join(root, 'config', 'locales'))
+ i18n.load_path << Dir[File.join(root, 'config', 'locales', '*.{rb,yml}')]
+ i18n.load_path.flatten!
+ end
- def default_serve_static_assets
- true
+ i18n
+ end
end
# Adds a single Gem dependency to the rails application. By default, it will require
@@ -241,15 +254,15 @@ module Rails
#
# config.gem 'qrp', :version => '0.4.1', :lib => false
def gem(name, options = {})
- @gems << Rails::GemDependency.new(name, options)
+ gems << Rails::GemDependency.new(name, options)
end
- def default_gems
- []
+ def gems
+ @gems ||= []
end
def environment_path
- "#{root_path}/config/environments/#{RAILS_ENV}.rb"
+ "#{root}/config/environments/#{RAILS_ENV}.rb"
end
def reload_plugins?
diff --git a/railties/lib/rails/generators/rails/app/app_generator.rb b/railties/lib/rails/generators/rails/app/app_generator.rb
index 78b4b057ae..bd7af25bd5 100644
--- a/railties/lib/rails/generators/rails/app/app_generator.rb
+++ b/railties/lib/rails/generators/rails/app/app_generator.rb
@@ -54,6 +54,7 @@ module Rails::Generators
copy_file "Rakefile"
copy_file "README"
copy_file "config.ru"
+ template "Gemfile"
end
def create_app_files
diff --git a/railties/lib/rails/generators/rails/app/templates/Gemfile b/railties/lib/rails/generators/rails/app/templates/Gemfile
new file mode 100644
index 0000000000..bcbaa432b7
--- /dev/null
+++ b/railties/lib/rails/generators/rails/app/templates/Gemfile
@@ -0,0 +1,10 @@
+# Gemfile is where you list all of your application's dependencies
+#
+gem "rails", "<%= Rails::VERSION::STRING %>"
+#
+# Bundling edge rails:
+# gem "rails", "<%= Rails::VERSION::STRING %>", :git => "git://github.com/rails/rails.git"
+
+# You can list more dependencies here
+# gem "nokogiri"
+# gem "merb" # ;) \ No newline at end of file
diff --git a/railties/lib/rails/paths.rb b/railties/lib/rails/paths.rb
index 3899b744b0..0f24106353 100644
--- a/railties/lib/rails/paths.rb
+++ b/railties/lib/rails/paths.rb
@@ -19,14 +19,14 @@ module Rails
class Root
include PathParent
- attr_reader :path
+ attr_accessor :path
+
def initialize(path)
- raise unless path.is_a?(String)
+ raise if path.is_a?(Array)
@children = {}
- # TODO: Move logic from set_root_path initializer
- @path = File.expand_path(path)
+ @path = path
@root = self
@all_paths = []
end
@@ -123,8 +123,10 @@ module Rails
end
def paths
+ raise "You need to set a path root" unless @root.path
+
@paths.map do |path|
- path.index('/') == 0 ? path : File.join(@root.path, path)
+ path.index('/') == 0 ? path : File.expand_path(File.join(@root.path, path))
end
end