From ae7ada1fde1648a5c81b9e6adf74d515e0bbe0a5 Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Thu, 31 Dec 2009 13:11:54 -0800 Subject: Some railties cleanup: * Rename /rails.rb -> /railtie.rb * Rails::Plugin -> Rails::Railtie * Rails::Plugin::Vendored -> Rails::Plugin --- actionmailer/lib/action_mailer/rails.rb | 25 ---- actionmailer/lib/action_mailer/railtie.rb | 25 ++++ actionpack/lib/action_controller/rails.rb | 96 ---------------- actionpack/lib/action_controller/railtie.rb | 96 ++++++++++++++++ actionpack/lib/action_view/rails.rb | 2 - actionpack/lib/action_view/railtie.rb | 2 + activemodel/lib/active_model/rails.rb | 2 - activemodel/lib/active_model/railtie.rb | 2 + activerecord/lib/active_record/rails.rb | 71 ------------ activerecord/lib/active_record/railtie.rb | 71 ++++++++++++ activeresource/lib/active_resource/rails.rb | 2 - activeresource/lib/active_resource/railtie.rb | 2 + railties/lib/rails.rb | 1 + railties/lib/rails/all.rb | 2 +- railties/lib/rails/application.rb | 4 +- railties/lib/rails/configuration.rb | 8 +- .../generators/rails/app/templates/config/boot.rb | 15 ++- railties/lib/rails/plugin.rb | 126 +++++++-------------- railties/lib/rails/railtie.rb | 43 +++++++ railties/test/application/configuration_test.rb | 1 + railties/test/application/initializer_test.rb | 2 + railties/test/application/notifications_test.rb | 1 + railties/test/initializer/path_test.rb | 2 +- railties/test/plugins/configuration_test.rb | 8 +- railties/test/plugins/framework_extension_test.rb | 4 +- 25 files changed, 310 insertions(+), 303 deletions(-) delete mode 100644 actionmailer/lib/action_mailer/rails.rb create mode 100644 actionmailer/lib/action_mailer/railtie.rb delete mode 100644 actionpack/lib/action_controller/rails.rb create mode 100644 actionpack/lib/action_controller/railtie.rb delete mode 100644 actionpack/lib/action_view/rails.rb create mode 100644 actionpack/lib/action_view/railtie.rb delete mode 100644 activemodel/lib/active_model/rails.rb create mode 100644 activemodel/lib/active_model/railtie.rb delete mode 100644 activerecord/lib/active_record/rails.rb create mode 100644 activerecord/lib/active_record/railtie.rb delete mode 100644 activeresource/lib/active_resource/rails.rb create mode 100644 activeresource/lib/active_resource/railtie.rb create mode 100644 railties/lib/rails/railtie.rb diff --git a/actionmailer/lib/action_mailer/rails.rb b/actionmailer/lib/action_mailer/rails.rb deleted file mode 100644 index 5389f243e5..0000000000 --- a/actionmailer/lib/action_mailer/rails.rb +++ /dev/null @@ -1,25 +0,0 @@ -require "action_mailer" -require "rails" - -module ActionMailer - class Plugin < Rails::Plugin - plugin_name :action_mailer - - initializer "action_mailer.set_configs" do |app| - app.config.action_mailer.each do |k,v| - ActionMailer::Base.send "#{k}=", v - end - end - - # TODO: ActionController::Base.logger should delegate to its own config.logger - initializer "action_mailer.logger" do - ActionMailer::Base.logger ||= Rails.logger - end - - initializer "action_mailer.view_paths" do |app| - # TODO: this should be combined with the logic for default config.action_mailer.view_paths - view_path = ActionView::PathSet.type_cast(app.config.view_path, app.config.cache_classes) - ActionMailer::Base.template_root = view_path if ActionMailer::Base.view_paths.blank? - end - end -end \ No newline at end of file diff --git a/actionmailer/lib/action_mailer/railtie.rb b/actionmailer/lib/action_mailer/railtie.rb new file mode 100644 index 0000000000..5410c7d75f --- /dev/null +++ b/actionmailer/lib/action_mailer/railtie.rb @@ -0,0 +1,25 @@ +require "action_mailer" +require "rails" + +module ActionMailer + class Railtie < Rails::Railtie + plugin_name :action_mailer + + initializer "action_mailer.set_configs" do |app| + app.config.action_mailer.each do |k,v| + ActionMailer::Base.send "#{k}=", v + end + end + + # TODO: ActionController::Base.logger should delegate to its own config.logger + initializer "action_mailer.logger" do + ActionMailer::Base.logger ||= Rails.logger + end + + initializer "action_mailer.view_paths" do |app| + # TODO: this should be combined with the logic for default config.action_mailer.view_paths + view_path = ActionView::PathSet.type_cast(app.config.view_path, app.config.cache_classes) + ActionMailer::Base.template_root = view_path if ActionMailer::Base.view_paths.blank? + end + end +end \ No newline at end of file diff --git a/actionpack/lib/action_controller/rails.rb b/actionpack/lib/action_controller/rails.rb deleted file mode 100644 index 4d3ca43f99..0000000000 --- a/actionpack/lib/action_controller/rails.rb +++ /dev/null @@ -1,96 +0,0 @@ -require "action_controller" -require "rails" - -module ActionController - class Plugin < Rails::Plugin - plugin_name :action_controller - - initializer "action_controller.set_configs" do |app| - app.config.action_controller.each do |k,v| - ActionController::Base.send "#{k}=", v - end - end - - # TODO: ActionController::Base.logger should delegate to its own config.logger - initializer "action_controller.logger" do - ActionController::Base.logger ||= Rails.logger - end - - # Routing must be initialized after plugins to allow the former to extend the routes - initializer "action_controller.initialize_routing" do |app| - app.route_configuration_files << app.config.routes_configuration_file - app.route_configuration_files << app.config.builtin_routes_configuration_file - app.reload_routes! - end - - # Include middleware to serve up static assets - initializer "action_controller.initialize_static_server" do |app| - if app.config.serve_static_assets - app.config.middleware.use(ActionDispatch::Static, Rails.public_path) - end - end - - initializer "action_controller.initialize_middleware_stack" do |app| - middleware = app.config.middleware - middleware.use(::Rack::Lock, :if => lambda { ActionController::Base.allow_concurrency }) - middleware.use(::Rack::Runtime) - middleware.use(ActionDispatch::ShowExceptions, lambda { ActionController::Base.consider_all_requests_local }) - middleware.use(ActionDispatch::Callbacks, lambda { ActionController::Dispatcher.prepare_each_request }) - middleware.use(lambda { ActionController::Base.session_store }, lambda { ActionController::Base.session_options }) - middleware.use(ActionDispatch::ParamsParser) - middleware.use(::Rack::MethodOverride) - middleware.use(::Rack::Head) - middleware.use(ActionDispatch::StringCoercion) - end - - initializer "action_controller.initialize_framework_caches" do - ActionController::Base.cache_store ||= RAILS_CACHE - end - - # Sets +ActionController::Base#view_paths+ and +ActionMailer::Base#template_root+ - # (but only for those frameworks that are to be loaded). If the framework's - # paths have already been set, it is not changed, otherwise it is - # set to use Configuration#view_path. - initializer "action_controller.initialize_framework_views" do |app| - # TODO: this should be combined with the logic for default config.action_controller.view_paths - view_path = ActionView::PathSet.type_cast(app.config.view_path, app.config.cache_classes) - ActionController::Base.view_paths = view_path if ActionController::Base.view_paths.blank? - end - - initializer "action_controller.initialize_metal" do |app| - Rails::Rack::Metal.requested_metals = app.config.metals - - app.config.middleware.insert_before(:"ActionDispatch::ParamsParser", - Rails::Rack::Metal, :if => Rails::Rack::Metal.metals.any?) - end - - # # Prepare dispatcher callbacks and run 'prepare' callbacks - initializer "action_controller.prepare_dispatcher" do |app| - # TODO: This used to say unless defined?(Dispatcher). Find out why and fix. - require 'rails/dispatcher' - - Dispatcher.define_dispatcher_callbacks(app.config.cache_classes) - - unless app.config.cache_classes - # Setup dev mode route reloading - routes_last_modified = app.routes_changed_at - reload_routes = lambda do - unless app.routes_changed_at == routes_last_modified - routes_last_modified = app.routes_changed_at - app.reload_routes! - end - end - ActionDispatch::Callbacks.before_dispatch { |callbacks| reload_routes.call } - end - end - - initializer "action_controller.notifications" do |app| - require 'active_support/notifications' - - ActiveSupport::Notifications.subscribe do |*args| - ActionController::Base.log_event(*args) if ActionController::Base.logger - end - end - - end -end diff --git a/actionpack/lib/action_controller/railtie.rb b/actionpack/lib/action_controller/railtie.rb new file mode 100644 index 0000000000..f861d12905 --- /dev/null +++ b/actionpack/lib/action_controller/railtie.rb @@ -0,0 +1,96 @@ +require "action_controller" +require "rails" + +module ActionController + class Railtie < Rails::Railtie + plugin_name :action_controller + + initializer "action_controller.set_configs" do |app| + app.config.action_controller.each do |k,v| + ActionController::Base.send "#{k}=", v + end + end + + # TODO: ActionController::Base.logger should delegate to its own config.logger + initializer "action_controller.logger" do + ActionController::Base.logger ||= Rails.logger + end + + # Routing must be initialized after plugins to allow the former to extend the routes + initializer "action_controller.initialize_routing" do |app| + app.route_configuration_files << app.config.routes_configuration_file + app.route_configuration_files << app.config.builtin_routes_configuration_file + app.reload_routes! + end + + # Include middleware to serve up static assets + initializer "action_controller.initialize_static_server" do |app| + if app.config.serve_static_assets + app.config.middleware.use(ActionDispatch::Static, Rails.public_path) + end + end + + initializer "action_controller.initialize_middleware_stack" do |app| + middleware = app.config.middleware + middleware.use(::Rack::Lock, :if => lambda { ActionController::Base.allow_concurrency }) + middleware.use(::Rack::Runtime) + middleware.use(ActionDispatch::ShowExceptions, lambda { ActionController::Base.consider_all_requests_local }) + middleware.use(ActionDispatch::Callbacks, lambda { ActionController::Dispatcher.prepare_each_request }) + middleware.use(lambda { ActionController::Base.session_store }, lambda { ActionController::Base.session_options }) + middleware.use(ActionDispatch::ParamsParser) + middleware.use(::Rack::MethodOverride) + middleware.use(::Rack::Head) + middleware.use(ActionDispatch::StringCoercion) + end + + initializer "action_controller.initialize_framework_caches" do + ActionController::Base.cache_store ||= RAILS_CACHE + end + + # Sets +ActionController::Base#view_paths+ and +ActionMailer::Base#template_root+ + # (but only for those frameworks that are to be loaded). If the framework's + # paths have already been set, it is not changed, otherwise it is + # set to use Configuration#view_path. + initializer "action_controller.initialize_framework_views" do |app| + # TODO: this should be combined with the logic for default config.action_controller.view_paths + view_path = ActionView::PathSet.type_cast(app.config.view_path, app.config.cache_classes) + ActionController::Base.view_paths = view_path if ActionController::Base.view_paths.blank? + end + + initializer "action_controller.initialize_metal" do |app| + Rails::Rack::Metal.requested_metals = app.config.metals + + app.config.middleware.insert_before(:"ActionDispatch::ParamsParser", + Rails::Rack::Metal, :if => Rails::Rack::Metal.metals.any?) + end + + # # Prepare dispatcher callbacks and run 'prepare' callbacks + initializer "action_controller.prepare_dispatcher" do |app| + # TODO: This used to say unless defined?(Dispatcher). Find out why and fix. + require 'rails/dispatcher' + + Dispatcher.define_dispatcher_callbacks(app.config.cache_classes) + + unless app.config.cache_classes + # Setup dev mode route reloading + routes_last_modified = app.routes_changed_at + reload_routes = lambda do + unless app.routes_changed_at == routes_last_modified + routes_last_modified = app.routes_changed_at + app.reload_routes! + end + end + ActionDispatch::Callbacks.before_dispatch { |callbacks| reload_routes.call } + end + end + + initializer "action_controller.notifications" do |app| + require 'active_support/notifications' + + ActiveSupport::Notifications.subscribe do |*args| + ActionController::Base.log_event(*args) if ActionController::Base.logger + end + end + + end +end diff --git a/actionpack/lib/action_view/rails.rb b/actionpack/lib/action_view/rails.rb deleted file mode 100644 index a90e0636b9..0000000000 --- a/actionpack/lib/action_view/rails.rb +++ /dev/null @@ -1,2 +0,0 @@ -require "action_view" -require "rails" \ No newline at end of file diff --git a/actionpack/lib/action_view/railtie.rb b/actionpack/lib/action_view/railtie.rb new file mode 100644 index 0000000000..a90e0636b9 --- /dev/null +++ b/actionpack/lib/action_view/railtie.rb @@ -0,0 +1,2 @@ +require "action_view" +require "rails" \ No newline at end of file diff --git a/activemodel/lib/active_model/rails.rb b/activemodel/lib/active_model/rails.rb deleted file mode 100644 index 63ffe5db63..0000000000 --- a/activemodel/lib/active_model/rails.rb +++ /dev/null @@ -1,2 +0,0 @@ -require "active_model" -require "rails" \ No newline at end of file diff --git a/activemodel/lib/active_model/railtie.rb b/activemodel/lib/active_model/railtie.rb new file mode 100644 index 0000000000..63ffe5db63 --- /dev/null +++ b/activemodel/lib/active_model/railtie.rb @@ -0,0 +1,2 @@ +require "active_model" +require "rails" \ No newline at end of file diff --git a/activerecord/lib/active_record/rails.rb b/activerecord/lib/active_record/rails.rb deleted file mode 100644 index f628c3ace0..0000000000 --- a/activerecord/lib/active_record/rails.rb +++ /dev/null @@ -1,71 +0,0 @@ -# For now, action_controller must always be present with -# rails, so let's make sure that it gets required before -# here. This is needed for correctly setting up the middleware. -# In the future, this might become an optional require. -require "active_record" -require "action_controller/rails" -require "rails" - -module ActiveRecord - class Plugin < Rails::Plugin - plugin_name :active_record - - rake_tasks do - load "active_record/railties/databases.rake" - end - - initializer "active_record.set_configs" do |app| - app.config.active_record.each do |k,v| - ActiveRecord::Base.send "#{k}=", v - end - end - - # This sets the database configuration from Configuration#database_configuration - # and then establishes the connection. - initializer "active_record.initialize_database" do |app| - ActiveRecord::Base.configurations = app.config.database_configuration - ActiveRecord::Base.establish_connection - end - - initializer "active_record.initialize_timezone" do - ActiveRecord::Base.time_zone_aware_attributes = true - ActiveRecord::Base.default_timezone = :utc - end - - # Expose database runtime to controller for logging. - initializer "active_record.log_runtime" do |app| - require "active_record/railties/controller_runtime" - ActionController::Base.send :include, ActiveRecord::Railties::ControllerRuntime - end - - # Setup database middleware after initializers have run - initializer "active_record.initialize_database_middleware" do |app| - middleware = app.config.middleware - if middleware.include?(ActiveRecord::SessionStore) - middleware.insert_before ActiveRecord::SessionStore, ActiveRecord::ConnectionAdapters::ConnectionManagement - middleware.insert_before ActiveRecord::SessionStore, ActiveRecord::QueryCache - else - middleware.use ActiveRecord::ConnectionAdapters::ConnectionManagement - middleware.use ActiveRecord::QueryCache - end - end - - initializer "active_record.load_observers" do - ActiveRecord::Base.instantiate_observers - end - - # TODO: ActiveRecord::Base.logger should delegate to its own config.logger - initializer "active_record.logger" do - ActiveRecord::Base.logger ||= ::Rails.logger - end - - initializer "active_record.notifications" do - require 'active_support/notifications' - - ActiveSupport::Notifications.subscribe("sql") do |name, before, after, instrumenter_id, payload| - ActiveRecord::Base.connection.log_info(payload[:sql], payload[:name], (after - before) * 1000) - end - end - - end -end diff --git a/activerecord/lib/active_record/railtie.rb b/activerecord/lib/active_record/railtie.rb new file mode 100644 index 0000000000..657ee738c0 --- /dev/null +++ b/activerecord/lib/active_record/railtie.rb @@ -0,0 +1,71 @@ +# For now, action_controller must always be present with +# rails, so let's make sure that it gets required before +# here. This is needed for correctly setting up the middleware. +# In the future, this might become an optional require. +require "active_record" +require "action_controller/railtie" +require "rails" + +module ActiveRecord + class Railtie < Rails::Railtie + plugin_name :active_record + + rake_tasks do + load "active_record/railties/databases.rake" + end + + initializer "active_record.set_configs" do |app| + app.config.active_record.each do |k,v| + ActiveRecord::Base.send "#{k}=", v + end + end + + # This sets the database configuration from Configuration#database_configuration + # and then establishes the connection. + initializer "active_record.initialize_database" do |app| + ActiveRecord::Base.configurations = app.config.database_configuration + ActiveRecord::Base.establish_connection + end + + initializer "active_record.initialize_timezone" do + ActiveRecord::Base.time_zone_aware_attributes = true + ActiveRecord::Base.default_timezone = :utc + end + + # Expose database runtime to controller for logging. + initializer "active_record.log_runtime" do |app| + require "active_record/railties/controller_runtime" + ActionController::Base.send :include, ActiveRecord::Railties::ControllerRuntime + end + + # Setup database middleware after initializers have run + initializer "active_record.initialize_database_middleware" do |app| + middleware = app.config.middleware + if middleware.include?(ActiveRecord::SessionStore) + middleware.insert_before ActiveRecord::SessionStore, ActiveRecord::ConnectionAdapters::ConnectionManagement + middleware.insert_before ActiveRecord::SessionStore, ActiveRecord::QueryCache + else + middleware.use ActiveRecord::ConnectionAdapters::ConnectionManagement + middleware.use ActiveRecord::QueryCache + end + end + + initializer "active_record.load_observers" do + ActiveRecord::Base.instantiate_observers + end + + # TODO: ActiveRecord::Base.logger should delegate to its own config.logger + initializer "active_record.logger" do + ActiveRecord::Base.logger ||= ::Rails.logger + end + + initializer "active_record.notifications" do + require 'active_support/notifications' + + ActiveSupport::Notifications.subscribe("sql") do |name, before, after, instrumenter_id, payload| + ActiveRecord::Base.connection.log_info(payload[:sql], payload[:name], (after - before) * 1000) + end + end + + end +end diff --git a/activeresource/lib/active_resource/rails.rb b/activeresource/lib/active_resource/rails.rb deleted file mode 100644 index 4f264c82b8..0000000000 --- a/activeresource/lib/active_resource/rails.rb +++ /dev/null @@ -1,2 +0,0 @@ -require "active_resource" -require "rails" \ No newline at end of file diff --git a/activeresource/lib/active_resource/railtie.rb b/activeresource/lib/active_resource/railtie.rb new file mode 100644 index 0000000000..4f264c82b8 --- /dev/null +++ b/activeresource/lib/active_resource/railtie.rb @@ -0,0 +1,2 @@ +require "active_resource" +require "rails" \ No newline at end of file diff --git a/railties/lib/rails.rb b/railties/lib/rails.rb index ab95edc676..d69e3eea6a 100644 --- a/railties/lib/rails.rb +++ b/railties/lib/rails.rb @@ -7,6 +7,7 @@ require 'action_dispatch' require 'rails/initializable' require 'rails/application' +require 'rails/railtie' require 'rails/plugin' require 'rails/railties_path' require 'rails/version' diff --git a/railties/lib/rails/all.rb b/railties/lib/rails/all.rb index a1ad72e98d..7dfe2b8b63 100644 --- a/railties/lib/rails/all.rb +++ b/railties/lib/rails/all.rb @@ -9,7 +9,7 @@ require "rails" active_resource ).each do |framework| begin - require "#{framework}/rails" + require "#{framework}/railtie" rescue LoadError end end \ No newline at end of file diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index 88f10727c5..457eef648c 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -92,8 +92,8 @@ module Rails def plugins @plugins ||= begin plugin_names = config.plugins || [:all] - Plugin.plugins.select { |p| plugin_names.include?(:all) || plugin_names.include?(p.plugin_name) }.map { |p| p.new } + - Plugin::Vendored.all(config.plugins || [:all], config.paths.vendor.plugins) + Railtie.plugins.select { |p| plugin_names.include?(:all) || plugin_names.include?(p.plugin_name) }.map { |p| p.new } + + Plugin.all(config.plugins || [:all], config.paths.vendor.plugins) end end diff --git a/railties/lib/rails/configuration.rb b/railties/lib/rails/configuration.rb index adb11e8257..f0a0d5e55e 100644 --- a/railties/lib/rails/configuration.rb +++ b/railties/lib/rails/configuration.rb @@ -3,7 +3,7 @@ require 'active_support/ordered_options' module Rails # Temporarily separate the plugin configuration class from the main # configuration class while this bit is being cleaned up. - class Plugin::Configuration + class Railtie::Configuration def self.default @default ||= new @@ -45,12 +45,12 @@ module Rails end def config_keys - ([ :active_support, :action_view, :action_mailer, :active_resource ] + - Plugin.plugin_names).map { |n| n.to_s }.uniq + ([ :active_support, :action_view ] + + Railtie.plugin_names).map { |n| n.to_s }.uniq end end - class Configuration < Plugin::Configuration + class Configuration < Railtie::Configuration attr_accessor :after_initialize_blocks, :cache_classes, :consider_all_requests_local, :dependency_loading, :gems, :load_once_paths, :logger, :metals, :plugins, diff --git a/railties/lib/rails/generators/rails/app/templates/config/boot.rb b/railties/lib/rails/generators/rails/app/templates/config/boot.rb index 6051a8104f..6de1725260 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/boot.rb +++ b/railties/lib/rails/generators/rails/app/templates/config/boot.rb @@ -15,12 +15,11 @@ end require 'rails/all' # To pick the frameworks you want, remove 'require "rails/all"' -# and list the frameworks that you want: +# and list the framework railties that you want: # -# require "rails" -# require "active_model/rails" -# require "active_record/rails" -# require "action_controller/rails" -# require "action_view/rails" -# require "action_mailer/rails" -# require "active_resource/rails" \ No newline at end of file +# require "active_model/railtie" +# require "active_record/railtie" +# require "action_controller/railtie" +# require "action_view/railtie" +# require "action_mailer/railtie" +# require "active_resource/railtie" \ No newline at end of file diff --git a/railties/lib/rails/plugin.rb b/railties/lib/rails/plugin.rb index e154e9b706..9cc6b9c35b 100644 --- a/railties/lib/rails/plugin.rb +++ b/railties/lib/rails/plugin.rb @@ -1,106 +1,64 @@ module Rails - class Plugin - include Initializable - - def self.plugin_name(plugin_name = nil) - @plugin_name ||= name.demodulize.underscore - @plugin_name = plugin_name if plugin_name - @plugin_name - end - - def self.inherited(klass) - @plugins ||= [] - @plugins << klass unless klass == Vendored - end - - def self.plugins - @plugins - end + class Plugin < Railtie + def self.all(list, paths) + plugins = [] + paths.each do |path| + Dir["#{path}/*"].each do |plugin_path| + plugin = new(plugin_path) + next unless list.include?(plugin.name) || list.include?(:all) + plugins << plugin + end + end - def self.plugin_names - plugins.map { |p| p.plugin_name } + plugins.sort_by do |p| + [list.index(p.name) || list.index(:all), p.name.to_s] + end end - def self.config - Configuration.default - end + attr_reader :name, :path - def self.rake_tasks(&blk) - @rake_tasks ||= [] - @rake_tasks << blk if blk - @rake_tasks + def initialize(path) + @name = File.basename(path).to_sym + @path = path end - def rake_tasks - self.class.rake_tasks + def load_paths + Dir["#{path}/{lib}", "#{path}/app/{models,controllers,helpers}"] end def load_tasks - return unless rake_tasks - rake_tasks.each { |blk| blk.call } + Dir["#{path}/**/tasks/**/*.rake"].sort.each { |ext| load ext } end - class Vendored < Plugin - def self.all(list, paths) - plugins = [] - paths.each do |path| - Dir["#{path}/*"].each do |plugin_path| - plugin = new(plugin_path) - next unless list.include?(plugin.name) || list.include?(:all) - plugins << plugin - end - end - - plugins.sort_by do |p| - [list.index(p.name) || list.index(:all), p.name.to_s] - end - end - - attr_reader :name, :path - - def initialize(path) - @name = File.basename(path).to_sym - @path = path - end - - def load_paths - Dir["#{path}/{lib}", "#{path}/app/{models,controllers,helpers}"] - end - - def load_tasks - Dir["#{path}/**/tasks/**/*.rake"].sort.each { |ext| load ext } - end - - initializer :add_to_load_path, :after => :set_autoload_paths do |app| - load_paths.each do |path| - $LOAD_PATH << path - require "active_support/dependencies" + initializer :add_to_load_path, :after => :set_autoload_paths do |app| + load_paths.each do |path| + $LOAD_PATH << path + require "active_support/dependencies" - ActiveSupport::Dependencies.load_paths << path + ActiveSupport::Dependencies.load_paths << path - unless app.config.reload_plugins - ActiveSupport::Dependencies.load_once_paths << path - end + unless app.config.reload_plugins + ActiveSupport::Dependencies.load_once_paths << path end end + end - initializer :load_init_rb, :before => :load_application_initializers do |app| - file = "#{@path}/init.rb" - config = app.config - eval File.read(file), binding, file if File.file?(file) - end + initializer :load_init_rb, :before => :load_application_initializers do |app| + file = "#{@path}/init.rb" + config = app.config + eval File.read(file), binding, file if File.file?(file) + end - initializer :add_view_paths, :after => :initialize_framework_views do - ActionController::Base.view_paths.concat ["#{path}/app/views"] if File.directory?("#{path}/app/views") - end + initializer :add_view_paths, :after => :initialize_framework_views do + ActionController::Base.view_paths.concat ["#{path}/app/views"] if File.directory?("#{path}/app/views") + end - initializer :add_routing_file, :after => :initialize_routing do |app| - routing_file = "#{path}/config/routes.rb" - if File.exist?(routing_file) - app.route_configuration_files << routing_file - app.reload_routes! - end + initializer :add_routing_file, :after => :initialize_routing do |app| + routing_file = "#{path}/config/routes.rb" + if File.exist?(routing_file) + app.route_configuration_files << routing_file + app.reload_routes! end end end -end +end \ No newline at end of file diff --git a/railties/lib/rails/railtie.rb b/railties/lib/rails/railtie.rb new file mode 100644 index 0000000000..ff28ade35d --- /dev/null +++ b/railties/lib/rails/railtie.rb @@ -0,0 +1,43 @@ +module Rails + class Railtie + include Initializable + + def self.plugin_name(plugin_name = nil) + @plugin_name ||= name.demodulize.underscore + @plugin_name = plugin_name if plugin_name + @plugin_name + end + + def self.inherited(klass) + @plugins ||= [] + @plugins << klass unless klass == Plugin + end + + def self.plugins + @plugins + end + + def self.plugin_names + plugins.map { |p| p.plugin_name } + end + + def self.config + Configuration.default + end + + def self.rake_tasks(&blk) + @rake_tasks ||= [] + @rake_tasks << blk if blk + @rake_tasks + end + + def rake_tasks + self.class.rake_tasks + end + + def load_tasks + return unless rake_tasks + rake_tasks.each { |blk| blk.call } + end + end +end diff --git a/railties/test/application/configuration_test.rb b/railties/test/application/configuration_test.rb index 1f04eb9f75..adb867ca6d 100644 --- a/railties/test/application/configuration_test.rb +++ b/railties/test/application/configuration_test.rb @@ -16,6 +16,7 @@ module ApplicationTests FileUtils.rm_rf(new_app) if File.directory?(new_app) build_app boot_rails + FileUtils.rm_rf("#{app_path}/config/environments") end test "the application root is set correctly" do diff --git a/railties/test/application/initializer_test.rb b/railties/test/application/initializer_test.rb index e03f9ffb00..3fd0b0e5df 100644 --- a/railties/test/application/initializer_test.rb +++ b/railties/test/application/initializer_test.rb @@ -7,6 +7,7 @@ module ApplicationTests def setup build_app boot_rails + FileUtils.rm_rf "#{app_path}/config/environments" end test "initializing an application adds the application paths to the load path" do @@ -160,6 +161,7 @@ module ApplicationTests def setup build_app boot_rails + FileUtils.rm_rf "#{app_path}/config/environments" end test "database middleware doesn't initialize when activerecord is not in frameworks" do diff --git a/railties/test/application/notifications_test.rb b/railties/test/application/notifications_test.rb index 8229e83147..b57e829cca 100644 --- a/railties/test/application/notifications_test.rb +++ b/railties/test/application/notifications_test.rb @@ -18,6 +18,7 @@ module ApplicationTests def setup build_app boot_rails + FileUtils.rm_rf("#{app_path}/config/environments") require "active_support/notifications" @events = [] diff --git a/railties/test/initializer/path_test.rb b/railties/test/initializer/path_test.rb index 26663d0f47..bfb1887d11 100644 --- a/railties/test/initializer/path_test.rb +++ b/railties/test/initializer/path_test.rb @@ -7,6 +7,7 @@ module InitializerTests def setup build_app boot_rails + FileUtils.rm_rf("#{app_path}/config/environments") add_to_config <<-RUBY config.root = "#{app_path}" config.after_initialize do @@ -14,7 +15,6 @@ module InitializerTests end RUBY use_frameworks [:action_controller, :action_view, :action_mailer, :active_record] - require "rails/all" require "#{app_path}/config/environment" @paths = Rails.application.config.paths end diff --git a/railties/test/plugins/configuration_test.rb b/railties/test/plugins/configuration_test.rb index 9616d1b2d0..25bf24eb3b 100644 --- a/railties/test/plugins/configuration_test.rb +++ b/railties/test/plugins/configuration_test.rb @@ -9,23 +9,23 @@ module PluginsTest end test "config is available to plugins" do - class Foo < Rails::Plugin ; end + class Foo < Rails::Railtie ; end assert_nil Foo.config.action_controller.foo end test "a config name is available for the plugin" do - class Foo < Rails::Plugin ; config.foo.greetings = "hello" ; end + class Foo < Rails::Railtie ; config.foo.greetings = "hello" ; end assert_equal "hello", Foo.config.foo.greetings end test "plugin configurations are available in the application" do - class Foo < Rails::Plugin ; config.foo.greetings = "hello" ; end + class Foo < Rails::Railtie ; config.foo.greetings = "hello" ; end require "#{app_path}/config/application" assert_equal "hello", AppTemplate::Application.config.foo.greetings end test "plugin config merges are deep" do - class Foo < Rails::Plugin ; config.foo.greetings = 'hello' ; end + class Foo < Rails::Railtie ; config.foo.greetings = 'hello' ; end class MyApp < Rails::Application config.foo.bar = "bar" end diff --git a/railties/test/plugins/framework_extension_test.rb b/railties/test/plugins/framework_extension_test.rb index 86e5cebfcd..c920db77aa 100644 --- a/railties/test/plugins/framework_extension_test.rb +++ b/railties/test/plugins/framework_extension_test.rb @@ -7,13 +7,14 @@ module PluginsTest def setup build_app boot_rails + FileUtils.rm_rf("#{app_path}/config/environments") require "rails/all" end test "rake_tasks block is executed when MyApp.load_tasks is called" do $ran_block = false - class MyPlugin < Rails::Plugin + class MyTie < Rails::Railtie rake_tasks do $ran_block = true end @@ -37,6 +38,7 @@ module PluginsTest def setup build_app boot_rails + FileUtils.rm_rf("#{app_path}/config/environments") end test "active_record extensions are applied to ActiveRecord" do -- cgit v1.2.3