diff options
author | Rajinder Yadav <devguy.ca@gmail.com> | 2010-10-15 22:19:39 -0400 |
---|---|---|
committer | Rajinder Yadav <devguy.ca@gmail.com> | 2010-10-15 22:19:39 -0400 |
commit | 62eb00a62e3868c3500372def52968b919332b51 (patch) | |
tree | 8497f517402c19eae1fea22943565cbf11496ef7 /railties/lib | |
parent | 018bf0f5756ef3887815ea46d6671dc1d3b526cf (diff) | |
parent | cffae06a4f1b7aac86a7e99cfb244890f837f976 (diff) | |
download | rails-62eb00a62e3868c3500372def52968b919332b51.tar.gz rails-62eb00a62e3868c3500372def52968b919332b51.tar.bz2 rails-62eb00a62e3868c3500372def52968b919332b51.zip |
Merge branch 'master' of git://github.com/lifo/docrails
Diffstat (limited to 'railties/lib')
24 files changed, 429 insertions, 233 deletions
diff --git a/railties/lib/rails.rb b/railties/lib/rails.rb index 3663910281..cca0891835 100644 --- a/railties/lib/rails.rb +++ b/railties/lib/rails.rb @@ -92,7 +92,7 @@ module Rails end def public_path - application && application.paths.public.to_a.first + application && application.paths["public"].first end end end diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index 0e85e6d1d5..d13356ab4d 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) @@ -71,7 +72,7 @@ module Rails end def require_environment! #:nodoc: - environment = paths.config.environment.to_a.first + environment = paths["config/environment"].existent.first require environment if environment end @@ -80,18 +81,12 @@ module Rails super end - def routes_reloader - @routes_reloader ||= ActiveSupport::FileUpdateChecker.new([]){ reload_routes! } + def reload_routes! + routes_reloader.reload! 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 + def routes_reloader + @routes_reloader ||= RoutesReloader.new end def initialize! @@ -133,10 +128,9 @@ module Rails end def initializers - initializers = Bootstrap.initializers_for(self) - initializers += super - initializers += Finisher.initializers_for(self) - initializers + Bootstrap.initializers_for(self) + + super + + Finisher.initializers_for(self) end def config @@ -150,8 +144,8 @@ module Rails rack_cache = config.action_controller.perform_caching && config.action_dispatch.rack_cache require "action_dispatch/http/rack_cache" if rack_cache + middleware.use ::Rack::Cache, rack_cache if rack_cache - middleware.use ::Rack::Cache, rack_cache if rack_cache middleware.use ::ActionDispatch::Static, config.static_asset_paths if config.serve_static_assets middleware.use ::Rack::Lock if !config.allow_concurrency middleware.use ::Rack::Runtime @@ -170,6 +164,8 @@ module Rails middleware.use ::ActionDispatch::ParamsParser middleware.use ::Rack::MethodOverride middleware.use ::ActionDispatch::Head + middleware.use ::Rack::ConditionalGet + middleware.use ::Rack::ETag, "no-cache" middleware.use ::ActionDispatch::BestStandardsSupport, config.action_dispatch.best_standards_support if config.action_dispatch.best_standards_support end end diff --git a/railties/lib/rails/application/bootstrap.rb b/railties/lib/rails/application/bootstrap.rb index e39b3bc705..213aa0768a 100644 --- a/railties/lib/rails/application/bootstrap.rb +++ b/railties/lib/rails/application/bootstrap.rb @@ -23,7 +23,7 @@ module Rails # Initialize the logger early in the stack in case we need to log some deprecation. initializer :initialize_logger do Rails.logger ||= config.logger || begin - path = config.paths.log.to_a.first + path = config.paths["log"].first logger = ActiveSupport::BufferedLogger.new(path) logger.level = ActiveSupport::BufferedLogger.const_get(config.log_level.to_s.upcase) logger.auto_flushing = false if Rails.env.production? diff --git a/railties/lib/rails/application/configuration.rb b/railties/lib/rails/application/configuration.rb index a0ecbc0fc8..3505388479 100644 --- a/railties/lib/rails/application/configuration.rb +++ b/railties/lib/rails/application/configuration.rb @@ -9,7 +9,7 @@ module Rails :filter_parameters, :log_level, :logger, :preload_frameworks, :reload_plugins, :secret_token, :serve_static_assets, :session_options, - :time_zone, :whiny_nils + :time_zone, :whiny_nils, :helpers_paths def initialize(*) super @@ -17,6 +17,7 @@ module Rails @allow_concurrency = false @consider_all_requests_local = false @filter_parameters = [] + @helpers_paths = [] @dependency_loading = true @serve_static_assets = true @session_store = :cookie_store @@ -24,6 +25,7 @@ module Rails @time_zone = "UTC" @middleware = app_middleware @asset_path = '/' + @generators = app_generators end def asset_path=(value) @@ -59,13 +61,12 @@ module Rails def paths @paths ||= begin paths = super - paths.config.database "config/database.yml" - paths.config.environment "config/environment.rb" - paths.lib.templates "lib/templates" - paths.log "log/#{Rails.env}.log" - paths.tmp "tmp" - paths.tmp.cache "tmp/cache" - + paths.add "config/database", :with => "config/database.yml" + paths.add "config/environment", :with => "config/environment.rb" + paths.add "lib/templates" + paths.add "log", :with => "log/#{Rails.env}.log" + paths.add "tmp" + paths.add "tmp/cache" paths end end @@ -87,7 +88,7 @@ module Rails # YAML::load. def database_configuration require 'erb' - YAML::load(ERB.new(IO.read(paths.config.database.to_a.first)).result) + YAML::load(ERB.new(IO.read(paths["config/database"].first)).result) end def cache_store diff --git a/railties/lib/rails/application/finisher.rb b/railties/lib/rails/application/finisher.rb index b95df467c7..e3342be7ee 100644 --- a/railties/lib/rails/application/finisher.rb +++ b/railties/lib/rails/application/finisher.rb @@ -4,7 +4,7 @@ module Rails include Initializable initializer :add_generator_templates do - config.generators.templates.unshift(*paths.lib.templates.to_a) + config.generators.templates.unshift(*paths["lib/templates"].existent) end initializer :ensure_autoload_once_paths_as_subset do diff --git a/railties/lib/rails/application/routes_reloader.rb b/railties/lib/rails/application/routes_reloader.rb new file mode 100644 index 0000000000..1d1f5e1b06 --- /dev/null +++ b/railties/lib/rails/application/routes_reloader.rb @@ -0,0 +1,45 @@ +module Rails + class Application + class RoutesReloader < ::ActiveSupport::FileUpdateChecker + attr_reader :route_sets + + def initialize + super([]) { reload! } + @route_sets = [] + end + + def reload! + clear! + load_paths + finalize! + ensure + revert + end + + protected + + def clear! + route_sets.each do |routes| + routes.disable_clear_and_finalize = true + routes.clear! + end + end + + def load_paths + paths.each { |path| load(path) } + end + + def finalize! + route_sets.each do |routes| + ActiveSupport.on_load(:action_controller) { routes.finalize! } + end + end + + def revert + route_sets.each do |routes| + routes.disable_clear_and_finalize = false + end + end + end + end +end diff --git a/railties/lib/rails/commands.rb b/railties/lib/rails/commands.rb index 60a93c9848..e3aa6d7c3e 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,15 @@ when 'console' Rails::Console.start(Rails.application) when 'server' + # Change to the application's path if there is no config.ru file in current dir. + # This allows us to run script/rails server from other directories, but still get + # the main config.ru and properly set the tmp directory. + 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, + # otherwise the --environment option given to the server won't propagate. require APP_PATH Dir.chdir(Rails.application.root) server.start diff --git a/railties/lib/rails/commands/plugin.rb b/railties/lib/rails/commands/plugin.rb index 7bb4a1c054..8b2cd1bdba 100644 --- a/railties/lib/rails/commands/plugin.rb +++ b/railties/lib/rails/commands/plugin.rb @@ -58,7 +58,7 @@ class RailsEnvironment else plugin = name_uri_or_plugin end - unless plugin.nil? + if plugin plugin.install else puts "Plugin not found: #{name_uri_or_plugin}" diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index 0620b8608e..e9ce9610b8 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -17,7 +17,7 @@ module Rails # In Rails versions before to 3.0, your gems automatically behaved as Engine, however # this coupled Rails to Rubygems. Since Rails 3.0, if you want a gem to automatically # behave as Engine, you have to specify an Engine for it somewhere inside your plugin - # lib folder (similar with how we spceify a Railtie): + # lib folder (similar to how we specify a Railtie): # # # lib/my_engine.rb # module MyEngine @@ -47,6 +47,26 @@ module Rails # end # end # + # == Generators + # + # You can set up generators for engine with config.generators method: + # + # class MyEngine < Rails::Engine + # config.generators do |g| + # g.orm :active_record + # g.template_engine :erb + # g.test_framework :test_unit + # end + # end + # + # You can also set generators for application by using config.app_generators: + # + # class MyEngine < Rails::Engine + # # note that you can also pass block to app_generators in the same way you + # # can pass it to generators method + # config.app_generators.orm :datamapper + # end + # # == Paths # # Since Rails 3.0, both your Application and Engines do not have hardcoded paths. @@ -57,30 +77,30 @@ module Rails # you need to do is: # # class MyEngine < Rails::Engine - # paths.app.controllers = "lib/controllers" + # paths["app/controllers"] = "lib/controllers" # end # # You can also have your controllers being loaded from both "app/controllers" and # "lib/controllers": # # class MyEngine < Rails::Engine - # paths.app.controllers << "lib/controllers" + # paths["app/controllers"] << "lib/controllers" # end # # The available paths in an Engine are: # # class MyEngine < Rails::Engine - # paths.app = "app" - # paths.app.controllers = "app/controllers" - # paths.app.helpers = "app/helpers" - # paths.app.models = "app/models" - # paths.app.views = "app/views" - # paths.lib = "lib" - # paths.lib.tasks = "lib/tasks" - # paths.config = "config" - # paths.config.initializers = "config/initializers" - # paths.config.locales = "config/locales" - # paths.config.routes = "config/routes.rb" + # paths["app"] #=> ["app"] + # paths["app/controllers"] #=> ["app/controllers"] + # paths["app/helpers"] #=> ["app/helpers"] + # paths["app/models"] #=> ["app/models"] + # paths["app/views"] #=> ["app/views"] + # paths["lib"] #=> ["lib"] + # paths["lib/tasks"] #=> ["lib/tasks"] + # paths["config"] #=> ["config"] + # paths["config/initializers"] #=> ["config/initializers"] + # paths["config/locales"] #=> ["config/locales"] + # paths["config/routes"] #=> ["config/routes.rb"] # end # # Your Application class adds a couple more paths to this set. And as in your Application, @@ -165,19 +185,21 @@ module Rails # == Serving static files # # By default, rails use ActionDispatch::Static to serve static files in development mode. This is ok - # while you develop your application, but when you want to deploy it, assets from engine will not be served. + # while you develop your application, but when you want to deploy it, assets from engine will not be + # served by default. You should choose one of the two following strategies: # - # You can fix it in one of two ways: # * enable serving static files by setting config.serve_static_assets to true # * symlink engines' public directories in application's public directory by running - # `rake railties:create_symlinks` + # `rake ENGINE_NAME:install:assets`, where ENGINE_NAME is usually my_engine for the + # examples above # # == Engine name # # There are some places where engine's name is used. + # # * routes: when you mount engine with mount(MyEngine::Engine => '/my_engine'), it's used as default :as option - # * migrations: when you copy engine's migrations, they will be decorated with suffix based on engine_name, for example: - # 2010010203121314_create_users.my_engine.rb + # + # * rake tasks: engines have a few rake tasks. They are usually under my_engine namespace. # # Engine name is set by default based on class name. For MyEngine::Engine it will be my_engine_engine. # You can change it manually it manually using engine_name method: @@ -190,15 +212,17 @@ module Rails # # == Namespaced Engine # - # Normally, when you create controllers, helpers and models inside engine, they are treated - # as they would be created inside application. One of the cosequences of that is including - # application's helpers and url_helpers inside controller. Sometimes, especially when your - # engine provides its own routes, you don't want that. To isolate engine's stuff from application - # you can use namespace method: + # Normally when you create controllers, helpers and models inside engine, they are treated + # as they were created inside the application. This means all applications helpers and named routes + # will be available to your engine controllers. + # + # However, sometimes you want to isolate your engine from the application, specially if your engine + # have its own router. To do that, you simply need to call +isolate_namespace+. This method requires + # you to pass a module where all your controllers, helpers and models should be nested to: # # module MyEngine # class Engine < Rails::Engine - # namespace MyEngine + # isolate_namespace MyEngine # end # end # @@ -215,15 +239,21 @@ module Rails # url_helpers from MyEngine::Engine.routes. # # The next thing that changes in isolated engine is routes behaviour. Normally, when you namespace - # your controllers, you need to use scope or namespace method in routes. With isolated engine, - # the namespace is applied by default, so you can ignore it in routes. Further more, you don't need - # to use longer url helpers like "my_engine_articles_path". As the prefix is not set you can just use - # articles_path as you would normally do. + # your controllers, you also need to do namespace all your routes. With isolated engine, + # the namespace is applied by default, so you can ignore it in routes: + # + # MyEngine::Engine.routes.draw do + # resources :articles + # end + # + # The routes above will automatically point to MyEngine::ApplicationContoller. Further more, you don't + # need to use longer url helpers like "my_engine_articles_path". Instead, you shuold simply use + # articles_path as you would do with your application. # # To make that behaviour consistent with other parts of framework, isolated engine has influence also on # ActiveModel::Naming. When you use namespaced model, like MyEngine::Article, it will normally - # use the prefix "my_engine". In isolated engine, the prefix will be ommited in most of the places, - # like url helpers or form fields. + # use the prefix "my_engine". In isolated engine, the prefix will be ommited in url helpers and + # form fields for convenience. # # polymorphic_url(MyEngine::Article.new) #=> "articles_path" # @@ -231,16 +261,15 @@ module Rails # text_field :title #=> <input type="text" name="article[title]" id="article_title" /> # end # - # - # Additionaly namespaced engine will set its name according to namespace, so in that case: - # MyEngine::Engine.engine_name #=> "my_engine" and it will set MyEngine.table_name_prefix - # to "my_engine_". + # Additionaly isolated engine will set its name according to namespace, so + # MyEngine::Engine.engine_name #=> "my_engine". It will also set MyEngine.table_name_prefix + # to "my_engine_", changing MyEngine::Article model to use my_engine_article table. # # == Using Engine's routes outside Engine # - # Since you can mount engine inside application's routes now, you do not have direct access to engine's - # url_helpers inside application. When you mount Engine in application's routes special helper is - # created to allow doing that. Consider such scenario: + # Since now you can mount engine inside application's routes, you do not have direct access to engine's + # url_helpers inside application. When you mount Engine in application's routes, a special helper is + # created to allow you to do that. Consider such scenario: # # # APP/config/routes.rb # MyApplication::Application.routes.draw do @@ -248,7 +277,7 @@ module Rails # match "/foo" => "foo#index" # end # - # Now, you can use my_engine helper: + # Now, you can use my_engine helper inside your application: # # class FooController < ApplicationController # def index @@ -256,24 +285,27 @@ module Rails # end # end # - # There is also 'app' helper that gives you access to application's routes inside Engine: + # There is also 'main_app' helper that gives you access to application's routes inside Engine: # # module MyEngine # class BarController - # app.foo_path #=> /foo + # def index + # main_app.foo_path #=> /foo + # end # end # end # - # Note that :as option takes engine_name as default, so most of the time you can ommit it. + # Note that the :as option given to mount takes the engine_name as default, so most of the time + # you can simply ommit it. # - # If you want to generate url to engine's route using polymorphic_url, you can also use that helpers. - # - # Let's say that you want to create a form pointing to one of the engine's routes. All you need to do - # is passing helper as the first element in array with attributes for url: + # Finally, if you want to generate url to engine's route using polymorphic_url, you also need + # to pass the engine helper. Let's say that you want to create a form pointing to one of the + # engine's routes. All you need to do is pass the helper as the first element in array with + # attributes for url: # # form_for([my_engine, @user]) # - # This code will use my_engine.user_path(@user) to generate proper route. + # This code will use my_engine.user_path(@user) to generate the proper route. # # == Migrations & seed data # @@ -283,7 +315,7 @@ module Rails # To use engine's migrations in application you can use rake task, which copies them to # application's dir: # - # rake railties:copy_migrations + # rake ENGINE_NAME:install:migrations # # If your engine has migrations, you may also want to prepare data for the database in # seeds.rb file. You can load that data using load_seed method, e.g. @@ -295,7 +327,7 @@ module Rails autoload :Configuration, "rails/engine/configuration" class << self - attr_accessor :called_from, :namespaced + attr_accessor :called_from, :isolated alias :engine_name :railtie_name def inherited(base) @@ -330,37 +362,38 @@ module Rails @endpoint end - def namespace(mod) + def isolate_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 + self.isolated = 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? - !!namespaced + def isolated? + !!isolated end end delegate :middleware, :root, :paths, :to => :config - delegate :engine_name, :namespaced?, :to => "self.class" + delegate :engine_name, :isolated?, :to => "self.class" def load_tasks super - config.paths.lib.tasks.to_a.sort.each { |ext| load(ext) } + paths["lib/tasks"].existent.sort.each { |ext| load(ext) } end def eager_load! @@ -400,6 +433,8 @@ module Rails def routes @routes ||= ActionDispatch::Routing::RouteSet.new + @routes.append(&Proc.new) if block_given? + @routes end def initializers @@ -418,7 +453,7 @@ module Rails # # Blog::Engine.load_seed def load_seed - seed_file = config.paths.db.seeds.to_a.first + seed_file = paths["db/seeds"].existent.first load(seed_file) if File.exist?(seed_file) end @@ -446,19 +481,22 @@ module Rails end initializer :add_routing_paths do |app| - paths.config.routes.to_a.each do |route| - app.routes_reloader.paths.unshift(route) if File.exists?(route) + paths = self.paths["config/routes"].existent + + if routes? || paths.any? + app.routes_reloader.paths.unshift(*paths) + app.routes_reloader.route_sets << routes end end # I18n load paths are a special case since the ones added # later have higher priority. initializer :add_locales do - config.i18n.railties_load_path.concat(paths.config.locales.to_a) + config.i18n.railties_load_path.concat(paths["config/locales"].existent) end initializer :add_view_paths do - views = paths.app.views.to_a + views = paths["app/views"].existent unless views.empty? ActiveSupport.on_load(:action_controller){ prepend_view_path(views) } ActiveSupport.on_load(:action_mailer){ prepend_view_path(views) } @@ -466,28 +504,27 @@ module Rails end initializer :load_environment_config, :before => :load_environment_hook do - environment = config.paths.config.environments.to_a.first + environment = paths["config/environments"].existent.first require environment if environment end initializer :append_asset_paths do config.asset_path ||= "/#{engine_name}%s" - public_path = config.paths.public.to_a.first + public_path = paths["public"].first if config.compiled_asset_path && File.exist?(public_path) config.static_asset_paths[config.compiled_asset_path] = public_path end end - initializer :prepend_helpers_path do - unless namespaced? - config.helpers_paths = [] unless config.respond_to?(:helpers_paths) - config.helpers_paths = config.paths.app.helpers.to_a + config.helpers_paths + initializer :prepend_helpers_path do |app| + if !isolated? || (app == self) + app.config.helpers_paths.unshift(*paths["app/helpers"].existent) end end initializer :load_config_initializers do - paths.config.initializers.to_a.sort.each do |initializer| + config.paths["config/initializers"].existent.sort.each do |initializer| load(initializer) end end @@ -497,7 +534,27 @@ module Rails # consistently executed after all the initializers above across all engines. end + rake_tasks do + next if self.is_a?(Rails::Application) + + namespace railtie_name do + namespace :install do + # TODO Add assets copying to this list + # TODO Skip this if there is no paths["db/migrate"] for the engine + desc "Copy migrations from #{railtie_name} to application" + task :migrations do + ENV["FROM"] = railtie_name + Rake::Task["railties:install:migrations"].invoke + end + end + end + end + protected + def routes? + defined?(@routes) + end + def find_root_with_flag(flag, default=nil) root_path = self.class.called_from @@ -509,7 +566,7 @@ module Rails root = File.exist?("#{root_path}/#{flag}") ? root_path : default raise "Could not find root path for #{self}" unless root - Config::CONFIG['host_os'] =~ /mswin|mingw/ ? + RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ ? Pathname.new(root).expand_path : Pathname.new(root).realpath end diff --git a/railties/lib/rails/engine/configuration.rb b/railties/lib/rails/engine/configuration.rb index d4d87be527..7a07dcad7d 100644 --- a/railties/lib/rails/engine/configuration.rb +++ b/railties/lib/rails/engine/configuration.rb @@ -4,40 +4,61 @@ module Rails class Engine class Configuration < ::Rails::Railtie::Configuration attr_reader :root - attr_writer :eager_load_paths, :autoload_once_paths, :autoload_paths - attr_accessor :middleware, :plugins, :asset_path + attr_writer :middleware, :eager_load_paths, :autoload_once_paths, :autoload_paths + attr_accessor :plugins, :asset_path def initialize(root=nil) super() @root = root - @middleware = Rails::Configuration::MiddlewareStackProxy.new - @helpers_paths = [] + end + + # Returns the middleware stack for the engine. + def middleware + @middleware ||= Rails::Configuration::MiddlewareStackProxy.new + end + + # Holds generators configuration: + # + # config.generators do |g| + # g.orm :datamapper, :migration => true + # g.template_engine :haml + # g.test_framework :rspec + # end + # + # If you want to disable color in console, do: + # + # config.generators.colorize_logging = false + # + def generators #:nodoc + @generators ||= Rails::Configuration::Generators.new + yield(@generators) if block_given? + @generators end def paths @paths ||= begin paths = Rails::Paths::Root.new(@root) - paths.app "app", :eager_load => true, :glob => "*" - paths.app.controllers "app/controllers", :eager_load => true - paths.app.helpers "app/helpers", :eager_load => true - paths.app.models "app/models", :eager_load => true - paths.app.mailers "app/mailers", :eager_load => true - paths.app.views "app/views" - paths.lib "lib", :load_path => true - paths.lib.tasks "lib/tasks", :glob => "**/*.rake" - paths.config "config" - paths.config.initializers "config/initializers", :glob => "**/*.rb" - paths.config.locales "config/locales", :glob => "*.{rb,yml}" - paths.config.routes "config/routes.rb" - paths.config.environments "config/environments", :glob => "#{Rails.env}.rb" - paths.public "public" - paths.public.javascripts "public/javascripts" - paths.public.stylesheets "public/stylesheets" - paths.vendor "vendor", :load_path => true - paths.vendor.plugins "vendor/plugins" - paths.db "db" - paths.db.migrate "db/migrate" - paths.db.seeds "db/seeds.rb" + paths.add "app", :eager_load => true, :glob => "*" + paths.add "app/controllers", :eager_load => true + paths.add "app/helpers", :eager_load => true + paths.add "app/models", :eager_load => true + paths.add "app/mailers", :eager_load => true + paths.add "app/views" + paths.add "lib", :load_path => true + paths.add "lib/tasks", :glob => "**/*.rake" + paths.add "config" + paths.add "config/environments", :glob => "#{Rails.env}.rb" + paths.add "config/initializers", :glob => "**/*.rb" + paths.add "config/locales", :glob => "*.{rb,yml}" + paths.add "config/routes", :with => "config/routes.rb" + paths.add "db" + paths.add "db/migrate" + paths.add "db/seeds", :with => "db/seeds.rb" + paths.add "public" + paths.add "public/javascripts" + paths.add "public/stylesheets" + paths.add "vendor", :load_path => true + paths.add "vendor/plugins" paths end end diff --git a/railties/lib/rails/engine/railties.rb b/railties/lib/rails/engine/railties.rb index 389a7602c6..e91bdbf1e5 100644 --- a/railties/lib/rails/engine/railties.rb +++ b/railties/lib/rails/engine/railties.rb @@ -15,7 +15,7 @@ module Rails def plugins @plugins ||= begin plugin_names = (@config.plugins || [:all]).map { |p| p.to_sym } - Plugin.all(plugin_names, @config.paths.vendor.plugins) + Plugin.all(plugin_names, @config.paths["vendor/plugins"].existent) end end end diff --git a/railties/lib/rails/generators/actions.rb b/railties/lib/rails/generators/actions.rb index 8f0d5ffff4..378c07cb0e 100644 --- a/railties/lib/rails/generators/actions.rb +++ b/railties/lib/rails/generators/actions.rb @@ -187,8 +187,8 @@ module Rails # initializer("globals.rb") do # data = "" # - # ['MY_WORK', 'ADMINS', 'BEST_COMPANY_EVAR'].each do - # data << "#{const} = :entp" + # ['MY_WORK', 'ADMINS', 'BEST_COMPANY_EVAR'].each do |const| + # data << "#{const} = :entp\n" # end # # data diff --git a/railties/lib/rails/generators/rails/app/app_generator.rb b/railties/lib/rails/generators/rails/app/app_generator.rb index 2715483914..7907191c74 100644 --- a/railties/lib/rails/generators/rails/app/app_generator.rb +++ b/railties/lib/rails/generators/rails/app/app_generator.rb @@ -14,23 +14,20 @@ module Rails @options = generator.options end - private - %w(template copy_file directory empty_directory inside - empty_directory_with_gitkeep create_file chmod shebang).each do |method| - class_eval <<-RUBY, __FILE__, __LINE__ + 1 - def #{method}(*args, &block) - @generator.send(:#{method}, *args, &block) - end - RUBY - end + private - # TODO: Remove once this is fully in place - def method_missing(meth, *args, &block) - STDERR.puts "Calling #{meth} with #{args.inspect} with #{block}" - @generator.send(meth, *args, &block) - end + def method_missing(meth, *args, &block) + @generator.send(meth, *args, &block) + end end + # The application builder allows you to override elements of the application + # generator without being forced to reverse the operations of the default + # generator. + # + # This allows you to override entire operations, like the creation of the + # Gemfile, README, or javascript files, without needing to know exactly + # what those operations do so you can create another template action. class AppBuilder def rakefile template "Rakefile" diff --git a/railties/lib/rails/generators/rails/app/templates/Gemfile b/railties/lib/rails/generators/rails/app/templates/Gemfile index 1dbf27d978..40213b1261 100644 --- a/railties/lib/rails/generators/rails/app/templates/Gemfile +++ b/railties/lib/rails/generators/rails/app/templates/Gemfile @@ -3,15 +3,18 @@ source 'http://rubygems.org' <%- if options.dev? -%> gem 'rails', :path => '<%= Rails::Generators::RAILS_DEV_PATH %>' gem 'arel', :git => 'git://github.com/rails/arel.git' +gem "rack", :git => "git://github.com/rack/rack.git" <%- elsif options.edge? -%> gem 'rails', :git => 'git://github.com/rails/rails.git' gem 'arel', :git => 'git://github.com/rails/arel.git' +gem "rack", :git => "git://github.com/rack/rack.git" <%- else -%> gem 'rails', '<%= Rails::VERSION::STRING %>' # Bundle edge Rails instead: # gem 'rails', :git => 'git://github.com/rails/rails.git' # gem 'arel', :git => 'git://github.com/rails/arel.git' +# gem "rack", :git => "git://github.com/rack/rack.git" <%- end -%> <% unless options[:skip_active_record] -%> diff --git a/railties/lib/rails/generators/rails/app/templates/app/views/layouts/application.html.erb.tt b/railties/lib/rails/generators/rails/app/templates/app/views/layouts/application.html.erb.tt index 1dd112b4a6..1de78eecae 100644 --- a/railties/lib/rails/generators/rails/app/templates/app/views/layouts/application.html.erb.tt +++ b/railties/lib/rails/generators/rails/app/templates/app/views/layouts/application.html.erb.tt @@ -4,7 +4,7 @@ <title><%= app_const_base %></title> <%%= stylesheet_link_tag :all %> <%%= javascript_include_tag :defaults %> - <%%= csrf_meta_tag %> + <%%= csrf_meta_tags %> </head> <body> diff --git a/railties/lib/rails/generators/rails/app/templates/config/databases/oracle.yml b/railties/lib/rails/generators/rails/app/templates/config/databases/oracle.yml index fddf8b8144..b661a60389 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/databases/oracle.yml +++ b/railties/lib/rails/generators/rails/app/templates/config/databases/oracle.yml @@ -10,7 +10,7 @@ # # By default prefetch_rows (OCI_ATTR_PREFETCH_ROWS) is set to 100. And # until true bind variables are supported, cursor_sharing is set by default -# to 'similar'. Both can be changed in the configation below; the defaults +# to 'similar'. Both can be changed in the configuration below; the defaults # are equivalent to specifying: # # prefetch_rows: 100 diff --git a/railties/lib/rails/paths.rb b/railties/lib/rails/paths.rb index d303212f52..8570fc7b3f 100644 --- a/railties/lib/rails/paths.rb +++ b/railties/lib/rails/paths.rb @@ -2,15 +2,17 @@ require 'set' module Rails module Paths - module PathParent - attr_reader :children - + module PathParent #:nodoc: def method_missing(id, *args) - name = id.to_s + match = id.to_s.match(/^(.*)=$/) + full = [@current, $1 || id].compact.join("/") + + ActiveSupport::Deprecation.warn 'config.paths.app.controller API is deprecated in ' << + 'favor of config.paths["app/controller"] API.' - if name =~ /^(.*)=$/ || args.any? - @children[$1 || name] = Path.new(@root, *args) - elsif path = @children[name] + if match || args.any? + @root[full] = Path.new(@root, full, *args) + elsif path = @root[full] path else super @@ -18,22 +20,72 @@ module Rails end end - class Root + # This object is an extended hash that behaves as root of the Rails::Paths system. + # It allows you to collect information about how you want to structure your application + # paths by a Hash like API. It requires you to give a physical path on initialization. + # + # root = Root.new + # root.add "app/controllers", :eager_load => true + # + # The command above creates a new root object and add "app/controllers" as a path. + # This means we can get a Path object back like below: + # + # path = root["app/controllers"] + # path.eager_load? #=> true + # path.is_a?(Rails::Paths::Path) #=> true + # + # The Path object is simply an array and allows you to easily add extra paths: + # + # path.is_a?(Array) #=> true + # path.inspect #=> ["app/controllers"] + # + # path << "lib/controllers" + # path.inspect #=> ["app/controllers", "lib/controllers"] + # + # Notice that when you add a path using #add, the path object created already + # contains the path with the same path value given to #add. In some situations, + # you may not want this behavior, so you can give :with as option. + # + # root.add "config/routes", :with => "config/routes.rb" + # root["config/routes"].inspect #=> ["config/routes.rb"] + # + # #add also accepts the following options as argument: eager_load, autoload, + # autoload_once and glob. + # + # Finally, the Path object also provides a few helpers: + # + # root = Root.new + # root.path = "/rails" + # root.add "app/controllers" + # + # root["app/controllers"].expanded #=> ["/rails/app/controllers"] + # root["app/controllers"].existent #=> ["/rails/app/controllers"] + # + # Check the Path documentation for more information. + class Root < ::Hash include PathParent - attr_accessor :path def initialize(path) raise if path.is_a?(Array) - @children = {} + @current = nil @path = path @root = self - @all_paths = [] + super() + end + + def []=(path, value) + value = Path.new(self, path, value) unless value.is_a?(Path) + super(path, value) + end + + def add(path, options={}) + with = options[:with] || path + self[path] = Path.new(self, path, with, options) end def all_paths - @all_paths.uniq! - @all_paths + values.tap { |v| v.uniq! } end def autoload_once @@ -52,68 +104,54 @@ module Rails filter_by(:load_path?) end - def push(*) - raise "Application root can only have one physical path" - end - - alias unshift push - alias << push - alias concat push - protected def filter_by(constraint) all = [] all_paths.each do |path| if path.send(constraint) - paths = path.paths - paths -= path.children.values.map { |p| p.send(constraint) ? [] : p.paths }.flatten + paths = path.existent + paths -= path.children.map { |p| p.send(constraint) ? [] : p.existent }.flatten all.concat(paths) end end all.uniq! - all.reject! { |p| !File.exists?(p) } all end end - class Path - include PathParent, Enumerable + class Path < Array + include PathParent attr_reader :path attr_accessor :glob - def initialize(root, *paths) - options = paths.last.is_a?(::Hash) ? paths.pop : {} - @children = {} + def initialize(root, current, *paths) + options = paths.last.is_a?(::Hash) ? paths.pop : {} + super(paths.flatten) + + @current = current @root = root - @paths = paths.flatten @glob = options[:glob] autoload_once! if options[:autoload_once] eager_load! if options[:eager_load] autoload! if options[:autoload] load_path! if options[:load_path] - - @root.all_paths << self - end - - def each - to_a.each { |p| yield p } end - def push(path) - @paths.push path + def children + keys = @root.keys.select { |k| k.include?(@current) } + keys.delete(@current) + @root.values_at(*keys.sort) end - alias << push - - def unshift(path) - @paths.unshift path + def first + expanded.first end - def concat(paths) - @paths.concat paths + def last + expanded.last end %w(autoload_once eager_load autoload load_path).each do |m| @@ -132,20 +170,36 @@ module Rails RUBY end - def paths + # Expands all paths against the root and return all unique values. + def expanded raise "You need to set a path root" unless @root.path + result = [] - result = @paths.map do |p| + each do |p| path = File.expand_path(p, @root.path) - @glob ? Dir[File.join(path, @glob)] : path + + if @glob + result.concat Dir[File.join(path, @glob)] + else + result << path + end end - result.flatten! result.uniq! result end - alias to_a paths + # Returns all expanded paths but only if they exist in the filesystem. + def existent + expanded.select { |f| File.exists?(f) } + end + + def paths + ActiveSupport::Deprecation.warn "paths is deprecated. Please call expand instead." + expanded + end + + alias to_a expanded end end -end
\ No newline at end of file +end diff --git a/railties/lib/rails/plugin.rb b/railties/lib/rails/plugin.rb index 5614624673..ceddd25eaa 100644 --- a/railties/lib/rails/plugin.rb +++ b/railties/lib/rails/plugin.rb @@ -62,13 +62,13 @@ module Rails end initializer :handle_lib_autoload, :before => :set_load_path do |app| - paths = if app.config.reload_plugins + autoload = if app.config.reload_plugins config.autoload_paths else config.autoload_once_paths end - paths.concat config.paths.lib.to_a + autoload.concat paths["lib"].existent end initializer :load_init_rb, :before => :load_config_initializers do |app| @@ -83,7 +83,7 @@ module Rails initializer :sanity_check_railties_collision do if Engine.subclasses.map { |k| k.root.to_s }.include?(root.to_s) - raise "\"#{name}\" is a Railtie/Engine and cannot be installed as plugin" + raise "\"#{name}\" is a Railtie/Engine and cannot be installed as a plugin" end end end diff --git a/railties/lib/rails/railtie.rb b/railties/lib/rails/railtie.rb index 09650789ac..c76bc83377 100644 --- a/railties/lib/rails/railtie.rb +++ b/railties/lib/rails/railtie.rb @@ -83,7 +83,7 @@ module Rails # # class MyRailtie < Rails::Railtie # # Customize the ORM - # config.generators.orm :my_railtie_orm + # config.app_generators.orm :my_railtie_orm # # # Add a to_prepare block which is executed once in production # # and before each request in development @@ -191,6 +191,13 @@ module Rails def load_tasks self.class.rake_tasks.each(&:call) + + # load also tasks from all superclasses + klass = self.class.superclass + while klass.respond_to?(:rake_tasks) + klass.rake_tasks.each { |t| self.instance_exec(&t) } + klass = klass.superclass + end end def load_generators diff --git a/railties/lib/rails/railtie/configuration.rb b/railties/lib/rails/railtie/configuration.rb index f09e3940cc..afeceafb67 100644 --- a/railties/lib/rails/railtie/configuration.rb +++ b/railties/lib/rails/railtie/configuration.rb @@ -5,7 +5,6 @@ module Rails class Configuration def initialize @@options ||= {} - @@static_asset_paths = ActiveSupport::OrderedHash.new end # This allows you to modify the application's middlewares from Engines. @@ -17,25 +16,19 @@ module Rails @@app_middleware ||= Rails::Configuration::MiddlewareStackProxy.new end - # Holds generators configuration: + # This allows you to modify application's generators from Railties. # - # config.generators do |g| - # g.orm :datamapper, :migration => true - # g.template_engine :haml - # g.test_framework :rspec - # end - # - # If you want to disable color in console, do: - # - # config.generators.colorize_logging = false - # - def generators - @@generators ||= Rails::Configuration::Generators.new - if block_given? - yield @@generators - else - @@generators - end + # Values set on app_generators will become defaults for applicaiton, unless + # application overwrites them. + def app_generators + @@app_generators ||= Rails::Configuration::Generators.new + yield(@@app_generators) if block_given? + @@app_generators + end + + def generators(&block) #:nodoc + ActiveSupport::Deprecation.warn "config.generators in Rails::Railtie is deprecated. Please use config.app_generators instead." + app_generators(&block) end def before_configuration(&block) @@ -70,7 +63,7 @@ module Rails # with associated public folders, like: # { "/" => "/app/public", "/my_engine" => "app/engines/my_engine/public" } def static_asset_paths - @@static_asset_paths + @@static_asset_paths ||= ActiveSupport::OrderedHash.new end private diff --git a/railties/lib/rails/tasks/documentation.rake b/railties/lib/rails/tasks/documentation.rake index 7aefbb49b0..edd716d7d0 100644 --- a/railties/lib/rails/tasks/documentation.rake +++ b/railties/lib/rails/tasks/documentation.rake @@ -47,7 +47,7 @@ namespace :doc do rdoc.rdoc_files.include('app/**/*.rb') rdoc.rdoc_files.include('lib/**/*.rb') } - Rake::Task['doc:app'].comment = "Generate docs for the app -- also availble doc:rails, doc:guides, doc:plugins (options: TEMPLATE=/rdoc-template.rb, TITLE=\"Custom Title\")" + Rake::Task['doc:app'].comment = "Generate docs for the app -- also available doc:rails, doc:guides, doc:plugins (options: TEMPLATE=/rdoc-template.rb, TITLE=\"Custom Title\")" # desc 'Generate documentation for the Rails framework.' RDocTaskWithoutDescriptions.new("rails") { |rdoc| diff --git a/railties/lib/rails/tasks/railties.rake b/railties/lib/rails/tasks/railties.rake index 7cf31f84a0..e08bd9687d 100644 --- a/railties/lib/rails/tasks/railties.rake +++ b/railties/lib/rails/tasks/railties.rake @@ -1,8 +1,8 @@ namespace :railties do - desc "Create symlinks to railties public directories in application's public directory." + # desc "Create symlinks to railties public directories in application's public directory." task :create_symlinks => :environment do paths = Rails.application.config.static_asset_paths.dup - app_public_path = Rails.application.config.paths.public.to_a.first + app_public_path = Rails.application.paths["public"].first paths.each do |mount_path, path| symlink_path = File.join(app_public_path, mount_path) 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/lib/rails/test_unit/testing.rake b/railties/lib/rails/test_unit/testing.rake index 713833f884..28dc40379b 100644 --- a/railties/lib/rails/test_unit/testing.rake +++ b/railties/lib/rails/test_unit/testing.rake @@ -73,7 +73,8 @@ end desc 'Runs test:units, test:functionals, test:integration together (also available: test:benchmark, test:profile, test:plugins)' task :test do - errors = %w(test:units test:functionals test:integration).collect do |task| + tests_to_run = ENV['TEST'] ? ["test:single"] : %w(test:units test:functionals test:integration) + errors = tests_to_run.collect do |task| begin Rake::Task[task].invoke nil @@ -123,6 +124,10 @@ namespace :test do end Rake::Task['test:uncommitted'].comment = "Test changes since last checkin (only Subversion and Git)" + Rake::TestTask.new(:single => "test:prepare") do |t| + t.libs << "test" + end + TestTaskWithoutDescription.new(:units => "test:prepare") do |t| t.libs << "test" t.pattern = 'test/unit/**/*_test.rb' |