From 7fcf8590e788cef8b64cc266f75931c418902ca9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 21 Jan 2010 23:14:20 +0100 Subject: Massive cleanup in Railties and load stack. --- railties/lib/rails.rb | 3 +- railties/lib/rails/application.rb | 64 +++++----- railties/lib/rails/bootstrap.rb | 62 +++------- railties/lib/rails/configuration.rb | 233 ++++++++++++++++++++---------------- railties/lib/rails/engine.rb | 107 +++++++++++++++++ railties/lib/rails/plugin.rb | 1 - railties/lib/rails/railtie.rb | 68 ++++++----- 7 files changed, 322 insertions(+), 216 deletions(-) create mode 100644 railties/lib/rails/engine.rb (limited to 'railties/lib') diff --git a/railties/lib/rails.rb b/railties/lib/rails.rb index 0bc7160815..93cf77ffd3 100644 --- a/railties/lib/rails.rb +++ b/railties/lib/rails.rb @@ -5,9 +5,10 @@ require 'active_support/core_ext/kernel/reporting' require 'active_support/core_ext/logger' require 'rails/initializable' -require 'rails/application' require 'rails/railtie' require 'rails/plugin' +require 'rails/engine' +require 'rails/application' require 'rails/railties_path' require 'rails/version' require 'rails/rack' diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index 5c9892c630..898dd0ad9b 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -1,14 +1,12 @@ require "fileutils" -require 'active_support/core_ext/module/delegation' module Rails - class Application + class Application < Engine include Initializable class << self - attr_writer :config - alias configure class_eval - delegate :initialize!, :load_tasks, :load_generators, :root, :to => :instance + alias :configure :class_eval + delegate :initialize!, :load_tasks, :load_generators, :to => :instance private :new def instance @@ -16,7 +14,16 @@ module Rails end def config - @config ||= Configuration.new(Plugin::Configuration.default) + @config ||= Configuration.new(root) + end + + def root + @root ||= find_root_with_file_flag("config.ru", Dir.pwd) + end + + def inherited(base) + super + Railtie.plugins.delete(base) end def routes @@ -24,8 +31,7 @@ module Rails end end - delegate :config, :routes, :to => :'self.class' - delegate :root, :middleware, :to => :config + delegate :routes, :to => :'self.class' attr_reader :route_configuration_files def initialize @@ -38,12 +44,7 @@ module Rails run_initializers(self) self end - - def require_environment - require config.environment_path - rescue LoadError - end - + def routes_changed_at routes_changed_at = nil @@ -59,6 +60,7 @@ module Rails end def reload_routes! + routes = Rails::Application.routes routes.disable_clear_and_finalize = true routes.clear! @@ -70,6 +72,12 @@ module Rails routes.disable_clear_and_finalize = false end + + def require_environment + require config.environment_path + rescue LoadError + end + def load_tasks require "rails/tasks" plugins.each { |p| p.load_tasks } @@ -114,37 +122,23 @@ module Rails app.call(env) end - initializer :load_application_initializers do - Dir["#{root}/config/initializers/**/*.rb"].sort.each do |initializer| - load(initializer) - end + initializer :build_middleware_stack, :after => :load_application_initializers do + app end - initializer :build_middleware_stack do - app + initializer :add_builtin_route do |app| + if Rails.env.development? + app.route_configuration_files << File.join(RAILTIES_PATH, 'builtin', 'routes.rb') + end end # Fires the user-supplied after_initialize block (Configuration#after_initialize) - initializer :after_initialize do + initializer :after_initialize, :after => :build_middleware_stack do config.after_initialize_blocks.each do |block| block.call end end - # Eager load application classes - initializer :load_application_classes do - next if $rails_rake_task - - if config.cache_classes - config.eager_load_paths.each do |load_path| - matcher = /\A#{Regexp.escape(load_path)}(.*)\.rb\Z/ - Dir.glob("#{load_path}/**/*.rb").sort.each do |file| - require_dependency file.sub(matcher, '\1') - end - end - end - end - # Disable dependency loading during request cycle initializer :disable_dependency_loading do if config.cache_classes && !config.dependency_loading diff --git a/railties/lib/rails/bootstrap.rb b/railties/lib/rails/bootstrap.rb index 5db663f9ef..688ab2f4b0 100644 --- a/railties/lib/rails/bootstrap.rb +++ b/railties/lib/rails/bootstrap.rb @@ -12,30 +12,25 @@ module Rails require "active_support/all" unless config.active_support.bare end - # Set the $LOAD_PATH based on the value of - # Configuration#load_paths. Duplicates are removed. - initializer :set_load_path do - config.paths.add_to_load_path - $LOAD_PATH.uniq! - end - - # Set the paths from which Rails will automatically load source files, and - # the load_once paths. - initializer :set_autoload_paths do - require 'active_support/dependencies' - ActiveSupport::Dependencies.load_paths = expand_load_path(config.load_paths) - ActiveSupport::Dependencies.load_once_paths = expand_load_path(config.load_once_paths) - - extra = ActiveSupport::Dependencies.load_once_paths - ActiveSupport::Dependencies.load_paths - unless extra.empty? - abort <<-end_error - load_once_paths must be a subset of the load_paths. - Extra items in load_once_paths: #{extra * ','} - end_error + initializer :initialize_logger do + Rails.logger ||= config.logger || begin + logger = ActiveSupport::BufferedLogger.new(config.paths.log.to_a.first) + logger.level = ActiveSupport::BufferedLogger.const_get(config.log_level.to_s.upcase) + logger.auto_flushing = false if Rails.env.production? + logger + rescue StandardError => e + logger = ActiveSupport::BufferedLogger.new(STDERR) + logger.level = ActiveSupport::BufferedLogger::WARN + logger.warn( + "Rails Error: Unable to access log file. Please ensure that #{config.log_path} exists and is chmod 0666. " + + "The log level has been raised to WARN and the output directed to STDERR until the problem is fixed." + ) + logger end + end - # Freeze the arrays so future modifications will fail rather than do nothing mysteriously - config.load_once_paths.freeze + initializer :container do + # FIXME This is just a dumb initializer used as hook end # Create tmp directories @@ -63,29 +58,6 @@ module Rails end end - initializer :initialize_logger do - Rails.logger ||= config.logger || begin - logger = ActiveSupport::BufferedLogger.new(config.log_path) - logger.level = ActiveSupport::BufferedLogger.const_get(config.log_level.to_s.upcase) - logger.auto_flushing = false if Rails.env.production? - logger - rescue StandardError => e - logger = ActiveSupport::BufferedLogger.new(STDERR) - logger.level = ActiveSupport::BufferedLogger::WARN - logger.warn( - "Rails Error: Unable to access log file. Please ensure that #{config.log_path} exists and is chmod 0666. " + - "The log level has been raised to WARN and the output directed to STDERR until the problem is fixed." - ) - logger - end - end - - # Sets the logger for dependencies and cache store. - initializer :initialize_framework_logging do - ActiveSupport::Dependencies.logger ||= Rails.logger - Rails.cache.logger ||= Rails.logger - end - # Sets the dependency loading mechanism based on the value of # Configuration#cache_classes. initializer :initialize_dependency_mechanism do diff --git a/railties/lib/rails/configuration.rb b/railties/lib/rails/configuration.rb index 7f1783a6b9..77248f2611 100644 --- a/railties/lib/rails/configuration.rb +++ b/railties/lib/rails/configuration.rb @@ -1,15 +1,9 @@ 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 Railtie::Configuration - def self.default - @default ||= new - end - - def self.default_middleware_stack - ActionDispatch::MiddlewareStack.new.tap do |middleware| + module SharedConfiguration + def self.middleware_stack + @default_middleware_stack ||= ActionDispatch::MiddlewareStack.new.tap do |middleware| middleware.use('::ActionDispatch::Static', lambda { Rails.public_path }, :if => lambda { Rails.application.config.serve_static_assets }) middleware.use('::Rack::Lock', :if => lambda { !ActionController::Base.allow_concurrency }) middleware.use('::Rack::Runtime') @@ -26,16 +20,23 @@ module Rails end end + def self.options + @options ||= Hash.new { |h,k| h[k] = ActiveSupport::OrderedOptions.new } + end + end + + # Temporarily separate the plugin configuration class from the main + # configuration class while this bit is being cleaned up. + class Railtie::Configuration + def self.default + @default ||= new + end + attr_reader :middleware - def initialize(base = nil) - if base - @options = base.options.dup - @middleware = base.middleware.dup - else - @options = Hash.new { |h,k| h[k] = ActiveSupport::OrderedOptions.new } - @middleware = self.class.default_middleware_stack - end + def initialize + @options = SharedConfiguration.options + @middleware = SharedConfiguration.middleware_stack end def respond_to?(name) @@ -61,27 +62,65 @@ module Rails /^(#{bits})(?:=)?$/ end + # TODO Remove :active_support as special case by adding a railtie + # for it and for I18n def config_keys - ([ :active_support, :action_view ] + - Railtie.plugin_names).map { |n| n.to_s }.uniq + ([:active_support] + Railtie.plugin_names).map { |n| n.to_s }.uniq end end - class Configuration < Railtie::Configuration + class Engine::Configuration < Railtie::Configuration + attr_reader :root + + def initialize(root) + @root = root + super() + end + + def paths + @paths ||= begin + paths = Rails::Application::Root.new(root) + paths.app "app", :load_path => true + paths.app_glob "app/*", :load_path => true, :eager_load => true + paths.app.controllers "app/controllers" + paths.app.metals "app/metal" + paths.app.views "app/views" + paths.lib "lib", :load_path => true + paths.config "config" + paths.config.environments "config/environments", :glob => "#{Rails.env}.rb" + paths.config.initializers "config/initializers" + paths.config.locales "config/locales" + paths.config.routes "config/routes.rb" + paths + end + end + + def eager_load_paths + @eager_load_paths ||= paths.eager_load + end + + def load_once_paths + @eager_load_paths ||= paths.load_once + end + + def load_paths + @load_paths ||= paths.load_paths + end + end + + class Configuration < Engine::Configuration attr_accessor :after_initialize_blocks, :cache_classes, :colorize_logging, :consider_all_requests_local, :dependency_loading, :filter_parameters, - :load_once_paths, :logger, :metals, :plugins, + :logger, :metals, :plugins, :preload_frameworks, :reload_plugins, :serve_static_assets, :time_zone, :whiny_nils attr_writer :cache_store, :controller_paths, - :database_configuration_file, :eager_load_paths, - :i18n, :load_paths, :log_level, :log_path, :paths, - :routes_configuration_file, :view_path + :database_configuration_file, + :i18n, :log_level, :log_path - def initialize(base = nil) + def initialize(*) super - @load_once_paths = [] @after_initialize_blocks = [] @filter_parameters = [] @dependency_loading = true @@ -92,46 +131,23 @@ module Rails @after_initialize_blocks << blk if blk end - def root - @root ||= begin - call_stack = caller.map { |p| p.split(':').first } - root_path = call_stack.detect { |p| p !~ %r[railties/lib/rails|rack/lib/rack] } - root_path = File.dirname(root_path) - - 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 - - root = File.exist?("#{root_path}/config.ru") ? root_path : Dir.pwd - - RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? - Pathname.new(root).expand_path : - Pathname.new(root).realpath - end - end - - def root=(root) - @root = Pathname.new(root).expand_path - 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 = super + paths.builtin_controller builtin_directories, :eager_load => true + paths.config.database "config/database.yml" + paths.log "log/#{Rails.env}.log" + paths.tmp "tmp" + paths.tmp.cache "tmp/cache" + paths.vendor "vendor", :load_path => true + paths.vendor.plugins "vendor/plugins" + + if File.exists?("#{root}/test/mocks/#{Rails.env}") + ActiveSupport::Deprecation.warn "\"RAILS_ROOT/test/mocks/#{Rails.env}\" won't be added " << + "automatically to load paths anymore in next releases." + paths.mocks_path "test/mocks/#{Rails.env}", :load_path => true + end + paths end end @@ -163,17 +179,61 @@ module Rails # YAML::load. def database_configuration require 'erb' - YAML::load(ERB.new(IO.read(database_configuration_file)).result) + YAML::load(ERB.new(IO.read(paths.config.database.to_a.first)).result) + end + + def view_path=(value) + ActiveSupport::Deprecation.warn "config.view_path= is deprecated, " << + "please do config.paths.app.views= instead", caller + paths.app.views = value + end + + def view_path + ActiveSupport::Deprecation.warn "config.view_path is deprecated, " << + "please do config.paths.app.views instead", caller + paths.app.views.to_a.first + end + + def routes_configuration_file=(value) + ActiveSupport::Deprecation.warn "config.routes_configuration_file= is deprecated, " << + "please do config.paths.config.routes= instead", caller + paths.config.routes = value end def routes_configuration_file - @routes_configuration_file ||= File.join(root, 'config', 'routes.rb') + ActiveSupport::Deprecation.warn "config.routes_configuration_file is deprecated, " << + "please do config.paths.config.routes instead", caller + paths.config.routes.to_a.first + end + + def database_configuration_file=(value) + ActiveSupport::Deprecation.warn "config.database_configuration_file= is deprecated, " << + "please do config.paths.config.database= instead", caller + paths.config.database = value + end + + def database_configuration_file + ActiveSupport::Deprecation.warn "config.database_configuration_file is deprecated, " << + "please do config.paths.config.database instead", caller + paths.config.database.to_a.first + end + + def log_path=(value) + ActiveSupport::Deprecation.warn "config.log_path= is deprecated, " << + "please do config.paths.log= instead", caller + paths.config.log = value end - def builtin_routes_configuration_file - @builtin_routes_configuration_file ||= File.join(RAILTIES_PATH, 'builtin', 'routes.rb') + def log_path + ActiveSupport::Deprecation.warn "config.log_path is deprecated, " << + "please do config.paths.log instead", caller + paths.config.log.to_a.first end + + + # TODO Router needs this, but this wouldn't work with engines. + # There is a high chance of plugins routes to be broken. def controller_paths @controller_paths ||= begin paths = [File.join(root, 'app', 'controllers')] @@ -192,46 +252,11 @@ module Rails end end - def database_configuration_file - @database_configuration_file ||= File.join(root, 'config', 'database.yml') - end - - def view_path - @view_path ||= File.join(root, 'app', 'views') - end - - def eager_load_paths - @eager_load_paths ||= ["#{root}/app/*"] - end - - 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}") - - # Followed by the standard includes. - paths.concat %w( - app - app/* - lib - vendor - ).map { |dir| "#{root}/#{dir}" } - - paths.concat builtin_directories - end - end - + # Include builtins only in the development environment. def builtin_directories - # Include builtins only in the development environment. Rails.env.development? ? Dir["#{RAILTIES_PATH}/builtin/*/"] : [] end - def log_path - @log_path ||= File.join(root, 'log', "#{Rails.env}.log") - end - def log_level @log_level ||= Rails.env.production? ? :info : :debug end diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb new file mode 100644 index 0000000000..21c78036bd --- /dev/null +++ b/railties/lib/rails/engine.rb @@ -0,0 +1,107 @@ +require 'active_support/core_ext/module/delegation' + +module Rails + # TODO Move I18n and views path setup + class Engine < Railtie + + class << self + attr_accessor :called_from + + def root + @root ||= find_root_with_file_flag("lib") + end + + def config + @config ||= Configuration.new(root) + end + + def inherited(base) + base.called_from = begin + call_stack = caller.map { |p| p.split(':').first } + File.dirname(call_stack.detect { |p| p !~ %r[railties/lib/rails|rack/lib/rack] }) + end + super + end + + protected + + def find_root_with_file_flag(flag, default=nil) + root_path = self.called_from + + while root_path && File.directory?(root_path) && !File.exist?("#{root_path}/#{flag}") + parent = File.dirname(root_path) + root_path = parent != root_path && parent + end + + root = File.exist?("#{root_path}/flag") ? root_path : default + + raise "Could not find root path for #{self}" unless root + + RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? + Pathname.new(root).expand_path : + Pathname.new(root).realpath + end + end + + delegate :root, :config, :to => :'self.class' + delegate :middleware, :to => :config + + # Add configured load paths to ruby load paths and remove duplicates. + initializer :set_load_path, :before => :container do + config.paths.add_to_load_path + $LOAD_PATH.uniq! + end + + # Set the paths from which Rails will automatically load source files, + # and the load_once paths. + initializer :set_autoload_paths, :before => :container do + require 'active_support/dependencies' + + ActiveSupport::Dependencies.load_paths = expand_load_path(config.load_paths) + ActiveSupport::Dependencies.load_once_paths = expand_load_path(config.load_once_paths) + + extra = ActiveSupport::Dependencies.load_once_paths - ActiveSupport::Dependencies.load_paths + + unless extra.empty? + abort <<-end_error + load_once_paths must be a subset of the load_paths. + Extra items in load_once_paths: #{extra * ','} + end_error + end + + # Freeze the arrays so future modifications will fail rather than do nothing mysteriously + config.load_once_paths.freeze + end + + initializer :load_application_initializers do + Dir["#{root}/config/initializers/**/*.rb"].sort.each do |initializer| + load(initializer) + end + end + + # Routing must be initialized after plugins to allow the former to extend the routes + initializer :initialize_routing do |app| + app.route_configuration_files.concat(config.paths.config.routes.to_a) + end + + # Eager load application classes + initializer :load_application_classes do |app| + next if $rails_rake_task + + if app.config.cache_classes + config.eager_load_paths.each do |load_path| + matcher = /\A#{Regexp.escape(load_path)}(.*)\.rb\Z/ + Dir.glob("#{load_path}/**/*.rb").sort.each do |file| + require_dependency file.sub(matcher, '\1') + end + end + end + end + + private + + def expand_load_path(load_paths) + load_paths.map { |path| Dir.glob(path.to_s) }.flatten.uniq + end + end +end \ No newline at end of file diff --git a/railties/lib/rails/plugin.rb b/railties/lib/rails/plugin.rb index c3b81bcd3e..dde06bfcbd 100644 --- a/railties/lib/rails/plugin.rb +++ b/railties/lib/rails/plugin.rb @@ -57,7 +57,6 @@ module Rails end end - # TODO Isn't it supposed to be :after => "action_controller.initialize_routing" ? initializer :add_routing_file, :after => :initialize_routing do |app| routing_file = "#{path}/config/routes.rb" if File.exist?(routing_file) diff --git a/railties/lib/rails/railtie.rb b/railties/lib/rails/railtie.rb index e3297148e5..a7f52b25e4 100644 --- a/railties/lib/rails/railtie.rb +++ b/railties/lib/rails/railtie.rb @@ -2,43 +2,51 @@ 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 + ABSTRACT_RAILTIES = %w(Rails::Plugin Rails::Engine Rails::Application) - def self.inherited(klass) - @plugins ||= [] - @plugins << klass unless klass == Plugin - end + class << self + def abstract_railtie?(base) + ABSTRACT_RAILTIES.include?(base.name) + end - def self.plugins - @plugins - end + def inherited(base) + @@plugins ||= [] + @@plugins << base unless abstract_railtie?(base) + end - def self.plugin_names - plugins.map { |p| p.plugin_name } - end + def plugin_name(plugin_name = nil) + @plugin_name ||= name.demodulize.underscore + @plugin_name = plugin_name if plugin_name + @plugin_name + end - def self.config - Configuration.default - end + def plugins + @@plugins + end - def self.subscriber(subscriber) - Rails::Subscriber.add(plugin_name, subscriber) - end + def plugin_names + plugins.map { |p| p.plugin_name } + end - def self.rake_tasks(&blk) - @rake_tasks ||= [] - @rake_tasks << blk if blk - @rake_tasks - end + def config + Configuration.default + end + + def subscriber(subscriber) + Rails::Subscriber.add(plugin_name, subscriber) + end + + def rake_tasks(&blk) + @rake_tasks ||= [] + @rake_tasks << blk if blk + @rake_tasks + end - def self.generators(&blk) - @generators ||= [] - @generators << blk if blk - @generators + def generators(&blk) + @generators ||= [] + @generators << blk if blk + @generators + end end def rake_tasks -- cgit v1.2.3 From 02c5137eadbb3530033d919b7aebeb6f4f389b83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Fri, 22 Jan 2010 01:10:31 +0100 Subject: Add view paths to Engine setup. --- railties/lib/rails/application.rb | 18 +++++++------- railties/lib/rails/configuration.rb | 36 +++++++++++++--------------- railties/lib/rails/engine.rb | 48 ++++++++++++++++++++++++------------- railties/lib/rails/railtie.rb | 1 + 4 files changed, 58 insertions(+), 45 deletions(-) (limited to 'railties/lib') diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index 898dd0ad9b..9be9584873 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -1,4 +1,4 @@ -require "fileutils" +require 'fileutils' module Rails class Application < Engine @@ -6,7 +6,7 @@ module Rails class << self alias :configure :class_eval - delegate :initialize!, :load_tasks, :load_generators, :to => :instance + delegate :initialize!, :load_tasks, :load_generators, :root, :to => :instance private :new def instance @@ -14,11 +14,11 @@ module Rails end def config - @config ||= Configuration.new(root) + @config ||= Configuration.new(original_root) end - def root - @root ||= find_root_with_file_flag("config.ru", Dir.pwd) + def original_root + @original_root ||= find_root_with_file_flag("config.ru", Dir.pwd) end def inherited(base) @@ -122,16 +122,16 @@ module Rails app.call(env) end - initializer :build_middleware_stack, :after => :load_application_initializers do - app - end - initializer :add_builtin_route do |app| if Rails.env.development? app.route_configuration_files << File.join(RAILTIES_PATH, 'builtin', 'routes.rb') end end + initializer :build_middleware_stack, :after => :load_application_initializers do + app + end + # Fires the user-supplied after_initialize block (Configuration#after_initialize) initializer :after_initialize, :after => :build_middleware_stack do config.after_initialize_blocks.each do |block| diff --git a/railties/lib/rails/configuration.rb b/railties/lib/rails/configuration.rb index 77248f2611..9176809dbd 100644 --- a/railties/lib/rails/configuration.rb +++ b/railties/lib/rails/configuration.rb @@ -70,7 +70,8 @@ module Rails end class Engine::Configuration < Railtie::Configuration - attr_reader :root + attr_reader :root + attr_accessor :eager_load_paths, :load_once_paths, :load_paths def initialize(root) @root = root @@ -79,14 +80,15 @@ module Rails def paths @paths ||= begin - paths = Rails::Application::Root.new(root) + paths = Rails::Application::Root.new(@root) paths.app "app", :load_path => true paths.app_glob "app/*", :load_path => true, :eager_load => true - paths.app.controllers "app/controllers" + paths.app.controllers "app/controllers", :eager_load => true paths.app.metals "app/metal" paths.app.views "app/views" paths.lib "lib", :load_path => true paths.config "config" + paths.config.environment "config/environments/#{Rails.env}.rb" paths.config.environments "config/environments", :glob => "#{Rails.env}.rb" paths.config.initializers "config/initializers" paths.config.locales "config/locales" @@ -95,6 +97,10 @@ module Rails end end + def root=(value) + @root = paths.path = Pathname.new(value).expand_path + end + def eager_load_paths @eager_load_paths ||= paths.eager_load end @@ -106,6 +112,10 @@ module Rails def load_paths @load_paths ||= paths.load_paths end + + def controller_paths + paths.app.controllers.to_a.uniq + end end class Configuration < Engine::Configuration @@ -115,9 +125,7 @@ module Rails :preload_frameworks, :reload_plugins, :serve_static_assets, :time_zone, :whiny_nils - attr_writer :cache_store, :controller_paths, - :database_configuration_file, - :i18n, :log_level, :log_path + attr_writer :cache_store, :controller_paths, :i18n, :log_level def initialize(*) super @@ -134,7 +142,7 @@ module Rails def paths @paths ||= begin paths = super - paths.builtin_controller builtin_directories, :eager_load => true + paths.app.controllers << builtin_directories paths.config.database "config/database.yml" paths.log "log/#{Rails.env}.log" paths.tmp "tmp" @@ -144,7 +152,7 @@ module Rails if File.exists?("#{root}/test/mocks/#{Rails.env}") ActiveSupport::Deprecation.warn "\"RAILS_ROOT/test/mocks/#{Rails.env}\" won't be added " << - "automatically to load paths anymore in next releases." + "automatically to load paths anymore in future releases" paths.mocks_path "test/mocks/#{Rails.env}", :load_path => true end @@ -230,18 +238,6 @@ module Rails paths.config.log.to_a.first end - - - # TODO Router needs this, but this wouldn't work with engines. - # There is a high chance of plugins routes to be broken. - def controller_paths - @controller_paths ||= begin - paths = [File.join(root, 'app', 'controllers')] - paths.concat builtin_directories - paths - end - end - def cache_store @cache_store ||= begin if File.exist?("#{root}/tmp/cache/") diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index 21c78036bd..a0139f9983 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -1,18 +1,19 @@ require 'active_support/core_ext/module/delegation' module Rails - # TODO Move I18n and views path setup + # TODO Move I18n here + # TODO Set routes namespaces class Engine < Railtie class << self attr_accessor :called_from - def root - @root ||= find_root_with_file_flag("lib") + def original_root + @original_root ||= find_root_with_file_flag("lib") end def config - @config ||= Configuration.new(root) + @config ||= Configuration.new(original_root) end def inherited(base) @@ -20,6 +21,7 @@ module Rails call_stack = caller.map { |p| p.split(':').first } File.dirname(call_stack.detect { |p| p !~ %r[railties/lib/rails|rack/lib/rack] }) end + super end @@ -33,7 +35,7 @@ module Rails root_path = parent != root_path && parent end - root = File.exist?("#{root_path}/flag") ? root_path : default + root = File.exist?("#{root_path}/#{flag}") ? root_path : default raise "Could not find root path for #{self}" unless root @@ -43,8 +45,8 @@ module Rails end end - delegate :root, :config, :to => :'self.class' - delegate :middleware, :to => :config + delegate :config, :to => :'self.class' + delegate :middleware, :root, :to => :config # Add configured load paths to ruby load paths and remove duplicates. initializer :set_load_path, :before => :container do @@ -57,10 +59,11 @@ module Rails initializer :set_autoload_paths, :before => :container do require 'active_support/dependencies' - ActiveSupport::Dependencies.load_paths = expand_load_path(config.load_paths) + ActiveSupport::Dependencies.load_paths = expand_load_path(config.load_paths) ActiveSupport::Dependencies.load_once_paths = expand_load_path(config.load_once_paths) - extra = ActiveSupport::Dependencies.load_once_paths - ActiveSupport::Dependencies.load_paths + extra = ActiveSupport::Dependencies.load_once_paths - + ActiveSupport::Dependencies.load_paths unless extra.empty? abort <<-end_error @@ -73,15 +76,24 @@ module Rails config.load_once_paths.freeze end - initializer :load_application_initializers do - Dir["#{root}/config/initializers/**/*.rb"].sort.each do |initializer| - load(initializer) - end + # Routing must be initialized after plugins to allow the former to extend the routes + initializer :add_routing_files do |app| + routes = select_existing(config.paths.config.routes) + app.route_configuration_files.concat(routes) end - # Routing must be initialized after plugins to allow the former to extend the routes - initializer :initialize_routing do |app| - app.route_configuration_files.concat(config.paths.config.routes.to_a) + initializer :add_view_paths do + views = select_existing(config.paths.app.views) + ActionController::Base.view_paths.concat(views) if defined? ActionController + ActionMailer::Base.view_paths.concat(views) if defined? ActionMailer + end + + initializer :load_application_initializers do + select_existing(config.paths.config.initializers).each do |initializers| + Dir["#{initializers}/**/*.rb"].sort.each do |initializer| + load(initializer) + end + end end # Eager load application classes @@ -100,6 +112,10 @@ module Rails private + def select_existing(paths) + paths.to_a.select { |path| File.exists?(path) }.uniq + end + def expand_load_path(load_paths) load_paths.map { |path| Dir.glob(path.to_s) }.flatten.uniq end diff --git a/railties/lib/rails/railtie.rb b/railties/lib/rails/railtie.rb index a7f52b25e4..5b97fabe40 100644 --- a/railties/lib/rails/railtie.rb +++ b/railties/lib/rails/railtie.rb @@ -14,6 +14,7 @@ module Rails @@plugins << base unless abstract_railtie?(base) end + # This should be called railtie_name and engine_name def plugin_name(plugin_name = nil) @plugin_name ||= name.demodulize.underscore @plugin_name = plugin_name if plugin_name -- cgit v1.2.3 From c9dc1ac95bc97800dd3deb82fe1cf6f98e27413d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?= Date: Fri, 22 Jan 2010 13:32:16 +1100 Subject: Updating functional tests to not compare equality with encoded, Mail::Message has an == operator --- railties/lib/generators/test_unit/mailer/templates/functional_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'railties/lib') diff --git a/railties/lib/generators/test_unit/mailer/templates/functional_test.rb b/railties/lib/generators/test_unit/mailer/templates/functional_test.rb index 4de94076e9..d7366fea5f 100644 --- a/railties/lib/generators/test_unit/mailer/templates/functional_test.rb +++ b/railties/lib/generators/test_unit/mailer/templates/functional_test.rb @@ -7,7 +7,7 @@ class <%= class_name %>Test < ActionMailer::TestCase @expected.body = read_fixture('<%= action %>') @expected.date = Time.now - assert_equal @expected.encoded, <%= class_name %>.create_<%= action %>(@expected.date).encoded + assert_equal @expected, <%= class_name %>.create_<%= action %>(@expected.date) end <% end -%> -- cgit v1.2.3 From 6fd7d1fc128f1d6d7bddaa6770739d7e936dd049 Mon Sep 17 00:00:00 2001 From: Mikel Lindsaar Date: Fri, 22 Jan 2010 19:38:50 +1100 Subject: Fixing typo in config.frameworks error --- railties/lib/rails/configuration.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'railties/lib') diff --git a/railties/lib/rails/configuration.rb b/railties/lib/rails/configuration.rb index 7f1783a6b9..21736a28eb 100644 --- a/railties/lib/rails/configuration.rb +++ b/railties/lib/rails/configuration.rb @@ -137,7 +137,7 @@ module Rails end def frameworks(*args) - raise "config.frameworks in no longer supported. See the generated " \ + raise "config.frameworks is no longer supported. See the generated " \ "config/boot.rb for steps on how to limit the frameworks that " \ "will be loaded" end -- cgit v1.2.3 From 4ae79367277954bf6616151b03cbe0660808f9bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Fri, 22 Jan 2010 16:24:44 +0100 Subject: Got tests working once again. --- railties/lib/rails/application.rb | 4 +--- railties/lib/rails/configuration.rb | 28 +++++++++++++++++----------- railties/lib/rails/engine.rb | 4 +++- railties/lib/rails/paths.rb | 18 ++++++++---------- 4 files changed, 29 insertions(+), 25 deletions(-) (limited to 'railties/lib') diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index 9be9584873..db1f62f381 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -2,8 +2,6 @@ require 'fileutils' module Rails class Application < Engine - include Initializable - class << self alias :configure :class_eval delegate :initialize!, :load_tasks, :load_generators, :root, :to => :instance @@ -122,7 +120,7 @@ module Rails app.call(env) end - initializer :add_builtin_route do |app| + initializer :add_builtin_route, :before => :build_middleware_stack do |app| if Rails.env.development? app.route_configuration_files << File.join(RAILTIES_PATH, 'builtin', 'routes.rb') end diff --git a/railties/lib/rails/configuration.rb b/railties/lib/rails/configuration.rb index 9176809dbd..01fab1e474 100644 --- a/railties/lib/rails/configuration.rb +++ b/railties/lib/rails/configuration.rb @@ -81,15 +81,13 @@ module Rails def paths @paths ||= begin paths = Rails::Application::Root.new(@root) - paths.app "app", :load_path => true - paths.app_glob "app/*", :load_path => true, :eager_load => true - paths.app.controllers "app/controllers", :eager_load => true - paths.app.metals "app/metal" + paths.app "app", :eager_load => true, :glob => "*" + paths.app.controllers "app/controllers", :eager_load => true + paths.app.metals "app/metal", :eager_load => true paths.app.views "app/views" paths.lib "lib", :load_path => true paths.config "config" - paths.config.environment "config/environments/#{Rails.env}.rb" - paths.config.environments "config/environments", :glob => "#{Rails.env}.rb" + paths.config.environment "config/environments", :glob => "#{Rails.env}.rb" paths.config.initializers "config/initializers" paths.config.locales "config/locales" paths.config.routes "config/routes.rb" @@ -112,10 +110,6 @@ module Rails def load_paths @load_paths ||= paths.load_paths end - - def controller_paths - paths.app.controllers.to_a.uniq - end end class Configuration < Engine::Configuration @@ -142,7 +136,7 @@ module Rails def paths @paths ||= begin paths = super - paths.app.controllers << builtin_directories + paths.app.controllers.concat(builtin_directories) paths.config.database "config/database.yml" paths.log "log/#{Rails.env}.log" paths.tmp "tmp" @@ -238,6 +232,18 @@ module Rails paths.config.log.to_a.first end + def controller_paths=(value) + ActiveSupport::Deprecation.warn "config.controller_paths= is deprecated, " << + "please do config.paths.app.controllers= instead", caller + paths.app.controllers = value + end + + def controller_paths + ActiveSupport::Deprecation.warn "config.controller_paths is deprecated, " << + "please do config.paths.app.controllers instead", caller + paths.app.controllers.to_a.uniq + end + def cache_store @cache_store ||= begin if File.exist?("#{root}/tmp/cache/") diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index a0139f9983..effbfee6c1 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -50,7 +50,9 @@ module Rails # Add configured load paths to ruby load paths and remove duplicates. initializer :set_load_path, :before => :container do - config.paths.add_to_load_path + expand_load_path(config.load_paths).reverse_each do |path| + $LOAD_PATH.unshift(path) if File.directory?(path) + end $LOAD_PATH.uniq! end diff --git a/railties/lib/rails/paths.rb b/railties/lib/rails/paths.rb index b3d105d8c7..d81af3c709 100644 --- a/railties/lib/rails/paths.rb +++ b/railties/lib/rails/paths.rb @@ -39,6 +39,7 @@ module Rails all_paths.map { |path| path.paths if path.eager_load? }.compact.flatten.uniq end + # TODO Discover why do we need to call uniq! here def all_paths @all_paths.uniq! @all_paths @@ -48,12 +49,6 @@ module Rails all_paths.map { |path| path.paths if path.load_path? }.compact.flatten.uniq end - def add_to_load_path - load_paths.reverse_each do |path| - $LOAD_PATH.unshift(path) if File.directory?(path) - end - end - def push(*) raise "Application root can only have one physical path" end @@ -74,7 +69,7 @@ module Rails @children = {} @root = root @paths = paths.flatten - @glob = @options[:glob] || "**/*.rb" + @glob = @options.delete(:glob) @load_once = @options[:load_once] @eager_load = @options[:eager_load] @@ -128,10 +123,13 @@ module Rails def paths raise "You need to set a path root" unless @root.path - - @paths.map do |path| - path.index('/') == 0 ? path : File.expand_path(File.join(@root.path, path)) + result = @paths.map do |p| + path = File.expand_path(p, @root.path) + @glob ? Dir[File.join(path, @glob)] : path end + result.flatten! + result.uniq! + result end alias to_a paths -- cgit v1.2.3 From c8cc8a987213bf90fe6922517d52befb7c0587a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Fri, 22 Jan 2010 20:44:29 +0100 Subject: Moved more configuration away from bootstrap. --- railties/lib/rails/all.rb | 1 + railties/lib/rails/application.rb | 39 ++++++++++++++++++++ railties/lib/rails/bootstrap.rb | 72 ------------------------------------- railties/lib/rails/configuration.rb | 4 +-- 4 files changed, 41 insertions(+), 75 deletions(-) (limited to 'railties/lib') diff --git a/railties/lib/rails/all.rb b/railties/lib/rails/all.rb index 7dfe2b8b63..b8292a9b7e 100644 --- a/railties/lib/rails/all.rb +++ b/railties/lib/rails/all.rb @@ -1,6 +1,7 @@ require "rails" %w( + active_support active_model active_record action_controller diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index db1f62f381..5c4112e1d7 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -137,6 +137,45 @@ module Rails end end + # Set the i18n configuration from config.i18n but special-case for the load_path which should be + # appended to what's already set instead of overwritten. + initializer :initialize_i18n do + require 'active_support/i18n' + + config.i18n.each do |setting, value| + if setting == :load_path + I18n.load_path += value + else + I18n.send("#{setting}=", value) + end + end + + ActionDispatch::Callbacks.to_prepare do + I18n.reload! + end + end + + initializer :set_clear_dependencies_hook do + unless config.cache_classes + ActionDispatch::Callbacks.after do + ActiveSupport::Dependencies.clear + end + end + end + + initializer :initialize_notifications do + require 'active_support/notifications' + + if config.colorize_logging == false + Rails::Subscriber.colorize_logging = false + config.generators.colorize_logging = false + end + + ActiveSupport::Notifications.subscribe do |*args| + Rails::Subscriber.dispatch(args) + end + end + # Disable dependency loading during request cycle initializer :disable_dependency_loading do if config.cache_classes && !config.dependency_loading diff --git a/railties/lib/rails/bootstrap.rb b/railties/lib/rails/bootstrap.rb index 688ab2f4b0..3473f2fcaa 100644 --- a/railties/lib/rails/bootstrap.rb +++ b/railties/lib/rails/bootstrap.rb @@ -33,13 +33,6 @@ module Rails # FIXME This is just a dumb initializer used as hook end - # Create tmp directories - initializer :ensure_tmp_directories_exist do - %w(cache pids sessions sockets).each do |dir_to_make| - FileUtils.mkdir_p(File.join(root, 'tmp', dir_to_make)) - end - end - # Preload all frameworks specified by the Configuration#frameworks. # Used by Passenger to ensure everything's loaded before forking and # to avoid autoload race conditions in JRuby. @@ -64,70 +57,5 @@ module Rails # TODO: Remove files from the $" and always use require ActiveSupport::Dependencies.mechanism = config.cache_classes ? :require : :load end - - # Loads support for "whiny nil" (noisy warnings when methods are invoked - # on +nil+ values) if Configuration#whiny_nils is true. - initializer :initialize_whiny_nils do - require 'active_support/whiny_nil' if config.whiny_nils - end - - # Sets the default value for Time.zone - # If assigned value cannot be matched to a TimeZone, an exception will be raised. - initializer :initialize_time_zone do - require 'active_support/core_ext/time/zones' - zone_default = Time.__send__(:get_zone, config.time_zone) - - unless zone_default - raise \ - 'Value assigned to config.time_zone not recognized.' + - 'Run "rake -D time" for a list of tasks for finding appropriate time zone names.' - end - - Time.zone_default = zone_default - end - - # Set the i18n configuration from config.i18n but special-case for the load_path which should be - # appended to what's already set instead of overwritten. - initializer :initialize_i18n do - require 'active_support/i18n' - - config.i18n.each do |setting, value| - if setting == :load_path - I18n.load_path += value - else - I18n.send("#{setting}=", value) - end - end - - ActionDispatch::Callbacks.to_prepare do - I18n.reload! - end - end - - initializer :set_clear_dependencies_hook do - unless config.cache_classes - ActionDispatch::Callbacks.after do - ActiveSupport::Dependencies.clear - end - end - end - - initializer :initialize_notifications do - require 'active_support/notifications' - - if config.colorize_logging == false - Rails::Subscriber.colorize_logging = false - config.generators.colorize_logging = false - end - - ActiveSupport::Notifications.subscribe do |*args| - Rails::Subscriber.dispatch(args) - end - end - - private - def expand_load_path(load_paths) - load_paths.map { |path| Dir.glob(path.to_s) }.flatten.uniq - end end end diff --git a/railties/lib/rails/configuration.rb b/railties/lib/rails/configuration.rb index 01fab1e474..3ba27d79a7 100644 --- a/railties/lib/rails/configuration.rb +++ b/railties/lib/rails/configuration.rb @@ -62,10 +62,8 @@ module Rails /^(#{bits})(?:=)?$/ end - # TODO Remove :active_support as special case by adding a railtie - # for it and for I18n def config_keys - ([:active_support] + Railtie.plugin_names).map { |n| n.to_s }.uniq + Railtie.plugin_names.map { |n| n.to_s }.uniq end end -- cgit v1.2.3 From 98240c49b05093d6d14b9384a9bd695b58eefb59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 23 Jan 2010 01:29:29 +0100 Subject: Get rid of initializers global and create i18n railtie. --- .../generators/rails/app/templates/config/boot.rb | 4 +- railties/lib/rails.rb | 3 +- railties/lib/rails/application.rb | 76 ++---- railties/lib/rails/bootstrap.rb | 51 ++-- railties/lib/rails/configuration.rb | 285 +++++++++------------ railties/lib/rails/engine.rb | 48 ++-- railties/lib/rails/initializable.rb | 19 +- 7 files changed, 209 insertions(+), 277 deletions(-) (limited to 'railties/lib') diff --git a/railties/lib/generators/rails/app/templates/config/boot.rb b/railties/lib/generators/rails/app/templates/config/boot.rb index 466e1e50ec..cbfa5ca3e9 100644 --- a/railties/lib/generators/rails/app/templates/config/boot.rb +++ b/railties/lib/generators/rails/app/templates/config/boot.rb @@ -19,7 +19,7 @@ require 'rails/all' # To pick the frameworks you want, remove 'require "rails/all"' # and list the framework railties that you want: # -# require "active_model/railtie" +# require "active_support/railtie" # require "active_record/railtie" # require "action_controller/railtie" # require "action_view/railtie" @@ -28,7 +28,7 @@ require 'rails/all' <% else -%> # Pick the frameworks you want: # require "active_record/railtie" -require "active_model/railtie" +require "active_support/railtie" require "action_controller/railtie" require "action_view/railtie" require "action_mailer/railtie" diff --git a/railties/lib/rails.rb b/railties/lib/rails.rb index 93cf77ffd3..e3aa9e4c97 100644 --- a/railties/lib/rails.rb +++ b/railties/lib/rails.rb @@ -8,6 +8,7 @@ require 'rails/initializable' require 'rails/railtie' require 'rails/plugin' require 'rails/engine' +require 'rails/bootstrap' require 'rails/application' require 'rails/railties_path' require 'rails/version' @@ -33,8 +34,6 @@ else end module Rails - autoload :Bootstrap, 'rails/bootstrap' - class << self def application @@application ||= nil diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index 5c4112e1d7..9b0f39feb1 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -2,6 +2,11 @@ require 'fileutils' module Rails class Application < Engine + + # TODO Clear up 2 way delegation flow between App class and instance. + # Infact just add a method_missing on the class. + # + # TODO I'd like to track the "default app" different using an inherited hook. class << self alias :configure :class_eval delegate :initialize!, :load_tasks, :load_generators, :root, :to => :instance @@ -70,10 +75,9 @@ module Rails routes.disable_clear_and_finalize = false end - def require_environment - require config.environment_path - rescue LoadError + environment = config.paths.config.environment.to_a.first + require environment if environment end def load_tasks @@ -92,13 +96,6 @@ module Rails plugins.each { |p| p.load_generators } end - def initializers - initializers = Bootstrap.new(self).initializers - plugins.each { |p| initializers += p.initializers } - initializers += super - initializers - end - # TODO: Fix this method. It loads all railties independent if :all is given # or not, otherwise frameworks are never loaded. def plugins @@ -120,59 +117,30 @@ module Rails app.call(env) end - initializer :add_builtin_route, :before => :build_middleware_stack do |app| + def initializers + my = super + hook = my.index { |i| i.name == :set_autoload_paths } + 1 + initializers = Bootstrap.new(self).initializers + initializers += my[0...hook] + plugins.each { |p| initializers += p.initializers } + initializers += my[hook..-1] + initializers + end + + initializer :add_builtin_route do |app| if Rails.env.development? app.route_configuration_files << File.join(RAILTIES_PATH, 'builtin', 'routes.rb') end end - initializer :build_middleware_stack, :after => :load_application_initializers do + initializer :build_middleware_stack do app end - # Fires the user-supplied after_initialize block (Configuration#after_initialize) - initializer :after_initialize, :after => :build_middleware_stack do + # Fires the user-supplied after_initialize block (config#after_initialize) + initializer :after_initialize do config.after_initialize_blocks.each do |block| - block.call - end - end - - # Set the i18n configuration from config.i18n but special-case for the load_path which should be - # appended to what's already set instead of overwritten. - initializer :initialize_i18n do - require 'active_support/i18n' - - config.i18n.each do |setting, value| - if setting == :load_path - I18n.load_path += value - else - I18n.send("#{setting}=", value) - end - end - - ActionDispatch::Callbacks.to_prepare do - I18n.reload! - end - end - - initializer :set_clear_dependencies_hook do - unless config.cache_classes - ActionDispatch::Callbacks.after do - ActiveSupport::Dependencies.clear - end - end - end - - initializer :initialize_notifications do - require 'active_support/notifications' - - if config.colorize_logging == false - Rails::Subscriber.colorize_logging = false - config.generators.colorize_logging = false - end - - ActiveSupport::Notifications.subscribe do |*args| - Rails::Subscriber.dispatch(args) + block.call(self) end end diff --git a/railties/lib/rails/bootstrap.rb b/railties/lib/rails/bootstrap.rb index 3473f2fcaa..7c33955b2b 100644 --- a/railties/lib/rails/bootstrap.rb +++ b/railties/lib/rails/bootstrap.rb @@ -1,5 +1,5 @@ module Rails - class Bootstrap #< Railtie + class Bootstrap include Initializable def initialize(application) @@ -12,6 +12,15 @@ module Rails require "active_support/all" unless config.active_support.bare end + # Preload all frameworks specified by the Configuration#frameworks. + # Used by Passenger to ensure everything's loaded before forking and + # to avoid autoload race conditions in JRuby. + initializer :preload_frameworks do + require 'active_support/dependencies' + ActiveSupport::Autoload.eager_autoload! if config.preload_frameworks + end + + # Initialize the logger early in the stack in case we need to log some deprecation. initializer :initialize_logger do Rails.logger ||= config.logger || begin logger = ActiveSupport::BufferedLogger.new(config.paths.log.to_a.first) @@ -29,32 +38,42 @@ module Rails end end - initializer :container do - # FIXME This is just a dumb initializer used as hook - end - - # Preload all frameworks specified by the Configuration#frameworks. - # Used by Passenger to ensure everything's loaded before forking and - # to avoid autoload race conditions in JRuby. - initializer :preload_frameworks do - ActiveSupport::Autoload.eager_autoload! if config.preload_frameworks - end - + # Initialize cache early in the stack so railties can make use of it. initializer :initialize_cache do unless defined?(RAILS_CACHE) silence_warnings { Object.const_set "RAILS_CACHE", ActiveSupport::Cache.lookup_store(config.cache_store) } if RAILS_CACHE.respond_to?(:middleware) - # Insert middleware to setup and teardown local cache for each request config.middleware.insert_after(:"Rack::Lock", RAILS_CACHE.middleware) end end end - # Sets the dependency loading mechanism based on the value of - # Configuration#cache_classes. + # Initialize rails subscriber on top of notifications. + initializer :initialize_subscriber do |app| + require 'active_support/notifications' + + if app.config.colorize_logging == false + Rails::Subscriber.colorize_logging = false + app.config.generators.colorize_logging = false + end + + ActiveSupport::Notifications.subscribe do |*args| + Rails::Subscriber.dispatch(args) + end + end + + initializer :set_clear_dependencies_hook do + unless config.cache_classes + ActionDispatch::Callbacks.after do + ActiveSupport::Dependencies.clear + end + end + end + + # Sets the dependency loading mechanism. + # TODO: Remove files from the $" and always use require. initializer :initialize_dependency_mechanism do - # TODO: Remove files from the $" and always use require ActiveSupport::Dependencies.mechanism = config.cache_classes ? :require : :load end end diff --git a/railties/lib/rails/configuration.rb b/railties/lib/rails/configuration.rb index 3ba27d79a7..76ca52867b 100644 --- a/railties/lib/rails/configuration.rb +++ b/railties/lib/rails/configuration.rb @@ -1,59 +1,112 @@ require 'active_support/ordered_options' module Rails - module SharedConfiguration - def self.middleware_stack - @default_middleware_stack ||= ActionDispatch::MiddlewareStack.new.tap do |middleware| - middleware.use('::ActionDispatch::Static', lambda { Rails.public_path }, :if => lambda { Rails.application.config.serve_static_assets }) - middleware.use('::Rack::Lock', :if => lambda { !ActionController::Base.allow_concurrency }) - middleware.use('::Rack::Runtime') - middleware.use('::Rails::Rack::Logger') - middleware.use('::ActionDispatch::ShowExceptions', lambda { ActionController::Base.consider_all_requests_local }) - middleware.use('::ActionDispatch::Callbacks', lambda { !Rails.application.config.cache_classes }) - middleware.use('::ActionDispatch::Cookies') - middleware.use(lambda { ActionController::Base.session_store }, lambda { ActionController::Base.session_options }) - middleware.use('::ActionDispatch::Flash', :if => lambda { ActionController::Base.session_store }) - middleware.use(lambda { Rails::Rack::Metal.new(Rails.application.config.paths.app.metals.to_a, Rails.application.config.metals) }) - middleware.use('ActionDispatch::ParamsParser') - middleware.use('::Rack::MethodOverride') - middleware.use('::ActionDispatch::Head') + module Shared + # Those configuration values are shared between railtie, engines and so forth. + module Configuration + def middleware + @@default_middleware_stack ||= ActionDispatch::MiddlewareStack.new.tap do |middleware| + middleware.use('::ActionDispatch::Static', lambda { Rails.public_path }, :if => lambda { Rails.application.config.serve_static_assets }) + middleware.use('::Rack::Lock', :if => lambda { !ActionController::Base.allow_concurrency }) + middleware.use('::Rack::Runtime') + middleware.use('::Rails::Rack::Logger') + middleware.use('::ActionDispatch::ShowExceptions', lambda { ActionController::Base.consider_all_requests_local }) + middleware.use('::ActionDispatch::Callbacks', lambda { !Rails.application.config.cache_classes }) + middleware.use('::ActionDispatch::Cookies') + middleware.use(lambda { ActionController::Base.session_store }, lambda { ActionController::Base.session_options }) + middleware.use('::ActionDispatch::Flash', :if => lambda { ActionController::Base.session_store }) + middleware.use(lambda { Rails::Rack::Metal.new(Rails.application.config.paths.app.metals.to_a, Rails.application.config.metals) }) + middleware.use('ActionDispatch::ParamsParser') + middleware.use('::Rack::MethodOverride') + middleware.use('::ActionDispatch::Head') + end + 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 + @@generators ||= GeneratorsConfiguration.new + if block_given? + yield @@generators + else + @@generators + end + end + + def after_initialize_blocks + @@after_initialize_blocks ||= [] + end + + def after_initialize(&blk) + after_initialize_blocks << blk if blk + end + + protected + + def options + @@options ||= Hash.new { |h,k| h[k] = ActiveSupport::OrderedOptions.new } end end - def self.options - @options ||= Hash.new { |h,k| h[k] = ActiveSupport::OrderedOptions.new } + class GeneratorsConfiguration #:nodoc: + attr_accessor :aliases, :options, :colorize_logging + + def initialize + @aliases = Hash.new { |h,k| h[k] = {} } + @options = Hash.new { |h,k| h[k] = {} } + @colorize_logging = true + end + + def method_missing(method, *args) + method = method.to_s.sub(/=$/, '').to_sym + + if method == :rails + namespace, configuration = :rails, args.shift + elsif args.first.is_a?(Hash) + namespace, configuration = method, args.shift + else + namespace, configuration = args.shift, args.shift + @options[:rails][method] = namespace + end + + if configuration + aliases = configuration.delete(:aliases) + @aliases[namespace].merge!(aliases) if aliases + @options[namespace].merge!(configuration) + end + end end end - # Temporarily separate the plugin configuration class from the main - # configuration class while this bit is being cleaned up. + # Holds Railtie basic configuration. It does not include configuration values + # related with load paths and the application specifics. class Railtie::Configuration + include Shared::Configuration + def self.default @default ||= new end - attr_reader :middleware - - def initialize - @options = SharedConfiguration.options - @middleware = SharedConfiguration.middleware_stack - end - def respond_to?(name) super || name.to_s =~ config_key_regexp end - protected - - attr_reader :options - private def method_missing(name, *args, &blk) if name.to_s =~ config_key_regexp - return $2 == '=' ? @options[$1] = args.first : @options[$1] + return $2 == '=' ? options[$1] = args.first : options[$1] end - super end @@ -68,8 +121,8 @@ module Rails end class Engine::Configuration < Railtie::Configuration - attr_reader :root - attr_accessor :eager_load_paths, :load_once_paths, :load_paths + attr_reader :root + attr_writer :eager_load_paths, :load_once_paths, :load_paths def initialize(root) @root = root @@ -86,8 +139,8 @@ module Rails paths.lib "lib", :load_path => true paths.config "config" paths.config.environment "config/environments", :glob => "#{Rails.env}.rb" - paths.config.initializers "config/initializers" - paths.config.locales "config/locales" + paths.config.initializers "config/initializers", :glob => "**/*.rb" + paths.config.locales "config/locales", :glob => "*.{rb,yml}" paths.config.routes "config/routes.rb" paths end @@ -111,41 +164,35 @@ module Rails end class Configuration < Engine::Configuration - attr_accessor :after_initialize_blocks, :cache_classes, :colorize_logging, - :consider_all_requests_local, :dependency_loading, :filter_parameters, - :logger, :metals, :plugins, - :preload_frameworks, :reload_plugins, :serve_static_assets, - :time_zone, :whiny_nils + attr_accessor :cache_classes, :cache_store, :colorize_logging, + :consider_all_requests_local, :dependency_loading, + :filter_parameters, :log_level, :logger, :metals, + :plugins, :preload_frameworks, :reload_plugins, + :serve_static_assets, :time_zone, :whiny_nils - attr_writer :cache_store, :controller_paths, :i18n, :log_level - def initialize(*) + def initialize(*) super - @after_initialize_blocks = [] - @filter_parameters = [] - @dependency_loading = true - @serve_static_assets = true - end - - def after_initialize(&blk) - @after_initialize_blocks << blk if blk - end + @filter_parameters = [] + @dependency_loading = true + @serve_static_assets = true + end def paths @paths ||= begin paths = super - paths.app.controllers.concat(builtin_directories) + paths.app.controllers << builtin_controller if builtin_controller paths.config.database "config/database.yml" paths.log "log/#{Rails.env}.log" paths.tmp "tmp" paths.tmp.cache "tmp/cache" - paths.vendor "vendor", :load_path => true + paths.vendor "vendor", :load_path => true paths.vendor.plugins "vendor/plugins" if File.exists?("#{root}/test/mocks/#{Rails.env}") ActiveSupport::Deprecation.warn "\"RAILS_ROOT/test/mocks/#{Rails.env}\" won't be added " << "automatically to load paths anymore in future releases" - paths.mocks_path "test/mocks/#{Rails.env}", :load_path => true + paths.mocks_path "test/mocks", :load_path => true, :glob => Rails.env end paths @@ -182,6 +229,29 @@ module Rails YAML::load(ERB.new(IO.read(paths.config.database.to_a.first)).result) end + def cache_store + @cache_store ||= begin + if File.exist?("#{root}/tmp/cache/") + [ :file_store, "#{root}/tmp/cache/" ] + else + :memory_store + end + end + end + + def builtin_controller + File.join(RAILTIES_PATH, "builtin", "rails_info") if Rails.env.development? + end + + def log_level + @log_level ||= Rails.env.production? ? :info : :debug + end + + def time_zone + @time_zone ||= "UTC" + end + + # Deprecated paths def view_path=(value) ActiveSupport::Deprecation.warn "config.view_path= is deprecated, " << "please do config.paths.app.views= instead", caller @@ -241,108 +311,5 @@ module Rails "please do config.paths.app.controllers instead", caller paths.app.controllers.to_a.uniq end - - def cache_store - @cache_store ||= begin - if File.exist?("#{root}/tmp/cache/") - [ :file_store, "#{root}/tmp/cache/" ] - else - :memory_store - end - end - end - - # Include builtins only in the development environment. - def builtin_directories - Rails.env.development? ? Dir["#{RAILTIES_PATH}/builtin/*/"] : [] - end - - def log_level - @log_level ||= Rails.env.production? ? :info : :debug - end - - def time_zone - @time_zone ||= "UTC" - end - - def i18n - @i18n ||= begin - i18n = ActiveSupport::OrderedOptions.new - i18n.load_path = [] - - if File.exist?(File.join(root, 'config', 'locales')) - i18n.load_path << Dir[File.join(root, 'config', 'locales', '*.{rb,yml}')] - i18n.load_path.flatten! - end - - i18n - end - end - - def environment_path - "#{root}/config/environments/#{Rails.env}.rb" - 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 - @generators ||= Generators.new - if block_given? - yield @generators - else - @generators - end - end - - # Allow Notifications queue to be modified or add subscriptions: - # - # config.notifications.queue = MyNewQueue.new - # - # config.notifications.subscribe /action_dispatch.show_exception/ do |*args| - # ExceptionDeliver.deliver_exception(args) - # end - # - def notifications - ActiveSupport::Notifications - end - - class Generators #:nodoc: - attr_accessor :aliases, :options, :colorize_logging - - def initialize - @aliases = Hash.new { |h,k| h[k] = {} } - @options = Hash.new { |h,k| h[k] = {} } - @colorize_logging = true - end - - def method_missing(method, *args) - method = method.to_s.sub(/=$/, '').to_sym - - if method == :rails - namespace, configuration = :rails, args.shift - elsif args.first.is_a?(Hash) - namespace, configuration = method, args.shift - else - namespace, configuration = args.shift, args.shift - @options[:rails][method] = namespace - end - - if configuration - aliases = configuration.delete(:aliases) - @aliases[namespace].merge!(aliases) if aliases - @options[namespace].merge!(configuration) - end - end - end end end diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index effbfee6c1..93f39f176c 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -4,7 +4,6 @@ module Rails # TODO Move I18n here # TODO Set routes namespaces class Engine < Railtie - class << self attr_accessor :called_from @@ -49,8 +48,8 @@ module Rails delegate :middleware, :root, :to => :config # Add configured load paths to ruby load paths and remove duplicates. - initializer :set_load_path, :before => :container do - expand_load_path(config.load_paths).reverse_each do |path| + initializer :set_load_path do + config.load_paths.reverse_each do |path| $LOAD_PATH.unshift(path) if File.directory?(path) end $LOAD_PATH.uniq! @@ -58,11 +57,9 @@ module Rails # Set the paths from which Rails will automatically load source files, # and the load_once paths. - initializer :set_autoload_paths, :before => :container do - require 'active_support/dependencies' - - ActiveSupport::Dependencies.load_paths = expand_load_path(config.load_paths) - ActiveSupport::Dependencies.load_once_paths = expand_load_path(config.load_once_paths) + initializer :set_autoload_paths do + ActiveSupport::Dependencies.load_paths.concat(config.load_paths) + ActiveSupport::Dependencies.load_once_paths.concat(config.load_once_paths) extra = ActiveSupport::Dependencies.load_once_paths - ActiveSupport::Dependencies.load_paths @@ -74,31 +71,32 @@ module Rails end_error end - # Freeze the arrays so future modifications will fail rather than do nothing mysteriously + # Freeze so future modifications will fail rather than do nothing mysteriously config.load_once_paths.freeze end - # Routing must be initialized after plugins to allow the former to extend the routes initializer :add_routing_files do |app| - routes = select_existing(config.paths.config.routes) - app.route_configuration_files.concat(routes) + config.paths.config.routes.to_a.each do |route| + app.route_configuration_files << route if File.exists?(route) + end + end + + initializer :add_locales do + config.i18n.load_path.concat(config.paths.config.locales.to_a) end initializer :add_view_paths do - views = select_existing(config.paths.app.views) - ActionController::Base.view_paths.concat(views) if defined? ActionController - ActionMailer::Base.view_paths.concat(views) if defined? ActionMailer + views = config.paths.app.views.to_a + ActionController::Base.view_paths.concat(views) if defined?(ActionController) + ActionMailer::Base.view_paths.concat(views) if defined?(ActionMailer) end initializer :load_application_initializers do - select_existing(config.paths.config.initializers).each do |initializers| - Dir["#{initializers}/**/*.rb"].sort.each do |initializer| - load(initializer) - end + config.paths.config.initializers.each do |initializer| + load(initializer) end end - # Eager load application classes initializer :load_application_classes do |app| next if $rails_rake_task @@ -111,15 +109,5 @@ module Rails end end end - - private - - def select_existing(paths) - paths.to_a.select { |path| File.exists?(path) }.uniq - end - - def expand_load_path(load_paths) - load_paths.map { |path| Dir.glob(path.to_s) }.flatten.uniq - end end end \ No newline at end of file diff --git a/railties/lib/rails/initializable.rb b/railties/lib/rails/initializable.rb index 8fcb254590..cea4a0fdf7 100644 --- a/railties/lib/rails/initializable.rb +++ b/railties/lib/rails/initializable.rb @@ -19,12 +19,6 @@ module Rails @options[:after] end - def global - @options[:global] - end - - alias global? global - def run(*args) @context.instance_exec(*args, &block) end @@ -71,7 +65,7 @@ module Rails def initializers @initializers ||= begin - initializers = self.class.initializers_for(:instance) + initializers = self.class.initializers_chain Collection.new(initializers.map { |i| i.bind(self) }) end end @@ -81,26 +75,23 @@ module Rails @initializers ||= [] end - def initializers_for(scope = :global) + def initializers_chain initializers = Collection.new ancestors.reverse_each do |klass| next unless klass.respond_to?(:initializers) - initializers = initializers + klass.initializers.select { |i| - (scope == :global) == !!i.global? - } + initializers = initializers + klass.initializers end initializers end def initializer(name, opts = {}, &blk) raise ArgumentError, "A block must be passed when defining an initializer" unless blk - @initializers ||= [] - @initializers << Initializer.new(name, nil, opts, &blk) + initializers << Initializer.new(name, nil, opts, &blk) end def run_initializers(*args) return if @ran - initializers_for(:global).each do |initializer| + initializers_chain.each do |initializer| instance_exec(*args, &initializer.block) end @ran = true -- cgit v1.2.3 From 4eab3aad8d256b868390b739b075bd38661339b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 23 Jan 2010 02:03:11 +0100 Subject: Ensure user set load paths have higher preference and move Bootstrap inside Application. --- railties/lib/rails.rb | 1 - railties/lib/rails/application.rb | 121 +++++++++++++++++++++++++++++------- railties/lib/rails/bootstrap.rb | 80 ------------------------ railties/lib/rails/configuration.rb | 3 +- railties/lib/rails/engine.rb | 4 +- 5 files changed, 103 insertions(+), 106 deletions(-) delete mode 100644 railties/lib/rails/bootstrap.rb (limited to 'railties/lib') diff --git a/railties/lib/rails.rb b/railties/lib/rails.rb index e3aa9e4c97..29afb672b6 100644 --- a/railties/lib/rails.rb +++ b/railties/lib/rails.rb @@ -8,7 +8,6 @@ require 'rails/initializable' require 'rails/railtie' require 'rails/plugin' require 'rails/engine' -require 'rails/bootstrap' require 'rails/application' require 'rails/railties_path' require 'rails/version' diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index 9b0f39feb1..305d1c73e0 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -2,11 +2,14 @@ require 'fileutils' module Rails class Application < Engine - # TODO Clear up 2 way delegation flow between App class and instance. # Infact just add a method_missing on the class. # # TODO I'd like to track the "default app" different using an inherited hook. + # + # TODO Check helpers works as expected + # + # TODO Check routes namespaces class << self alias :configure :class_eval delegate :initialize!, :load_tasks, :load_generators, :root, :to => :instance @@ -118,36 +121,112 @@ module Rails end def initializers - my = super - hook = my.index { |i| i.name == :set_autoload_paths } + 1 - initializers = Bootstrap.new(self).initializers - initializers += my[0...hook] + initializers = Bootstrap.initializers + initializers += super plugins.each { |p| initializers += p.initializers } - initializers += my[hook..-1] + initializers += Finisher.initializers initializers end - initializer :add_builtin_route do |app| - if Rails.env.development? - app.route_configuration_files << File.join(RAILTIES_PATH, 'builtin', 'routes.rb') + module Bootstrap + include Initializable + + initializer :load_all_active_support do |app| + require "active_support/all" unless app.config.active_support.bare end - end - initializer :build_middleware_stack do - app - end + # Preload all frameworks specified by the Configuration#frameworks. + # Used by Passenger to ensure everything's loaded before forking and + # to avoid autoload race conditions in JRuby. + initializer :preload_frameworks do |app| + require 'active_support/dependencies' + ActiveSupport::Autoload.eager_autoload! if app.config.preload_frameworks + end + + # Initialize the logger early in the stack in case we need to log some deprecation. + initializer :initialize_logger do |app| + Rails.logger ||= app.config.logger || begin + path = app.config.paths.log.to_a.first + logger = ActiveSupport::BufferedLogger.new(path) + logger.level = ActiveSupport::BufferedLogger.const_get(app.config.log_level.to_s.upcase) + logger.auto_flushing = false if Rails.env.production? + logger + rescue StandardError => e + logger = ActiveSupport::BufferedLogger.new(STDERR) + logger.level = ActiveSupport::BufferedLogger::WARN + logger.warn( + "Rails Error: Unable to access log file. Please ensure that #{path} exists and is chmod 0666. " + + "The log level has been raised to WARN and the output directed to STDERR until the problem is fixed." + ) + logger + end + end + + # Initialize cache early in the stack so railties can make use of it. + initializer :initialize_cache do |app| + unless defined?(RAILS_CACHE) + silence_warnings { Object.const_set "RAILS_CACHE", ActiveSupport::Cache.lookup_store(app.config.cache_store) } + + if RAILS_CACHE.respond_to?(:middleware) + app.config.middleware.insert_after(:"Rack::Lock", RAILS_CACHE.middleware) + end + end + end + + # Initialize rails subscriber on top of notifications. + initializer :initialize_subscriber do |app| + require 'active_support/notifications' + + if app.config.colorize_logging == false + Rails::Subscriber.colorize_logging = false + app.config.generators.colorize_logging = false + end - # Fires the user-supplied after_initialize block (config#after_initialize) - initializer :after_initialize do - config.after_initialize_blocks.each do |block| - block.call(self) + ActiveSupport::Notifications.subscribe do |*args| + Rails::Subscriber.dispatch(args) + end + end + + initializer :set_clear_dependencies_hook do |app| + unless app.config.cache_classes + ActionDispatch::Callbacks.after do + ActiveSupport::Dependencies.clear + end + end + end + + # Sets the dependency loading mechanism. + # TODO: Remove files from the $" and always use require. + initializer :initialize_dependency_mechanism do |app| + ActiveSupport::Dependencies.mechanism = app.config.cache_classes ? :require : :load end end - # Disable dependency loading during request cycle - initializer :disable_dependency_loading do - if config.cache_classes && !config.dependency_loading - ActiveSupport::Dependencies.unhook! + module Finisher + include Initializable + + initializer :add_builtin_route do |app| + if Rails.env.development? + app.route_configuration_files << File.join(RAILTIES_PATH, 'builtin', 'routes.rb') + end + end + + initializer :build_middleware_stack do |app| + app.app + end + + # Fires the user-supplied after_initialize block (config#after_initialize) + initializer :after_initialize do |app| + app.config.after_initialize_blocks.each do |block| + block.call(app) + end + end + + # Disable dependency loading during request cycle + initializer :disable_dependency_loading do |app| + if app.config.cache_classes && !app.config.dependency_loading + ActiveSupport::Dependencies.unhook! + end end end end diff --git a/railties/lib/rails/bootstrap.rb b/railties/lib/rails/bootstrap.rb deleted file mode 100644 index 7c33955b2b..0000000000 --- a/railties/lib/rails/bootstrap.rb +++ /dev/null @@ -1,80 +0,0 @@ -module Rails - class Bootstrap - include Initializable - - def initialize(application) - @application = application - end - - delegate :config, :root, :to => :'@application' - - initializer :load_all_active_support do - require "active_support/all" unless config.active_support.bare - end - - # Preload all frameworks specified by the Configuration#frameworks. - # Used by Passenger to ensure everything's loaded before forking and - # to avoid autoload race conditions in JRuby. - initializer :preload_frameworks do - require 'active_support/dependencies' - ActiveSupport::Autoload.eager_autoload! if config.preload_frameworks - end - - # Initialize the logger early in the stack in case we need to log some deprecation. - initializer :initialize_logger do - Rails.logger ||= config.logger || begin - logger = ActiveSupport::BufferedLogger.new(config.paths.log.to_a.first) - logger.level = ActiveSupport::BufferedLogger.const_get(config.log_level.to_s.upcase) - logger.auto_flushing = false if Rails.env.production? - logger - rescue StandardError => e - logger = ActiveSupport::BufferedLogger.new(STDERR) - logger.level = ActiveSupport::BufferedLogger::WARN - logger.warn( - "Rails Error: Unable to access log file. Please ensure that #{config.log_path} exists and is chmod 0666. " + - "The log level has been raised to WARN and the output directed to STDERR until the problem is fixed." - ) - logger - end - end - - # Initialize cache early in the stack so railties can make use of it. - initializer :initialize_cache do - unless defined?(RAILS_CACHE) - silence_warnings { Object.const_set "RAILS_CACHE", ActiveSupport::Cache.lookup_store(config.cache_store) } - - if RAILS_CACHE.respond_to?(:middleware) - config.middleware.insert_after(:"Rack::Lock", RAILS_CACHE.middleware) - end - end - end - - # Initialize rails subscriber on top of notifications. - initializer :initialize_subscriber do |app| - require 'active_support/notifications' - - if app.config.colorize_logging == false - Rails::Subscriber.colorize_logging = false - app.config.generators.colorize_logging = false - end - - ActiveSupport::Notifications.subscribe do |*args| - Rails::Subscriber.dispatch(args) - end - end - - initializer :set_clear_dependencies_hook do - unless config.cache_classes - ActionDispatch::Callbacks.after do - ActiveSupport::Dependencies.clear - end - end - end - - # Sets the dependency loading mechanism. - # TODO: Remove files from the $" and always use require. - initializer :initialize_dependency_mechanism do - ActiveSupport::Dependencies.mechanism = config.cache_classes ? :require : :load - end - end -end diff --git a/railties/lib/rails/configuration.rb b/railties/lib/rails/configuration.rb index 76ca52867b..da2206d6a2 100644 --- a/railties/lib/rails/configuration.rb +++ b/railties/lib/rails/configuration.rb @@ -170,13 +170,12 @@ module Rails :plugins, :preload_frameworks, :reload_plugins, :serve_static_assets, :time_zone, :whiny_nils - def initialize(*) super @filter_parameters = [] @dependency_loading = true @serve_static_assets = true - end + end def paths @paths ||= begin diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index 93f39f176c..03c78b6d4b 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -77,12 +77,12 @@ module Rails initializer :add_routing_files do |app| config.paths.config.routes.to_a.each do |route| - app.route_configuration_files << route if File.exists?(route) + app.route_configuration_files.unshift(route) if File.exists?(route) end end initializer :add_locales do - config.i18n.load_path.concat(config.paths.config.locales.to_a) + config.i18n.load_path.unshift(*config.paths.config.locales.to_a) end initializer :add_view_paths do -- cgit v1.2.3 From 80130d1201c3bf9dc17b0e1fcd81c6b22e893b0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 23 Jan 2010 15:05:13 +0100 Subject: Extract routes reloading responsibilities from application and load them just upon a request. --- railties/lib/rails/application.rb | 67 +++++++---------------- railties/lib/rails/application/routes_reloader.rb | 46 ++++++++++++++++ railties/lib/rails/configuration.rb | 5 +- railties/lib/rails/console_app.rb | 3 +- railties/lib/rails/engine.rb | 6 +- railties/lib/rails/plugin.rb | 3 +- 6 files changed, 72 insertions(+), 58 deletions(-) create mode 100644 railties/lib/rails/application/routes_reloader.rb (limited to 'railties/lib') diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index 305d1c73e0..46d6bb4e7c 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -2,25 +2,20 @@ require 'fileutils' module Rails class Application < Engine - # TODO Clear up 2 way delegation flow between App class and instance. - # Infact just add a method_missing on the class. - # - # TODO I'd like to track the "default app" different using an inherited hook. - # + autoload :RoutesReloader, 'rails/application/routes_reloader' + # TODO Check helpers works as expected - # # TODO Check routes namespaces class << self - alias :configure :class_eval - delegate :initialize!, :load_tasks, :load_generators, :root, :to => :instance - private :new + alias :configure :class_eval + def instance @instance ||= new end def config - @config ||= Configuration.new(original_root) + @config ||= Configuration.new(self.original_root) end def original_root @@ -30,52 +25,35 @@ module Rails def inherited(base) super Railtie.plugins.delete(base) + Rails.application = base.instance end - def routes - ActionController::Routing::Routes + protected + + def method_missing(*args, &block) + instance.send(*args, &block) end end - delegate :routes, :to => :'self.class' - attr_reader :route_configuration_files - def initialize require_environment - Rails.application ||= self - @route_configuration_files = [] end - def initialize! - run_initializers(self) - self + def routes + ActionController::Routing::Routes end - - def routes_changed_at - routes_changed_at = nil - - route_configuration_files.each do |config| - config_changed_at = File.stat(config).mtime - if routes_changed_at.nil? || config_changed_at > routes_changed_at - routes_changed_at = config_changed_at - end - end - - routes_changed_at + def routes_reloader + @routes_reloader ||= RoutesReloader.new(config) end def reload_routes! - routes = Rails::Application.routes - routes.disable_clear_and_finalize = true - - routes.clear! - route_configuration_files.each { |config| load(config) } - routes.finalize! + routes_reloader.reload! + end - nil - ensure - routes.disable_clear_and_finalize = false + def initialize! + run_initializers(self) + self end def require_environment @@ -109,10 +87,7 @@ module Rails end def app - @app ||= begin - reload_routes! - middleware.build(routes) - end + @app ||= middleware.build(routes) end def call(env) @@ -207,7 +182,7 @@ module Rails initializer :add_builtin_route do |app| if Rails.env.development? - app.route_configuration_files << File.join(RAILTIES_PATH, 'builtin', 'routes.rb') + app.config.action_dispatch.route_files << File.join(RAILTIES_PATH, 'builtin', 'routes.rb') end end diff --git a/railties/lib/rails/application/routes_reloader.rb b/railties/lib/rails/application/routes_reloader.rb new file mode 100644 index 0000000000..621bb1ccbd --- /dev/null +++ b/railties/lib/rails/application/routes_reloader.rb @@ -0,0 +1,46 @@ +module Rails + class Application + class RoutesReloader + attr_reader :config + + def initialize(config) + @config, @last_change_at = config, nil + end + + def changed_at + routes_changed_at = nil + + config.action_dispatch.route_files.each do |config| + config_changed_at = File.stat(config).mtime + + if routes_changed_at.nil? || config_changed_at > routes_changed_at + routes_changed_at = config_changed_at + end + end + + routes_changed_at + end + + def reload! + routes = Rails::Application.routes + routes.disable_clear_and_finalize = true + + routes.clear! + config.action_dispatch.route_files.each { |config| load(config) } + routes.finalize! + + nil + ensure + routes.disable_clear_and_finalize = false + end + + def reload_if_changed + current_change_at = changed_at + if @last_change_at != current_change_at + @last_change_at = current_change_at + reload! + end + end + end + end +end \ No newline at end of file diff --git a/railties/lib/rails/configuration.rb b/railties/lib/rails/configuration.rb index da2206d6a2..5d0c7fd4b4 100644 --- a/railties/lib/rails/configuration.rb +++ b/railties/lib/rails/configuration.rb @@ -213,10 +213,7 @@ module Rails self.preload_frameworks = true self.cache_classes = true self.dependency_loading = false - - if respond_to?(:action_controller) - action_controller.allow_concurrency = true - end + action_controller.allow_concurrency = true if respond_to?(:action_controller) self end diff --git a/railties/lib/rails/console_app.rb b/railties/lib/rails/console_app.rb index 2c4a7a51e8..98f7f774a9 100644 --- a/railties/lib/rails/console_app.rb +++ b/railties/lib/rails/console_app.rb @@ -26,7 +26,6 @@ end #reloads the environment def reload! puts "Reloading..." - ActionDispatch::Callbacks.new(lambda {}, true) - Rails.application.reload_routes! + ActionDispatch::Callbacks.new(lambda {}, true).call({}) true end diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index 03c78b6d4b..aaa669ef32 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -1,8 +1,6 @@ require 'active_support/core_ext/module/delegation' module Rails - # TODO Move I18n here - # TODO Set routes namespaces class Engine < Railtie class << self attr_accessor :called_from @@ -75,9 +73,9 @@ module Rails config.load_once_paths.freeze end - initializer :add_routing_files do |app| + initializer :add_routing_files do config.paths.config.routes.to_a.each do |route| - app.route_configuration_files.unshift(route) if File.exists?(route) + config.action_dispatch.route_files.unshift(route) if File.exists?(route) end end diff --git a/railties/lib/rails/plugin.rb b/railties/lib/rails/plugin.rb index dde06bfcbd..43632127fc 100644 --- a/railties/lib/rails/plugin.rb +++ b/railties/lib/rails/plugin.rb @@ -60,8 +60,7 @@ module Rails 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! + app.config.action_dispatch.route_files.unshift(routing_file) end end end -- cgit v1.2.3 From 4f036032152518791d379f47260236f619713fbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 23 Jan 2010 16:07:20 +0100 Subject: Break application.rb file in smaller chunks. --- railties/lib/rails/application.rb | 104 +--------------------------- railties/lib/rails/application/bootstrap.rb | 77 ++++++++++++++++++++ railties/lib/rails/application/finisher.rb | 31 +++++++++ 3 files changed, 110 insertions(+), 102 deletions(-) create mode 100644 railties/lib/rails/application/bootstrap.rb create mode 100644 railties/lib/rails/application/finisher.rb (limited to 'railties/lib') diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index 46d6bb4e7c..81606d09e4 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -2,6 +2,8 @@ require 'fileutils' module Rails class Application < Engine + autoload :Bootstrap, 'rails/application/bootstrap' + autoload :Finisher, 'rails/application/finisher' autoload :RoutesReloader, 'rails/application/routes_reloader' # TODO Check helpers works as expected @@ -102,107 +104,5 @@ module Rails initializers += Finisher.initializers initializers end - - module Bootstrap - include Initializable - - initializer :load_all_active_support do |app| - require "active_support/all" unless app.config.active_support.bare - end - - # Preload all frameworks specified by the Configuration#frameworks. - # Used by Passenger to ensure everything's loaded before forking and - # to avoid autoload race conditions in JRuby. - initializer :preload_frameworks do |app| - require 'active_support/dependencies' - ActiveSupport::Autoload.eager_autoload! if app.config.preload_frameworks - end - - # Initialize the logger early in the stack in case we need to log some deprecation. - initializer :initialize_logger do |app| - Rails.logger ||= app.config.logger || begin - path = app.config.paths.log.to_a.first - logger = ActiveSupport::BufferedLogger.new(path) - logger.level = ActiveSupport::BufferedLogger.const_get(app.config.log_level.to_s.upcase) - logger.auto_flushing = false if Rails.env.production? - logger - rescue StandardError => e - logger = ActiveSupport::BufferedLogger.new(STDERR) - logger.level = ActiveSupport::BufferedLogger::WARN - logger.warn( - "Rails Error: Unable to access log file. Please ensure that #{path} exists and is chmod 0666. " + - "The log level has been raised to WARN and the output directed to STDERR until the problem is fixed." - ) - logger - end - end - - # Initialize cache early in the stack so railties can make use of it. - initializer :initialize_cache do |app| - unless defined?(RAILS_CACHE) - silence_warnings { Object.const_set "RAILS_CACHE", ActiveSupport::Cache.lookup_store(app.config.cache_store) } - - if RAILS_CACHE.respond_to?(:middleware) - app.config.middleware.insert_after(:"Rack::Lock", RAILS_CACHE.middleware) - end - end - end - - # Initialize rails subscriber on top of notifications. - initializer :initialize_subscriber do |app| - require 'active_support/notifications' - - if app.config.colorize_logging == false - Rails::Subscriber.colorize_logging = false - app.config.generators.colorize_logging = false - end - - ActiveSupport::Notifications.subscribe do |*args| - Rails::Subscriber.dispatch(args) - end - end - - initializer :set_clear_dependencies_hook do |app| - unless app.config.cache_classes - ActionDispatch::Callbacks.after do - ActiveSupport::Dependencies.clear - end - end - end - - # Sets the dependency loading mechanism. - # TODO: Remove files from the $" and always use require. - initializer :initialize_dependency_mechanism do |app| - ActiveSupport::Dependencies.mechanism = app.config.cache_classes ? :require : :load - end - end - - module Finisher - include Initializable - - initializer :add_builtin_route do |app| - if Rails.env.development? - app.config.action_dispatch.route_files << File.join(RAILTIES_PATH, 'builtin', 'routes.rb') - end - end - - initializer :build_middleware_stack do |app| - app.app - end - - # Fires the user-supplied after_initialize block (config#after_initialize) - initializer :after_initialize do |app| - app.config.after_initialize_blocks.each do |block| - block.call(app) - end - end - - # Disable dependency loading during request cycle - initializer :disable_dependency_loading do |app| - if app.config.cache_classes && !app.config.dependency_loading - ActiveSupport::Dependencies.unhook! - end - end - end end end diff --git a/railties/lib/rails/application/bootstrap.rb b/railties/lib/rails/application/bootstrap.rb new file mode 100644 index 0000000000..819d00be4e --- /dev/null +++ b/railties/lib/rails/application/bootstrap.rb @@ -0,0 +1,77 @@ +module Rails + class Application + module Bootstrap + include Initializable + + initializer :load_all_active_support do |app| + require "active_support/all" unless app.config.active_support.bare + end + + # Preload all frameworks specified by the Configuration#frameworks. + # Used by Passenger to ensure everything's loaded before forking and + # to avoid autoload race conditions in JRuby. + initializer :preload_frameworks do |app| + require 'active_support/dependencies' + ActiveSupport::Autoload.eager_autoload! if app.config.preload_frameworks + end + + # Initialize the logger early in the stack in case we need to log some deprecation. + initializer :initialize_logger do |app| + Rails.logger ||= app.config.logger || begin + path = app.config.paths.log.to_a.first + logger = ActiveSupport::BufferedLogger.new(path) + logger.level = ActiveSupport::BufferedLogger.const_get(app.config.log_level.to_s.upcase) + logger.auto_flushing = false if Rails.env.production? + logger + rescue StandardError => e + logger = ActiveSupport::BufferedLogger.new(STDERR) + logger.level = ActiveSupport::BufferedLogger::WARN + logger.warn( + "Rails Error: Unable to access log file. Please ensure that #{path} exists and is chmod 0666. " + + "The log level has been raised to WARN and the output directed to STDERR until the problem is fixed." + ) + logger + end + end + + # Initialize cache early in the stack so railties can make use of it. + initializer :initialize_cache do |app| + unless defined?(RAILS_CACHE) + silence_warnings { Object.const_set "RAILS_CACHE", ActiveSupport::Cache.lookup_store(app.config.cache_store) } + + if RAILS_CACHE.respond_to?(:middleware) + app.config.middleware.insert_after(:"Rack::Lock", RAILS_CACHE.middleware) + end + end + end + + # Initialize rails subscriber on top of notifications. + initializer :initialize_subscriber do |app| + require 'active_support/notifications' + + if app.config.colorize_logging == false + Rails::Subscriber.colorize_logging = false + app.config.generators.colorize_logging = false + end + + ActiveSupport::Notifications.subscribe do |*args| + Rails::Subscriber.dispatch(args) + end + end + + initializer :set_clear_dependencies_hook do |app| + unless app.config.cache_classes + ActionDispatch::Callbacks.after do + ActiveSupport::Dependencies.clear + end + end + end + + # Sets the dependency loading mechanism. + # TODO: Remove files from the $" and always use require. + initializer :initialize_dependency_mechanism do |app| + ActiveSupport::Dependencies.mechanism = app.config.cache_classes ? :require : :load + end + end + end +end \ No newline at end of file diff --git a/railties/lib/rails/application/finisher.rb b/railties/lib/rails/application/finisher.rb new file mode 100644 index 0000000000..4e7fffd0b3 --- /dev/null +++ b/railties/lib/rails/application/finisher.rb @@ -0,0 +1,31 @@ +module Rails + class Application + module Finisher + include Initializable + + initializer :add_builtin_route do |app| + if Rails.env.development? + app.config.action_dispatch.route_files << File.join(RAILTIES_PATH, 'builtin', 'routes.rb') + end + end + + initializer :build_middleware_stack do |app| + app.app + end + + # Fires the user-supplied after_initialize block (config#after_initialize) + initializer :after_initialize do |app| + app.config.after_initialize_blocks.each do |block| + block.call(app) + end + end + + # Disable dependency loading during request cycle + initializer :disable_dependency_loading do |app| + if app.config.cache_classes && !app.config.dependency_loading + ActiveSupport::Dependencies.unhook! + end + end + end + end +end \ No newline at end of file -- cgit v1.2.3 From 13d66cdf2544af0d465d596383743b16b5005996 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 23 Jan 2010 16:59:32 +0100 Subject: Extract Railtie load from application. --- railties/lib/rails/application.rb | 59 ++++++++++++----------- railties/lib/rails/application/railties.rb | 31 ++++++++++++ railties/lib/rails/application/routes_reloader.rb | 14 ++++-- railties/lib/rails/configuration.rb | 2 + railties/lib/rails/engine.rb | 9 ++-- railties/lib/rails/railtie.rb | 14 +++--- 6 files changed, 83 insertions(+), 46 deletions(-) create mode 100644 railties/lib/rails/application/railties.rb (limited to 'railties/lib') diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index 81606d09e4..8f562350b4 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -4,6 +4,7 @@ module Rails class Application < Engine autoload :Bootstrap, 'rails/application/bootstrap' autoload :Finisher, 'rails/application/finisher' + autoload :Railties, 'rails/application/railties' autoload :RoutesReloader, 'rails/application/routes_reloader' # TODO Check helpers works as expected @@ -25,9 +26,22 @@ module Rails end def inherited(base) + # TODO Add this check + # raise "You cannot have more than one Rails::Application" if Rails.application super - Railtie.plugins.delete(base) - Rails.application = base.instance + + # TODO Add a test which ensures me + # Railtie.plugins.delete(base) + Rails.application ||= base.instance + + base.rake_tasks do + require "rails/tasks" + paths.lib.tasks.to_a.sort.each { |r| load(rake) } + task :environment do + $rails_rake_task = true + initialize! + end + end end protected @@ -38,11 +52,16 @@ module Rails end def initialize - require_environment + environment = config.paths.config.environment.to_a.first + require environment if environment end def routes - ActionController::Routing::Routes + ::ActionController::Routing::Routes + end + + def railties + @railties ||= Railties.new(config) end def routes_reloader @@ -58,34 +77,16 @@ module Rails self end - def require_environment - environment = config.paths.config.environment.to_a.first - require environment if environment - end - def load_tasks - require "rails/tasks" - plugins.each { |p| p.load_tasks } - # Load all application tasks - # TODO: extract out the path to the rake tasks - Dir["#{root}/lib/tasks/**/*.rake"].sort.each { |ext| load ext } - task :environment do - $rails_rake_task = true - initialize! - end + super + railties.all { |r| r.load_tasks } + self end def load_generators - plugins.each { |p| p.load_generators } - end - - # TODO: Fix this method. It loads all railties independent if :all is given - # or not, otherwise frameworks are never loaded. - def plugins - @plugins ||= begin - plugin_names = (config.plugins || [:all]).map { |p| p.to_sym } - Railtie.plugins.map(&:new) + Plugin.all(plugin_names, config.paths.vendor.plugins) - end + super + railties.all { |r| r.load_generators } + self end def app @@ -100,7 +101,7 @@ module Rails def initializers initializers = Bootstrap.initializers initializers += super - plugins.each { |p| initializers += p.initializers } + railties.all { |r| initializers += r.initializers } initializers += Finisher.initializers initializers end diff --git a/railties/lib/rails/application/railties.rb b/railties/lib/rails/application/railties.rb new file mode 100644 index 0000000000..d167d9bf4f --- /dev/null +++ b/railties/lib/rails/application/railties.rb @@ -0,0 +1,31 @@ +module Rails + class Application + class Railties + # TODO Write tests + def initialize(config) + @config = config + end + + def all(&block) + @all ||= railties + engines + plugins + @all.each(&block) if block + @all + end + + def railties + @railties ||= ::Rails::Railtie.subclasses.map(&:new) + end + + def engines + @engines ||= ::Rails::Engine.subclasses.map(&:new) + end + + def plugins + @plugins ||= begin + plugin_names = (@config.plugins || [:all]).map { |p| p.to_sym } + Plugin.all(plugin_names, @config.paths.vendor.plugins) + end + end + end + end +end \ No newline at end of file diff --git a/railties/lib/rails/application/routes_reloader.rb b/railties/lib/rails/application/routes_reloader.rb index 621bb1ccbd..6d61de2320 100644 --- a/railties/lib/rails/application/routes_reloader.rb +++ b/railties/lib/rails/application/routes_reloader.rb @@ -1,8 +1,8 @@ module Rails class Application class RoutesReloader - attr_reader :config - + # TODO Change config.action_dispatch.route_files to config.action_dispatch.routes_path + # TODO Write tests def initialize(config) @config, @last_change_at = config, nil end @@ -10,8 +10,8 @@ module Rails def changed_at routes_changed_at = nil - config.action_dispatch.route_files.each do |config| - config_changed_at = File.stat(config).mtime + files.each do |file| + config_changed_at = File.stat(file).mtime if routes_changed_at.nil? || config_changed_at > routes_changed_at routes_changed_at = config_changed_at @@ -26,7 +26,7 @@ module Rails routes.disable_clear_and_finalize = true routes.clear! - config.action_dispatch.route_files.each { |config| load(config) } + files.each { |file| load(file) } routes.finalize! nil @@ -41,6 +41,10 @@ module Rails reload! end end + + def files + @config.action_dispatch.route_files + end end end end \ No newline at end of file diff --git a/railties/lib/rails/configuration.rb b/railties/lib/rails/configuration.rb index 5d0c7fd4b4..b8bed01cdc 100644 --- a/railties/lib/rails/configuration.rb +++ b/railties/lib/rails/configuration.rb @@ -115,6 +115,7 @@ module Rails /^(#{bits})(?:=)?$/ end + # TODO Fix me def config_keys Railtie.plugin_names.map { |n| n.to_s }.uniq end @@ -182,6 +183,7 @@ module Rails paths = super paths.app.controllers << builtin_controller if builtin_controller paths.config.database "config/database.yml" + paths.lib.tasks "lib/tasks", :glob => "**/*.rake" paths.log "log/#{Rails.env}.log" paths.tmp "tmp" paths.tmp.cache "tmp/cache" diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index aaa669ef32..cf6ca7c3f5 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -4,6 +4,7 @@ module Rails class Engine < Railtie class << self attr_accessor :called_from + delegate :middleware, :root, :paths, :to => :config def original_root @original_root ||= find_root_with_file_flag("lib") @@ -18,7 +19,6 @@ module Rails call_stack = caller.map { |p| p.split(':').first } File.dirname(call_stack.detect { |p| p !~ %r[railties/lib/rails|rack/lib/rack] }) end - super end @@ -33,17 +33,14 @@ module Rails end root = File.exist?("#{root_path}/#{flag}") ? root_path : default - raise "Could not find root path for #{self}" unless root RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? - Pathname.new(root).expand_path : - Pathname.new(root).realpath + Pathname.new(root).expand_path : Pathname.new(root).realpath end end - delegate :config, :to => :'self.class' - delegate :middleware, :root, :to => :config + delegate :middleware, :paths, :root, :config, :to => :'self.class' # Add configured load paths to ruby load paths and remove duplicates. initializer :set_load_path do diff --git a/railties/lib/rails/railtie.rb b/railties/lib/rails/railtie.rb index 5b97fabe40..84f7a86946 100644 --- a/railties/lib/rails/railtie.rb +++ b/railties/lib/rails/railtie.rb @@ -5,26 +5,30 @@ module Rails ABSTRACT_RAILTIES = %w(Rails::Plugin Rails::Engine Rails::Application) class << self + attr_reader :subclasses + def abstract_railtie?(base) ABSTRACT_RAILTIES.include?(base.name) end def inherited(base) - @@plugins ||= [] - @@plugins << base unless abstract_railtie?(base) + @subclasses ||= [] + @subclasses << base unless abstract_railtie?(base) end - # This should be called railtie_name and engine_name + # TODO This should be called railtie_name and engine_name def plugin_name(plugin_name = nil) @plugin_name ||= name.demodulize.underscore @plugin_name = plugin_name if plugin_name @plugin_name end + # TODO Deprecate me def plugins - @@plugins + @subclasses end + # TODO Deprecate me def plugin_names plugins.map { |p| p.plugin_name } end @@ -59,12 +63,10 @@ module Rails end def load_tasks - return unless rake_tasks rake_tasks.each { |blk| blk.call } end def load_generators - return unless generators generators.each { |blk| blk.call } end end -- cgit v1.2.3 From 924fa084e81b8b2f5ae9eab93d6b711c2b6b89d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 23 Jan 2010 17:13:25 +0100 Subject: First steps into making Plugin < Engine. --- railties/lib/rails/application.rb | 5 +++++ railties/lib/rails/application/finisher.rb | 12 ++++++++++++ railties/lib/rails/engine.rb | 20 ++++++++++---------- railties/lib/rails/paths.rb | 3 ++- 4 files changed, 29 insertions(+), 11 deletions(-) (limited to 'railties/lib') diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index 8f562350b4..1286d437c7 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -51,6 +51,11 @@ module Rails end end + # Application is always reloadable when config.cache_classes is false. + def reloadable?(app) + true + end + def initialize environment = config.paths.config.environment.to_a.first require environment if environment diff --git a/railties/lib/rails/application/finisher.rb b/railties/lib/rails/application/finisher.rb index 4e7fffd0b3..2ac881ac11 100644 --- a/railties/lib/rails/application/finisher.rb +++ b/railties/lib/rails/application/finisher.rb @@ -3,6 +3,18 @@ module Rails module Finisher include Initializable + initializer :ensure_load_once_paths_as_subset do + extra = ActiveSupport::Dependencies.load_once_paths - + ActiveSupport::Dependencies.load_paths + + unless extra.empty? + abort <<-end_error + load_once_paths must be a subset of the load_paths. + Extra items in load_once_paths: #{extra * ','} + end_error + end + end + initializer :add_builtin_route do |app| if Rails.env.development? app.config.action_dispatch.route_files << File.join(RAILTIES_PATH, 'builtin', 'routes.rb') diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index cf6ca7c3f5..b11c1ac73e 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -42,6 +42,10 @@ module Rails delegate :middleware, :paths, :root, :config, :to => :'self.class' + def reloadable?(app) + app.config.reload_plugins + end + # Add configured load paths to ruby load paths and remove duplicates. initializer :set_load_path do config.load_paths.reverse_each do |path| @@ -52,21 +56,17 @@ module Rails # Set the paths from which Rails will automatically load source files, # and the load_once paths. - initializer :set_autoload_paths do + initializer :set_autoload_paths do |app| ActiveSupport::Dependencies.load_paths.concat(config.load_paths) - ActiveSupport::Dependencies.load_once_paths.concat(config.load_once_paths) - - extra = ActiveSupport::Dependencies.load_once_paths - - ActiveSupport::Dependencies.load_paths - unless extra.empty? - abort <<-end_error - load_once_paths must be a subset of the load_paths. - Extra items in load_once_paths: #{extra * ','} - end_error + if reloadable?(app) + ActiveSupport::Dependencies.load_once_paths.concat(config.load_once_paths) + else + ActiveSupport::Dependencies.load_once_paths.concat(config.load_paths) end # Freeze so future modifications will fail rather than do nothing mysteriously + config.load_paths.freeze config.load_once_paths.freeze end diff --git a/railties/lib/rails/paths.rb b/railties/lib/rails/paths.rb index d81af3c709..069371aa9c 100644 --- a/railties/lib/rails/paths.rb +++ b/railties/lib/rails/paths.rb @@ -73,7 +73,7 @@ module Rails @load_once = @options[:load_once] @eager_load = @options[:eager_load] - @load_path = @options[:load_path] || @eager_load + @load_path = @options[:load_path] || @eager_load || @load_once @root.all_paths << self end @@ -98,6 +98,7 @@ module Rails def load_once! @load_once = true + @load_path = true end def load_once? -- cgit v1.2.3 From 2b75b94ac0c329de12a0a94ba77f8aad5574514f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 23 Jan 2010 17:51:48 +0100 Subject: Plugin is now an Engine. --- railties/lib/rails.rb | 2 +- railties/lib/rails/application.rb | 1 - railties/lib/rails/configuration.rb | 15 ++++--- railties/lib/rails/engine.rb | 11 +++++- railties/lib/rails/plugin.rb | 78 +++++++++++++------------------------ 5 files changed, 46 insertions(+), 61 deletions(-) (limited to 'railties/lib') diff --git a/railties/lib/rails.rb b/railties/lib/rails.rb index 29afb672b6..667587cdce 100644 --- a/railties/lib/rails.rb +++ b/railties/lib/rails.rb @@ -6,8 +6,8 @@ require 'active_support/core_ext/logger' require 'rails/initializable' require 'rails/railtie' -require 'rails/plugin' require 'rails/engine' +require 'rails/plugin' require 'rails/application' require 'rails/railties_path' require 'rails/version' diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index 1286d437c7..6d1702bf93 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -36,7 +36,6 @@ module Rails base.rake_tasks do require "rails/tasks" - paths.lib.tasks.to_a.sort.each { |r| load(rake) } task :environment do $rails_rake_task = true initialize! diff --git a/railties/lib/rails/configuration.rb b/railties/lib/rails/configuration.rb index b8bed01cdc..900173abcc 100644 --- a/railties/lib/rails/configuration.rb +++ b/railties/lib/rails/configuration.rb @@ -125,19 +125,19 @@ module Rails attr_reader :root attr_writer :eager_load_paths, :load_once_paths, :load_paths - def initialize(root) + def initialize(root=nil) @root = root - super() end def paths @paths ||= begin paths = Rails::Application::Root.new(@root) - paths.app "app", :eager_load => true, :glob => "*" - paths.app.controllers "app/controllers", :eager_load => true - paths.app.metals "app/metal", :eager_load => true + paths.app "app", :eager_load => true, :glob => "*" + paths.app.controllers "app/controllers", :eager_load => true + paths.app.metals "app/metal", :eager_load => true paths.app.views "app/views" - paths.lib "lib", :load_path => true + paths.lib "lib", :load_path => true + paths.lib.tasks "lib/tasks", :glob => "**/*.rake" paths.config "config" paths.config.environment "config/environments", :glob => "#{Rails.env}.rb" paths.config.initializers "config/initializers", :glob => "**/*.rb" @@ -183,7 +183,6 @@ module Rails paths = super paths.app.controllers << builtin_controller if builtin_controller paths.config.database "config/database.yml" - paths.lib.tasks "lib/tasks", :glob => "**/*.rake" paths.log "log/#{Rails.env}.log" paths.tmp "tmp" paths.tmp.cache "tmp/cache" @@ -215,7 +214,7 @@ module Rails self.preload_frameworks = true self.cache_classes = true self.dependency_loading = false - action_controller.allow_concurrency = true if respond_to?(:action_controller) + self.action_controller.allow_concurrency = true if respond_to?(:action_controller) self end diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index b11c1ac73e..39afac0604 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -40,12 +40,21 @@ module Rails end end - delegate :middleware, :paths, :root, :config, :to => :'self.class' + delegate :middleware, :paths, :root, :to => :config + + def config + self.class.config + end def reloadable?(app) app.config.reload_plugins end + def load_tasks + super + config.paths.lib.tasks.to_a.sort.each { |ext| load(ext) } + end + # Add configured load paths to ruby load paths and remove duplicates. initializer :set_load_path do config.load_paths.reverse_each do |path| diff --git a/railties/lib/rails/plugin.rb b/railties/lib/rails/plugin.rb index 43632127fc..b4fd503631 100644 --- a/railties/lib/rails/plugin.rb +++ b/railties/lib/rails/plugin.rb @@ -1,67 +1,45 @@ module Rails - 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 + class Plugin < Engine + class << self + def inherited(base) + raise "You should not inherit from Rails::Plugin" end - plugins.sort_by do |p| - [list.index(p.name) || list.index(:all), p.name.to_s] + def config + raise "Plugins does not provide configuration at the class level" 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,lib/tasks,rails/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" - ActiveSupport::Dependencies.load_paths << path + def 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 - unless app.config.reload_plugins - ActiveSupport::Dependencies.load_once_paths << path + plugins.sort_by do |p| + [list.index(p.name) || list.index(:all), p.name.to_s] 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) + attr_reader :name, :path + + def initialize(root) + @name = File.basename(root).to_sym + config.root = root end - initializer :add_view_paths, :after => :initialize_framework_views do - plugin_views = "#{path}/app/views" - if File.directory?(plugin_views) - ActionController::Base.view_paths.concat([plugin_views]) if defined? ActionController - ActionMailer::Base.view_paths.concat([plugin_views]) if defined? ActionMailer - end + def config + @config ||= Engine::Configuration.new end - initializer :add_routing_file, :after => :initialize_routing do |app| - routing_file = "#{path}/config/routes.rb" - if File.exist?(routing_file) - app.config.action_dispatch.route_files.unshift(routing_file) - end + initializer :load_init_rb do |app| + file = Dir["#{root}/{rails/init,init}.rb"].first + config = app.config + eval(File.read(file), binding, file) if file && File.file?(file) end end end -- cgit v1.2.3 From 788fce2550ed587d86d239d8fcd488f919d15b5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 23 Jan 2010 18:41:53 +0100 Subject: Create configurable modules and ensure that they are added only on direct children. --- railties/lib/rails/application.rb | 58 +++++++++++++++--------------- railties/lib/rails/engine.rb | 38 ++++++++------------ railties/lib/rails/engine/configurable.rb | 24 +++++++++++++ railties/lib/rails/plugin.rb | 32 +++++++---------- railties/lib/rails/railtie.rb | 34 ++++++++++++------ railties/lib/rails/railtie/configurable.rb | 23 ++++++++++++ 6 files changed, 128 insertions(+), 81 deletions(-) create mode 100644 railties/lib/rails/engine/configurable.rb create mode 100644 railties/lib/rails/railtie/configurable.rb (limited to 'railties/lib') diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index 6d1702bf93..676e395d39 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -9,6 +9,8 @@ module Rails # TODO Check helpers works as expected # TODO Check routes namespaces + # TODO raise "You cannot have more than one Rails::Application" if Rails.application + # TODO Ensure production settings are read properly class << self private :new alias :configure :class_eval @@ -17,30 +19,10 @@ module Rails @instance ||= new end - def config - @config ||= Configuration.new(self.original_root) - end - - def original_root - @original_root ||= find_root_with_file_flag("config.ru", Dir.pwd) - end - def inherited(base) - # TODO Add this check - # raise "You cannot have more than one Rails::Application" if Rails.application super - - # TODO Add a test which ensures me - # Railtie.plugins.delete(base) - Rails.application ||= base.instance - - base.rake_tasks do - require "rails/tasks" - task :environment do - $rails_rake_task = true - initialize! - end - end + Rails.application = base.instance + base.require_environment! end protected @@ -50,16 +32,15 @@ module Rails end end - # Application is always reloadable when config.cache_classes is false. - def reloadable?(app) - true - end - - def initialize + def require_environment! environment = config.paths.config.environment.to_a.first require environment if environment end + def config + @config ||= ::Rails::Configuration.new(self.class.find_root_with_flag("config.ru", Dir.pwd)) + end + def routes ::ActionController::Routing::Routes end @@ -82,12 +63,14 @@ module Rails end def load_tasks + initialize_tasks super railties.all { |r| r.load_tasks } self end def load_generators + initialize_generators super railties.all { |r| r.load_generators } self @@ -109,5 +92,24 @@ module Rails initializers += Finisher.initializers initializers end + + protected + + def initialize_tasks + require "rails/tasks" + task :environment do + $rails_rake_task = true + initialize! + end + end + + def initialize_generators + require "rails/generators" + end + + # Application is always reloadable when config.cache_classes is false. + def reloadable?(app) + true + end end end diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index 39afac0604..b373b931f9 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -2,29 +2,23 @@ require 'active_support/core_ext/module/delegation' module Rails class Engine < Railtie + autoload :Configurable, "rails/engine/configurable" + class << self attr_accessor :called_from - delegate :middleware, :root, :paths, :to => :config - - def original_root - @original_root ||= find_root_with_file_flag("lib") - end - - def config - @config ||= Configuration.new(original_root) - end def inherited(base) - base.called_from = begin - call_stack = caller.map { |p| p.split(':').first } - File.dirname(call_stack.detect { |p| p !~ %r[railties/lib/rails|rack/lib/rack] }) + unless abstract_railtie?(base) + base.called_from = begin + call_stack = caller.map { |p| p.split(':').first } + File.dirname(call_stack.detect { |p| p !~ %r[railties/lib/rails|rack/lib/rack] }) + end end + super end - protected - - def find_root_with_file_flag(flag, default=nil) + def find_root_with_flag(flag, default=nil) root_path = self.called_from while root_path && File.directory?(root_path) && !File.exist?("#{root_path}/#{flag}") @@ -42,14 +36,6 @@ module Rails delegate :middleware, :paths, :root, :to => :config - def config - self.class.config - end - - def reloadable?(app) - app.config.reload_plugins - end - def load_tasks super config.paths.lib.tasks.to_a.sort.each { |ext| load(ext) } @@ -113,5 +99,11 @@ module Rails end end end + + protected + + def reloadable?(app) + app.config.reload_plugins + end end end \ No newline at end of file diff --git a/railties/lib/rails/engine/configurable.rb b/railties/lib/rails/engine/configurable.rb new file mode 100644 index 0000000000..fb420e8a12 --- /dev/null +++ b/railties/lib/rails/engine/configurable.rb @@ -0,0 +1,24 @@ +module Rails + class Engine + module Configurable + def self.included(base) + base.extend ClassMethods + base.delegate :middleware, :root, :paths, :to => :config + end + + module ClassMethods + def config + @config ||= Configuration.new(find_root_with_flag("lib")) + end + + def inherited(base) + raise "You cannot inherit from a Rails::Engine child" + end + end + + def config + self.class.config + end + end + end +end \ No newline at end of file diff --git a/railties/lib/rails/plugin.rb b/railties/lib/rails/plugin.rb index b4fd503631..cb3fdc8501 100644 --- a/railties/lib/rails/plugin.rb +++ b/railties/lib/rails/plugin.rb @@ -1,27 +1,21 @@ module Rails class Plugin < Engine - class << self - def inherited(base) - raise "You should not inherit from Rails::Plugin" - end - - def config - raise "Plugins does not provide configuration at the class level" - end + def self.inherited(base) + raise "You cannot inherit from Rails::Plugin" + end - def 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 + 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 + plugins.sort_by do |p| + [list.index(p.name) || list.index(:all), p.name.to_s] end end diff --git a/railties/lib/rails/railtie.rb b/railties/lib/rails/railtie.rb index 84f7a86946..208b017348 100644 --- a/railties/lib/rails/railtie.rb +++ b/railties/lib/rails/railtie.rb @@ -1,19 +1,21 @@ module Rails class Railtie + autoload :Configurable, "rails/railtie/configurable" + include Initializable ABSTRACT_RAILTIES = %w(Rails::Plugin Rails::Engine Rails::Application) class << self - attr_reader :subclasses - - def abstract_railtie?(base) - ABSTRACT_RAILTIES.include?(base.name) + def subclasses + @subclasses ||= [] end def inherited(base) - @subclasses ||= [] - @subclasses << base unless abstract_railtie?(base) + unless abstract_railtie?(base) + base.send(:include, self::Configurable) if add_configurable?(base) + subclasses << base + end end # TODO This should be called railtie_name and engine_name @@ -25,7 +27,7 @@ module Rails # TODO Deprecate me def plugins - @subclasses + subclasses end # TODO Deprecate me @@ -33,10 +35,6 @@ module Rails plugins.map { |p| p.plugin_name } end - def config - Configuration.default - end - def subscriber(subscriber) Rails::Subscriber.add(plugin_name, subscriber) end @@ -52,6 +50,20 @@ module Rails @generators << blk if blk @generators end + + protected + + def abstract_railtie?(base) + ABSTRACT_RAILTIES.include?(base.name) + end + + # Just add configurable behavior if a Configurable module is defined + # and the class is a direct child from self. This is required to avoid + # application or plugins getting class configuration method from Railties + # and/or Engines. + def add_configurable?(base) + defined?(self::Configurable) && base.ancestors[1] == self + end end def rake_tasks diff --git a/railties/lib/rails/railtie/configurable.rb b/railties/lib/rails/railtie/configurable.rb new file mode 100644 index 0000000000..3850efa4a7 --- /dev/null +++ b/railties/lib/rails/railtie/configurable.rb @@ -0,0 +1,23 @@ +module Rails + class Railtie + module Configurable + def self.included(base) + base.extend ClassMethods + end + + module ClassMethods + def config + @config ||= Configuration.new + end + + def inherited(base) + raise "You cannot inherit from a Rails::Railtie child" + end + end + + def config + self.class.config + end + end + end +end -- cgit v1.2.3 From b17e358e3df34c03019e357f693611618092e1d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 23 Jan 2010 22:30:17 +0100 Subject: Move configuration to subfolders. --- railties/lib/rails.rb | 8 - railties/lib/rails/application.rb | 7 +- railties/lib/rails/application/configuration.rb | 85 +++++++ railties/lib/rails/configuration.rb | 292 +++++++----------------- railties/lib/rails/engine.rb | 4 +- railties/lib/rails/engine/configurable.rb | 2 +- railties/lib/rails/engine/configuration.rb | 48 ++++ railties/lib/rails/paths.rb | 2 +- railties/lib/rails/plugin.rb | 4 + railties/lib/rails/railtie.rb | 6 +- railties/lib/rails/railtie/configurable.rb | 2 +- railties/lib/rails/railtie/configuration.rb | 9 + 12 files changed, 243 insertions(+), 226 deletions(-) create mode 100644 railties/lib/rails/application/configuration.rb create mode 100644 railties/lib/rails/engine/configuration.rb create mode 100644 railties/lib/rails/railtie/configuration.rb (limited to 'railties/lib') diff --git a/railties/lib/rails.rb b/railties/lib/rails.rb index 667587cdce..623555e7c1 100644 --- a/railties/lib/rails.rb +++ b/railties/lib/rails.rb @@ -4,16 +4,8 @@ require 'active_support' require 'active_support/core_ext/kernel/reporting' require 'active_support/core_ext/logger' -require 'rails/initializable' -require 'rails/railtie' -require 'rails/engine' -require 'rails/plugin' require 'rails/application' -require 'rails/railties_path' require 'rails/version' -require 'rails/rack' -require 'rails/paths' -require 'rails/configuration' require 'rails/deprecation' require 'rails/subscriber' require 'rails/ruby_version_check' diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index 676e395d39..504a241da8 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -1,8 +1,13 @@ require 'fileutils' +require 'rails/railties_path' +require 'rails/railtie' +require 'rails/engine' +require 'rails/plugin' module Rails class Application < Engine autoload :Bootstrap, 'rails/application/bootstrap' + autoload :Configuration, 'rails/application/configuration' autoload :Finisher, 'rails/application/finisher' autoload :Railties, 'rails/application/railties' autoload :RoutesReloader, 'rails/application/routes_reloader' @@ -38,7 +43,7 @@ module Rails end def config - @config ||= ::Rails::Configuration.new(self.class.find_root_with_flag("config.ru", Dir.pwd)) + @config ||= Application::Configuration.new(self.class.find_root_with_flag("config.ru", Dir.pwd)) end def routes diff --git a/railties/lib/rails/application/configuration.rb b/railties/lib/rails/application/configuration.rb new file mode 100644 index 0000000000..aaf18b5f51 --- /dev/null +++ b/railties/lib/rails/application/configuration.rb @@ -0,0 +1,85 @@ +require 'rails/engine/configuration' + +module Rails + class Application + class Configuration < ::Rails::Engine::Configuration + include ::Rails::Configuration::Deprecated + + attr_accessor :cache_classes, :cache_store, :colorize_logging, + :consider_all_requests_local, :dependency_loading, + :filter_parameters, :log_level, :logger, :metals, + :plugins, :preload_frameworks, :reload_plugins, + :serve_static_assets, :time_zone, :whiny_nils + + def initialize(*) + super + @filter_parameters = [] + @dependency_loading = true + @serve_static_assets = true + end + + def paths + @paths ||= begin + paths = super + paths.app.controllers << builtin_controller if builtin_controller + paths.config.database "config/database.yml" + paths.log "log/#{Rails.env}.log" + paths.tmp "tmp" + paths.tmp.cache "tmp/cache" + paths.vendor "vendor", :load_path => true + paths.vendor.plugins "vendor/plugins" + + if File.exists?("#{root}/test/mocks/#{Rails.env}") + ActiveSupport::Deprecation.warn "\"RAILS_ROOT/test/mocks/#{Rails.env}\" won't be added " << + "automatically to load paths anymore in future releases" + paths.mocks_path "test/mocks", :load_path => true, :glob => Rails.env + end + + paths + end + end + + # Enable threaded mode. Allows concurrent requests to controller actions and + # multiple database connections. Also disables automatic dependency loading + # after boot, and disables reloading code on every request, as these are + # fundamentally incompatible with thread safety. + def threadsafe! + self.preload_frameworks = true + self.cache_classes = true + self.dependency_loading = false + self.action_controller.allow_concurrency = true if respond_to?(:action_controller) + self + end + + # Loads and returns the contents of the #database_configuration_file. The + # contents of the file are processed via ERB before being sent through + # YAML::load. + def database_configuration + require 'erb' + YAML::load(ERB.new(IO.read(paths.config.database.to_a.first)).result) + end + + def cache_store + @cache_store ||= begin + if File.exist?("#{root}/tmp/cache/") + [ :file_store, "#{root}/tmp/cache/" ] + else + :memory_store + end + end + end + + def builtin_controller + File.join(RAILTIES_PATH, "builtin", "rails_info") if Rails.env.development? + end + + def log_level + @log_level ||= Rails.env.production? ? :info : :debug + end + + def time_zone + @time_zone ||= "UTC" + end + end + end +end \ No newline at end of file diff --git a/railties/lib/rails/configuration.rb b/railties/lib/rails/configuration.rb index 900173abcc..e0bd12497f 100644 --- a/railties/lib/rails/configuration.rb +++ b/railties/lib/rails/configuration.rb @@ -1,9 +1,10 @@ require 'active_support/ordered_options' +require 'rails/paths' +require 'rails/rack' module Rails - module Shared - # Those configuration values are shared between railtie, engines and so forth. - module Configuration + module Configuration + module Shared def middleware @@default_middleware_stack ||= ActionDispatch::MiddlewareStack.new.tap do |middleware| middleware.use('::ActionDispatch::Static', lambda { Rails.public_path }, :if => lambda { Rails.application.config.serve_static_assets }) @@ -35,7 +36,7 @@ module Rails # config.generators.colorize_logging = false # def generators - @@generators ||= GeneratorsConfiguration.new + @@generators ||= Rails::Configuration::Generators.new if block_given? yield @@generators else @@ -51,14 +52,34 @@ module Rails after_initialize_blocks << blk if blk end - protected + def respond_to?(name) + super || name.to_s =~ config_key_regexp + end + + private + + def method_missing(name, *args, &blk) + if name.to_s =~ config_key_regexp + return $2 == '=' ? options[$1] = args.first : options[$1] + end + super + end + + def config_key_regexp + bits = config_keys.map { |n| Regexp.escape(n.to_s) }.join('|') + /^(#{bits})(?:=)?$/ + end + + def config_keys + (Railtie.plugin_names + Engine.plugin_names).map { |n| n.to_s }.uniq + end def options @@options ||= Hash.new { |h,k| h[k] = ActiveSupport::OrderedOptions.new } end end - class GeneratorsConfiguration #:nodoc: + class Generators #:nodoc: attr_accessor :aliases, :options, :colorize_logging def initialize @@ -86,227 +107,74 @@ module Rails end end end - end - # Holds Railtie basic configuration. It does not include configuration values - # related with load paths and the application specifics. - class Railtie::Configuration - include Shared::Configuration - - def self.default - @default ||= new - end - - def respond_to?(name) - super || name.to_s =~ config_key_regexp - end - - private - - def method_missing(name, *args, &blk) - if name.to_s =~ config_key_regexp - return $2 == '=' ? options[$1] = args.first : options[$1] + module Deprecated + def frameworks(*args) + raise "config.frameworks in no longer supported. See the generated " \ + "config/boot.rb for steps on how to limit the frameworks that " \ + "will be loaded" end - super - end - - def config_key_regexp - bits = config_keys.map { |n| Regexp.escape(n.to_s) }.join('|') - /^(#{bits})(?:=)?$/ - end + alias :frameworks= :frameworks - # TODO Fix me - def config_keys - Railtie.plugin_names.map { |n| n.to_s }.uniq - end - end - - class Engine::Configuration < Railtie::Configuration - attr_reader :root - attr_writer :eager_load_paths, :load_once_paths, :load_paths - - def initialize(root=nil) - @root = root - end - - def paths - @paths ||= begin - paths = Rails::Application::Root.new(@root) - paths.app "app", :eager_load => true, :glob => "*" - paths.app.controllers "app/controllers", :eager_load => true - paths.app.metals "app/metal", :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.environment "config/environments", :glob => "#{Rails.env}.rb" - paths.config.initializers "config/initializers", :glob => "**/*.rb" - paths.config.locales "config/locales", :glob => "*.{rb,yml}" - paths.config.routes "config/routes.rb" - paths + def view_path=(value) + ActiveSupport::Deprecation.warn "config.view_path= is deprecated, " << + "please do config.paths.app.views= instead", caller + paths.app.views = value end - end - - def root=(value) - @root = paths.path = Pathname.new(value).expand_path - end - - def eager_load_paths - @eager_load_paths ||= paths.eager_load - end - - def load_once_paths - @eager_load_paths ||= paths.load_once - end - - def load_paths - @load_paths ||= paths.load_paths - end - end - - class Configuration < Engine::Configuration - attr_accessor :cache_classes, :cache_store, :colorize_logging, - :consider_all_requests_local, :dependency_loading, - :filter_parameters, :log_level, :logger, :metals, - :plugins, :preload_frameworks, :reload_plugins, - :serve_static_assets, :time_zone, :whiny_nils - - def initialize(*) - super - @filter_parameters = [] - @dependency_loading = true - @serve_static_assets = true - end - def paths - @paths ||= begin - paths = super - paths.app.controllers << builtin_controller if builtin_controller - paths.config.database "config/database.yml" - paths.log "log/#{Rails.env}.log" - paths.tmp "tmp" - paths.tmp.cache "tmp/cache" - paths.vendor "vendor", :load_path => true - paths.vendor.plugins "vendor/plugins" - - if File.exists?("#{root}/test/mocks/#{Rails.env}") - ActiveSupport::Deprecation.warn "\"RAILS_ROOT/test/mocks/#{Rails.env}\" won't be added " << - "automatically to load paths anymore in future releases" - paths.mocks_path "test/mocks", :load_path => true, :glob => Rails.env - end - - paths + def view_path + ActiveSupport::Deprecation.warn "config.view_path is deprecated, " << + "please do config.paths.app.views instead", caller + paths.app.views.to_a.first end - end - - def frameworks(*args) - raise "config.frameworks in no longer supported. See the generated " \ - "config/boot.rb for steps on how to limit the frameworks that " \ - "will be loaded" - end - alias frameworks= frameworks - - # Enable threaded mode. Allows concurrent requests to controller actions and - # multiple database connections. Also disables automatic dependency loading - # after boot, and disables reloading code on every request, as these are - # fundamentally incompatible with thread safety. - def threadsafe! - self.preload_frameworks = true - self.cache_classes = true - self.dependency_loading = false - self.action_controller.allow_concurrency = true if respond_to?(:action_controller) - self - end - - # Loads and returns the contents of the #database_configuration_file. The - # contents of the file are processed via ERB before being sent through - # YAML::load. - def database_configuration - require 'erb' - YAML::load(ERB.new(IO.read(paths.config.database.to_a.first)).result) - end - def cache_store - @cache_store ||= begin - if File.exist?("#{root}/tmp/cache/") - [ :file_store, "#{root}/tmp/cache/" ] - else - :memory_store - end + def routes_configuration_file=(value) + ActiveSupport::Deprecation.warn "config.routes_configuration_file= is deprecated, " << + "please do config.paths.config.routes= instead", caller + paths.config.routes = value end - end - - def builtin_controller - File.join(RAILTIES_PATH, "builtin", "rails_info") if Rails.env.development? - end - - def log_level - @log_level ||= Rails.env.production? ? :info : :debug - end - - def time_zone - @time_zone ||= "UTC" - end - # Deprecated paths - def view_path=(value) - ActiveSupport::Deprecation.warn "config.view_path= is deprecated, " << - "please do config.paths.app.views= instead", caller - paths.app.views = value - end - - def view_path - ActiveSupport::Deprecation.warn "config.view_path is deprecated, " << - "please do config.paths.app.views instead", caller - paths.app.views.to_a.first - end - - def routes_configuration_file=(value) - ActiveSupport::Deprecation.warn "config.routes_configuration_file= is deprecated, " << - "please do config.paths.config.routes= instead", caller - paths.config.routes = value - end - - def routes_configuration_file - ActiveSupport::Deprecation.warn "config.routes_configuration_file is deprecated, " << - "please do config.paths.config.routes instead", caller - paths.config.routes.to_a.first - end + def routes_configuration_file + ActiveSupport::Deprecation.warn "config.routes_configuration_file is deprecated, " << + "please do config.paths.config.routes instead", caller + paths.config.routes.to_a.first + end - def database_configuration_file=(value) - ActiveSupport::Deprecation.warn "config.database_configuration_file= is deprecated, " << - "please do config.paths.config.database= instead", caller - paths.config.database = value - end + def database_configuration_file=(value) + ActiveSupport::Deprecation.warn "config.database_configuration_file= is deprecated, " << + "please do config.paths.config.database= instead", caller + paths.config.database = value + end - def database_configuration_file - ActiveSupport::Deprecation.warn "config.database_configuration_file is deprecated, " << - "please do config.paths.config.database instead", caller - paths.config.database.to_a.first - end + def database_configuration_file + ActiveSupport::Deprecation.warn "config.database_configuration_file is deprecated, " << + "please do config.paths.config.database instead", caller + paths.config.database.to_a.first + end - def log_path=(value) - ActiveSupport::Deprecation.warn "config.log_path= is deprecated, " << - "please do config.paths.log= instead", caller - paths.config.log = value - end + def log_path=(value) + ActiveSupport::Deprecation.warn "config.log_path= is deprecated, " << + "please do config.paths.log= instead", caller + paths.config.log = value + end - def log_path - ActiveSupport::Deprecation.warn "config.log_path is deprecated, " << - "please do config.paths.log instead", caller - paths.config.log.to_a.first - end + def log_path + ActiveSupport::Deprecation.warn "config.log_path is deprecated, " << + "please do config.paths.log instead", caller + paths.config.log.to_a.first + end - def controller_paths=(value) - ActiveSupport::Deprecation.warn "config.controller_paths= is deprecated, " << - "please do config.paths.app.controllers= instead", caller - paths.app.controllers = value - end + def controller_paths=(value) + ActiveSupport::Deprecation.warn "config.controller_paths= is deprecated, " << + "please do config.paths.app.controllers= instead", caller + paths.app.controllers = value + end - def controller_paths - ActiveSupport::Deprecation.warn "config.controller_paths is deprecated, " << - "please do config.paths.app.controllers instead", caller - paths.app.controllers.to_a.uniq + def controller_paths + ActiveSupport::Deprecation.warn "config.controller_paths is deprecated, " << + "please do config.paths.app.controllers instead", caller + paths.app.controllers.to_a.uniq + end end end end diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index b373b931f9..d972050dc9 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -1,8 +1,10 @@ require 'active_support/core_ext/module/delegation' +require 'rails/railtie' module Rails class Engine < Railtie - autoload :Configurable, "rails/engine/configurable" + autoload :Configurable, "rails/engine/configurable" + autoload :Configuration, "rails/engine/configuration" class << self attr_accessor :called_from diff --git a/railties/lib/rails/engine/configurable.rb b/railties/lib/rails/engine/configurable.rb index fb420e8a12..d4b7ecc532 100644 --- a/railties/lib/rails/engine/configurable.rb +++ b/railties/lib/rails/engine/configurable.rb @@ -8,7 +8,7 @@ module Rails module ClassMethods def config - @config ||= Configuration.new(find_root_with_flag("lib")) + @config ||= Engine::Configuration.new(find_root_with_flag("lib")) end def inherited(base) diff --git a/railties/lib/rails/engine/configuration.rb b/railties/lib/rails/engine/configuration.rb new file mode 100644 index 0000000000..e2fdf125d3 --- /dev/null +++ b/railties/lib/rails/engine/configuration.rb @@ -0,0 +1,48 @@ +require 'rails/railtie/configuration' + +module Rails + class Engine + class Configuration < ::Rails::Railtie::Configuration + attr_reader :root + attr_writer :eager_load_paths, :load_once_paths, :load_paths + + def initialize(root=nil) + @root = root + 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.metals "app/metal", :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.environment "config/environments", :glob => "#{Rails.env}.rb" + paths.config.initializers "config/initializers", :glob => "**/*.rb" + paths.config.locales "config/locales", :glob => "*.{rb,yml}" + paths.config.routes "config/routes.rb" + paths + end + end + + def root=(value) + @root = paths.path = Pathname.new(value).expand_path + end + + def eager_load_paths + @eager_load_paths ||= paths.eager_load + end + + def load_once_paths + @eager_load_paths ||= paths.load_once + end + + def load_paths + @load_paths ||= paths.load_paths + end + end + end +end \ No newline at end of file diff --git a/railties/lib/rails/paths.rb b/railties/lib/rails/paths.rb index 069371aa9c..88c0c1c68c 100644 --- a/railties/lib/rails/paths.rb +++ b/railties/lib/rails/paths.rb @@ -1,7 +1,7 @@ require 'set' module Rails - class Application + module Paths module PathParent def method_missing(id, *args) name = id.to_s diff --git a/railties/lib/rails/plugin.rb b/railties/lib/rails/plugin.rb index cb3fdc8501..394e634903 100644 --- a/railties/lib/rails/plugin.rb +++ b/railties/lib/rails/plugin.rb @@ -1,9 +1,13 @@ +require 'rails/engine' + module Rails class Plugin < Engine def self.inherited(base) raise "You cannot inherit from Rails::Plugin" end + # TODO Right now, if a plugin has an Engine or a Railtie inside it, + # the initializers for this plugin will be executed twice. def self.all(list, paths) plugins = [] paths.each do |path| diff --git a/railties/lib/rails/railtie.rb b/railties/lib/rails/railtie.rb index 208b017348..6424be5a55 100644 --- a/railties/lib/rails/railtie.rb +++ b/railties/lib/rails/railtie.rb @@ -1,6 +1,10 @@ +require 'rails/initializable' +require 'rails/configuration' + module Rails class Railtie - autoload :Configurable, "rails/railtie/configurable" + autoload :Configurable, "rails/railtie/configurable" + autoload :Configuration, "rails/railtie/configuration" include Initializable diff --git a/railties/lib/rails/railtie/configurable.rb b/railties/lib/rails/railtie/configurable.rb index 3850efa4a7..a2eb938c5a 100644 --- a/railties/lib/rails/railtie/configurable.rb +++ b/railties/lib/rails/railtie/configurable.rb @@ -7,7 +7,7 @@ module Rails module ClassMethods def config - @config ||= Configuration.new + @config ||= Railtie::Configuration.new end def inherited(base) diff --git a/railties/lib/rails/railtie/configuration.rb b/railties/lib/rails/railtie/configuration.rb new file mode 100644 index 0000000000..bfb43f7041 --- /dev/null +++ b/railties/lib/rails/railtie/configuration.rb @@ -0,0 +1,9 @@ +require 'rails/configuration' + +module Rails + class Railtie + class Configuration + include Rails::Configuration::Shared + end + end +end \ No newline at end of file -- cgit v1.2.3 From d3c40242a58a8863cd216f6639f93df5fdb0c075 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 23 Jan 2010 23:02:43 +0100 Subject: Move console stuff to its own directory. --- railties/lib/rails/commands/console.rb | 8 ++++---- railties/lib/rails/console/app.rb | 31 ++++++++++++++++++++++++++++++ railties/lib/rails/console/helpers.rb | 5 +++++ railties/lib/rails/console/sandbox.rb | 6 ++++++ railties/lib/rails/console_app.rb | 31 ------------------------------ railties/lib/rails/console_sandbox.rb | 6 ------ railties/lib/rails/console_with_helpers.rb | 5 ----- 7 files changed, 46 insertions(+), 46 deletions(-) create mode 100644 railties/lib/rails/console/app.rb create mode 100644 railties/lib/rails/console/helpers.rb create mode 100644 railties/lib/rails/console/sandbox.rb delete mode 100644 railties/lib/rails/console_app.rb delete mode 100644 railties/lib/rails/console_sandbox.rb delete mode 100644 railties/lib/rails/console_with_helpers.rb (limited to 'railties/lib') diff --git a/railties/lib/rails/commands/console.rb b/railties/lib/rails/commands/console.rb index 27ac7fd20a..a984eff6e2 100644 --- a/railties/lib/rails/commands/console.rb +++ b/railties/lib/rails/commands/console.rb @@ -1,6 +1,6 @@ require 'optparse' require 'irb' -require "irb/completion" +require 'irb/completion' module Rails class Console @@ -24,9 +24,9 @@ module Rails end @app.initialize! - require "rails/console_app" - require "rails/console_sandbox" if options[:sandbox] - require "rails/console_with_helpers" + require "rails/console/app" + require "rails/console/sandbox" if options[:sandbox] + require "rails/console/helpers" if options[:debugger] begin diff --git a/railties/lib/rails/console/app.rb b/railties/lib/rails/console/app.rb new file mode 100644 index 0000000000..98f7f774a9 --- /dev/null +++ b/railties/lib/rails/console/app.rb @@ -0,0 +1,31 @@ +require 'active_support/all' +require 'active_support/test_case' +require 'action_controller' + +# work around the at_exit hook in test/unit, which kills IRB +Test::Unit.run = true if Test::Unit.respond_to?(:run=) + +# reference the global "app" instance, created on demand. To recreate the +# instance, pass a non-false value as the parameter. +def app(create=false) + @app_integration_instance = nil if create + @app_integration_instance ||= new_session do |sess| + sess.host! "www.example.com" + end +end + +# create a new session. If a block is given, the new session will be yielded +# to the block before being returned. +def new_session + app = ActionController::Dispatcher.new + session = ActionController::Integration::Session.new(app) + yield session if block_given? + session +end + +#reloads the environment +def reload! + puts "Reloading..." + ActionDispatch::Callbacks.new(lambda {}, true).call({}) + true +end diff --git a/railties/lib/rails/console/helpers.rb b/railties/lib/rails/console/helpers.rb new file mode 100644 index 0000000000..039db667c4 --- /dev/null +++ b/railties/lib/rails/console/helpers.rb @@ -0,0 +1,5 @@ +def helper + @helper ||= ApplicationController.helpers +end + +@controller = ApplicationController.new diff --git a/railties/lib/rails/console/sandbox.rb b/railties/lib/rails/console/sandbox.rb new file mode 100644 index 0000000000..65a3d68619 --- /dev/null +++ b/railties/lib/rails/console/sandbox.rb @@ -0,0 +1,6 @@ +ActiveRecord::Base.connection.increment_open_transactions +ActiveRecord::Base.connection.begin_db_transaction +at_exit do + ActiveRecord::Base.connection.rollback_db_transaction + ActiveRecord::Base.connection.decrement_open_transactions +end diff --git a/railties/lib/rails/console_app.rb b/railties/lib/rails/console_app.rb deleted file mode 100644 index 98f7f774a9..0000000000 --- a/railties/lib/rails/console_app.rb +++ /dev/null @@ -1,31 +0,0 @@ -require 'active_support/all' -require 'active_support/test_case' -require 'action_controller' - -# work around the at_exit hook in test/unit, which kills IRB -Test::Unit.run = true if Test::Unit.respond_to?(:run=) - -# reference the global "app" instance, created on demand. To recreate the -# instance, pass a non-false value as the parameter. -def app(create=false) - @app_integration_instance = nil if create - @app_integration_instance ||= new_session do |sess| - sess.host! "www.example.com" - end -end - -# create a new session. If a block is given, the new session will be yielded -# to the block before being returned. -def new_session - app = ActionController::Dispatcher.new - session = ActionController::Integration::Session.new(app) - yield session if block_given? - session -end - -#reloads the environment -def reload! - puts "Reloading..." - ActionDispatch::Callbacks.new(lambda {}, true).call({}) - true -end diff --git a/railties/lib/rails/console_sandbox.rb b/railties/lib/rails/console_sandbox.rb deleted file mode 100644 index 65a3d68619..0000000000 --- a/railties/lib/rails/console_sandbox.rb +++ /dev/null @@ -1,6 +0,0 @@ -ActiveRecord::Base.connection.increment_open_transactions -ActiveRecord::Base.connection.begin_db_transaction -at_exit do - ActiveRecord::Base.connection.rollback_db_transaction - ActiveRecord::Base.connection.decrement_open_transactions -end diff --git a/railties/lib/rails/console_with_helpers.rb b/railties/lib/rails/console_with_helpers.rb deleted file mode 100644 index 039db667c4..0000000000 --- a/railties/lib/rails/console_with_helpers.rb +++ /dev/null @@ -1,5 +0,0 @@ -def helper - @helper ||= ApplicationController.helpers -end - -@controller = ApplicationController.new -- cgit v1.2.3 From 2fde9d774b322fc708990675671231c64c691a33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sun, 24 Jan 2010 09:00:18 +0100 Subject: Solve some pendencies. --- railties/lib/rails/application.rb | 5 ++--- railties/lib/rails/application/finisher.rb | 2 +- railties/lib/rails/application/railties.rb | 2 +- railties/lib/rails/application/routes_reloader.rb | 13 ++++++------- railties/lib/rails/engine.rb | 4 ++-- railties/lib/rails/paths.rb | 23 ++++++++++++++--------- railties/lib/rails/plugin.rb | 9 +++++++-- 7 files changed, 33 insertions(+), 25 deletions(-) (limited to 'railties/lib') diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index 504a241da8..a8ff125342 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -1,8 +1,7 @@ require 'fileutils' require 'rails/railties_path' -require 'rails/railtie' -require 'rails/engine' require 'rails/plugin' +require 'rails/engine' module Rails class Application < Engine @@ -14,7 +13,6 @@ module Rails # TODO Check helpers works as expected # TODO Check routes namespaces - # TODO raise "You cannot have more than one Rails::Application" if Rails.application # TODO Ensure production settings are read properly class << self private :new @@ -25,6 +23,7 @@ module Rails end def inherited(base) + raise "You cannot have more than one Rails::Application" if Rails.application super Rails.application = base.instance base.require_environment! diff --git a/railties/lib/rails/application/finisher.rb b/railties/lib/rails/application/finisher.rb index 2ac881ac11..db19011b7f 100644 --- a/railties/lib/rails/application/finisher.rb +++ b/railties/lib/rails/application/finisher.rb @@ -17,7 +17,7 @@ module Rails initializer :add_builtin_route do |app| if Rails.env.development? - app.config.action_dispatch.route_files << File.join(RAILTIES_PATH, 'builtin', 'routes.rb') + app.config.action_dispatch.route_paths << File.join(RAILTIES_PATH, 'builtin', 'routes.rb') end end diff --git a/railties/lib/rails/application/railties.rb b/railties/lib/rails/application/railties.rb index d167d9bf4f..b3e6693f89 100644 --- a/railties/lib/rails/application/railties.rb +++ b/railties/lib/rails/application/railties.rb @@ -1,7 +1,7 @@ module Rails class Application class Railties - # TODO Write tests + # TODO Write tests for this behavior extracted from Application def initialize(config) @config = config end diff --git a/railties/lib/rails/application/routes_reloader.rb b/railties/lib/rails/application/routes_reloader.rb index 6d61de2320..d861d27465 100644 --- a/railties/lib/rails/application/routes_reloader.rb +++ b/railties/lib/rails/application/routes_reloader.rb @@ -1,8 +1,7 @@ module Rails class Application class RoutesReloader - # TODO Change config.action_dispatch.route_files to config.action_dispatch.routes_path - # TODO Write tests + # TODO Write tests for this behavior extracted from Application def initialize(config) @config, @last_change_at = config, nil end @@ -10,8 +9,8 @@ module Rails def changed_at routes_changed_at = nil - files.each do |file| - config_changed_at = File.stat(file).mtime + paths.each do |path| + config_changed_at = File.stat(path).mtime if routes_changed_at.nil? || config_changed_at > routes_changed_at routes_changed_at = config_changed_at @@ -26,7 +25,7 @@ module Rails routes.disable_clear_and_finalize = true routes.clear! - files.each { |file| load(file) } + paths.each { |path| load(path) } routes.finalize! nil @@ -42,8 +41,8 @@ module Rails end end - def files - @config.action_dispatch.route_files + def paths + @config.action_dispatch.route_paths end end end diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index d972050dc9..cc878ac8f2 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -67,9 +67,9 @@ module Rails config.load_once_paths.freeze end - initializer :add_routing_files do + initializer :add_routing_paths do config.paths.config.routes.to_a.each do |route| - config.action_dispatch.route_files.unshift(route) if File.exists?(route) + config.action_dispatch.route_paths.unshift(route) if File.exists?(route) end end diff --git a/railties/lib/rails/paths.rb b/railties/lib/rails/paths.rb index 88c0c1c68c..55874813da 100644 --- a/railties/lib/rails/paths.rb +++ b/railties/lib/rails/paths.rb @@ -31,22 +31,21 @@ module Rails @all_paths = [] end - def load_once - all_paths.map { |path| path.paths if path.load_once? }.compact.flatten.uniq + def all_paths + @all_paths.uniq! + @all_paths end - def eager_load - all_paths.map { |path| path.paths if path.eager_load? }.compact.flatten.uniq + def load_once + filter { |path| path.paths if path.load_once? } end - # TODO Discover why do we need to call uniq! here - def all_paths - @all_paths.uniq! - @all_paths + def eager_load + filter { |path| path.paths if path.eager_load? } end def load_paths - all_paths.map { |path| path.paths if path.load_path? }.compact.flatten.uniq + filter { |path| path.paths if path.load_path? } end def push(*) @@ -56,6 +55,12 @@ module Rails alias unshift push alias << push alias concat push + + protected + + def filter(&block) + all_paths.map(&block).compact.flatten.uniq.select { |p| File.exists?(p) } + end end class Path diff --git a/railties/lib/rails/plugin.rb b/railties/lib/rails/plugin.rb index 394e634903..62dc7f30f8 100644 --- a/railties/lib/rails/plugin.rb +++ b/railties/lib/rails/plugin.rb @@ -6,8 +6,6 @@ module Rails raise "You cannot inherit from Rails::Plugin" end - # TODO Right now, if a plugin has an Engine or a Railtie inside it, - # the initializers for this plugin will be executed twice. def self.all(list, paths) plugins = [] paths.each do |path| @@ -39,5 +37,12 @@ module Rails config = app.config eval(File.read(file), binding, file) if file && File.file?(file) end + + # TODO Write tests for this sanity check + initializer :sanity_check_railties_collision do + if Engine.subclasses.map { |k| k.root.to_s }.include?(root.to_s) + raise "The plugin #{name.inspect} is a Railtie or an Engine and cannot be installed as Plugin" + end + end end end -- cgit v1.2.3 From 5b26fa48755c461619f7d48f4cd28faa9db442f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sun, 24 Jan 2010 09:42:28 +0100 Subject: Make plugin generator compatible with new schema. --- railties/lib/generators/rails/plugin/plugin_generator.rb | 2 +- .../rails/plugin/templates/lib/tasks/%file_name%_tasks.rake.tt | 4 ++++ .../generators/rails/plugin/templates/tasks/%file_name%_tasks.rake.tt | 4 ---- 3 files changed, 5 insertions(+), 5 deletions(-) create mode 100644 railties/lib/generators/rails/plugin/templates/lib/tasks/%file_name%_tasks.rake.tt delete mode 100644 railties/lib/generators/rails/plugin/templates/tasks/%file_name%_tasks.rake.tt (limited to 'railties/lib') diff --git a/railties/lib/generators/rails/plugin/plugin_generator.rb b/railties/lib/generators/rails/plugin/plugin_generator.rb index b68b8691db..8f01dcd589 100644 --- a/railties/lib/generators/rails/plugin/plugin_generator.rb +++ b/railties/lib/generators/rails/plugin/plugin_generator.rb @@ -17,7 +17,7 @@ module Rails def create_tasks_files return unless options[:tasks] - directory 'tasks', plugin_dir('tasks') + directory 'lib/tasks', plugin_dir('lib/tasks') end hook_for :generator do |generator| diff --git a/railties/lib/generators/rails/plugin/templates/lib/tasks/%file_name%_tasks.rake.tt b/railties/lib/generators/rails/plugin/templates/lib/tasks/%file_name%_tasks.rake.tt new file mode 100644 index 0000000000..72920a9d3a --- /dev/null +++ b/railties/lib/generators/rails/plugin/templates/lib/tasks/%file_name%_tasks.rake.tt @@ -0,0 +1,4 @@ +# desc "Explaining what the task does" +# task :<%= file_name %> do +# # Task goes here +# end diff --git a/railties/lib/generators/rails/plugin/templates/tasks/%file_name%_tasks.rake.tt b/railties/lib/generators/rails/plugin/templates/tasks/%file_name%_tasks.rake.tt deleted file mode 100644 index 72920a9d3a..0000000000 --- a/railties/lib/generators/rails/plugin/templates/tasks/%file_name%_tasks.rake.tt +++ /dev/null @@ -1,4 +0,0 @@ -# desc "Explaining what the task does" -# task :<%= file_name %> do -# # Task goes here -# end -- cgit v1.2.3 From 25724c664d8af6c50903709da69a5871475383fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sun, 24 Jan 2010 09:47:55 +0100 Subject: Load deprecated tasks for plugins. --- railties/lib/rails/plugin.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'railties/lib') diff --git a/railties/lib/rails/plugin.rb b/railties/lib/rails/plugin.rb index 62dc7f30f8..b47679d140 100644 --- a/railties/lib/rails/plugin.rb +++ b/railties/lib/rails/plugin.rb @@ -23,6 +23,17 @@ module Rails attr_reader :name, :path + def load_tasks + super + extra_tasks = Dir["#{root}/{tasks,rails/tasks}/**/*.rake"] + + unless extra_tasks.empty? + ActiveSupport::Deprecation.warn "Having rake tasks in PLUGIN_PATH/tasks or " << + "PLUGIN_PATH/rails/tasks is deprecated. Use to PLUGIN_PATH/lib/tasks instead" + extra_tasks.sort.each { |ext| load(ext) } + end + end + def initialize(root) @name = File.basename(root).to_sym config.root = root -- cgit v1.2.3 From e0bdc4f446686a498c3117e27ed8561f5c6d34f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sun, 24 Jan 2010 11:06:06 +0100 Subject: Ensure namespaced controllers in engines work. --- railties/lib/rails/engine.rb | 10 ++++++++++ railties/lib/rails/tasks/routes.rake | 1 + 2 files changed, 11 insertions(+) (limited to 'railties/lib') diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index cc878ac8f2..6f724963b2 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -73,6 +73,16 @@ module Rails end end + initializer :add_routing_namespaces do |app| + config.paths.app.controllers.to_a.each do |load_path| + load_path = File.expand_path(load_path) + Dir["#{load_path}/*/*_controller.rb"].collect do |path| + namespace = File.dirname(path).sub(/#{load_path}\/?/, '') + app.routes.controller_namespaces << namespace unless namespace.empty? + end + end + end + initializer :add_locales do config.i18n.load_path.unshift(*config.paths.config.locales.to_a) end diff --git a/railties/lib/rails/tasks/routes.rake b/railties/lib/rails/tasks/routes.rake index 2395d73b2f..e8da72db4d 100644 --- a/railties/lib/rails/tasks/routes.rake +++ b/railties/lib/rails/tasks/routes.rake @@ -1,5 +1,6 @@ desc 'Print out all defined routes in match order, with names. Target specific controller with CONTROLLER=x.' task :routes => :environment do + Rails::Application.reload_routes! all_routes = ENV['CONTROLLER'] ? ActionController::Routing::Routes.routes.select { |route| route.defaults[:controller] == ENV['CONTROLLER'] } : ActionController::Routing::Routes.routes routes = all_routes.collect do |route| name = ActionController::Routing::Routes.named_routes.routes.index(route).to_s -- cgit v1.2.3 From 37e4deb2606557e5340b48169ffc1435bb331439 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sun, 24 Jan 2010 12:04:37 +0100 Subject: Ensure helpers work from configured path. --- railties/lib/rails/application.rb | 2 -- railties/lib/rails/engine/configuration.rb | 1 + 2 files changed, 1 insertion(+), 2 deletions(-) (limited to 'railties/lib') diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index a8ff125342..90118c8cfc 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -11,8 +11,6 @@ module Rails autoload :Railties, 'rails/application/railties' autoload :RoutesReloader, 'rails/application/routes_reloader' - # TODO Check helpers works as expected - # TODO Check routes namespaces # TODO Ensure production settings are read properly class << self private :new diff --git a/railties/lib/rails/engine/configuration.rb b/railties/lib/rails/engine/configuration.rb index e2fdf125d3..93afdcf911 100644 --- a/railties/lib/rails/engine/configuration.rb +++ b/railties/lib/rails/engine/configuration.rb @@ -15,6 +15,7 @@ module Rails 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.metals "app/metal", :eager_load => true paths.app.views "app/views" paths.lib "lib", :load_path => true -- cgit v1.2.3 From b92608770e20618ab6a6c67099dd19ae4533689e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sun, 24 Jan 2010 12:15:46 +0100 Subject: Ensure environment config has higher priority than application ones. --- railties/lib/rails/application.rb | 2 -- railties/lib/rails/application/bootstrap.rb | 4 ++++ 2 files changed, 4 insertions(+), 2 deletions(-) (limited to 'railties/lib') diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index 90118c8cfc..e14719c758 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -11,7 +11,6 @@ module Rails autoload :Railties, 'rails/application/railties' autoload :RoutesReloader, 'rails/application/routes_reloader' - # TODO Ensure production settings are read properly class << self private :new alias :configure :class_eval @@ -24,7 +23,6 @@ module Rails raise "You cannot have more than one Rails::Application" if Rails.application super Rails.application = base.instance - base.require_environment! end protected diff --git a/railties/lib/rails/application/bootstrap.rb b/railties/lib/rails/application/bootstrap.rb index 819d00be4e..3c339ffc57 100644 --- a/railties/lib/rails/application/bootstrap.rb +++ b/railties/lib/rails/application/bootstrap.rb @@ -3,6 +3,10 @@ module Rails module Bootstrap include Initializable + initializer :load_environment_config do |app| + app.require_environment! + end + initializer :load_all_active_support do |app| require "active_support/all" unless app.config.active_support.bare end -- cgit v1.2.3 From e548f96b1d5cb6529dd6fbc6544f03a3a840b48c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sun, 24 Jan 2010 12:23:21 +0100 Subject: Rename plugin_name to railtie_name and engine_name. --- railties/lib/rails/configuration.rb | 2 +- railties/lib/rails/engine.rb | 3 +++ railties/lib/rails/railtie.rb | 21 +++++++-------------- 3 files changed, 11 insertions(+), 15 deletions(-) (limited to 'railties/lib') diff --git a/railties/lib/rails/configuration.rb b/railties/lib/rails/configuration.rb index e0bd12497f..c29cd0ef2c 100644 --- a/railties/lib/rails/configuration.rb +++ b/railties/lib/rails/configuration.rb @@ -71,7 +71,7 @@ module Rails end def config_keys - (Railtie.plugin_names + Engine.plugin_names).map { |n| n.to_s }.uniq + (Railtie.railtie_names + Engine.engine_names).map { |n| n.to_s }.uniq end def options diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index 6f724963b2..842785875a 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -9,6 +9,9 @@ module Rails class << self attr_accessor :called_from + alias :engine_name :railtie_name + alias :engine_names :railtie_names + def inherited(base) unless abstract_railtie?(base) base.called_from = begin diff --git a/railties/lib/rails/railtie.rb b/railties/lib/rails/railtie.rb index 6424be5a55..3cf358d75f 100644 --- a/railties/lib/rails/railtie.rb +++ b/railties/lib/rails/railtie.rb @@ -22,25 +22,18 @@ module Rails end end - # TODO This should be called railtie_name and engine_name - def plugin_name(plugin_name = nil) - @plugin_name ||= name.demodulize.underscore - @plugin_name = plugin_name if plugin_name - @plugin_name + def railtie_name(railtie_name = nil) + @railtie_name ||= name.demodulize.underscore + @railtie_name = railtie_name if railtie_name + @railtie_name end - # TODO Deprecate me - def plugins - subclasses - end - - # TODO Deprecate me - def plugin_names - plugins.map { |p| p.plugin_name } + def railtie_names + subclasses.map { |p| p.railtie_name } end def subscriber(subscriber) - Rails::Subscriber.add(plugin_name, subscriber) + Rails::Subscriber.add(railtie_name, subscriber) end def rake_tasks(&blk) -- cgit v1.2.3 From dd05b6c543f48050f494214da7803da6f5655292 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sun, 24 Jan 2010 12:49:12 +0100 Subject: Add tests for plugin sanity check. --- railties/lib/rails/engine/configurable.rb | 3 ++- railties/lib/rails/plugin.rb | 3 +-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'railties/lib') diff --git a/railties/lib/rails/engine/configurable.rb b/railties/lib/rails/engine/configurable.rb index d4b7ecc532..9a370f0abb 100644 --- a/railties/lib/rails/engine/configurable.rb +++ b/railties/lib/rails/engine/configurable.rb @@ -3,10 +3,11 @@ module Rails module Configurable def self.included(base) base.extend ClassMethods - base.delegate :middleware, :root, :paths, :to => :config end module ClassMethods + delegate :middleware, :root, :paths, :to => :config + def config @config ||= Engine::Configuration.new(find_root_with_flag("lib")) end diff --git a/railties/lib/rails/plugin.rb b/railties/lib/rails/plugin.rb index b47679d140..4c73809177 100644 --- a/railties/lib/rails/plugin.rb +++ b/railties/lib/rails/plugin.rb @@ -49,10 +49,9 @@ module Rails eval(File.read(file), binding, file) if file && File.file?(file) end - # TODO Write tests for this sanity check initializer :sanity_check_railties_collision do if Engine.subclasses.map { |k| k.root.to_s }.include?(root.to_s) - raise "The plugin #{name.inspect} is a Railtie or an Engine and cannot be installed as Plugin" + raise "\"#{name}\" is a Railtie/Engine and cannot be installed as plugin" end end end -- cgit v1.2.3 From 84ebfa4550b2325c6c89bc13aa6f904ff88d0db7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sun, 24 Jan 2010 14:48:00 +0100 Subject: Ensure metals and initializers in plugins are loaded. --- .../lib/generators/rails/metal/templates/metal.rb | 2 +- railties/lib/rails/application/finisher.rb | 2 +- railties/lib/rails/application/routes_reloader.rb | 14 +++++------ railties/lib/rails/configuration.rb | 2 +- railties/lib/rails/engine.rb | 18 ++++++++------ railties/lib/rails/engine/configuration.rb | 2 +- railties/lib/rails/rack/metal.rb | 29 ++++++++++++++++------ 7 files changed, 44 insertions(+), 25 deletions(-) (limited to 'railties/lib') diff --git a/railties/lib/generators/rails/metal/templates/metal.rb b/railties/lib/generators/rails/metal/templates/metal.rb index 2f5d4e7593..8cc3f1f258 100644 --- a/railties/lib/generators/rails/metal/templates/metal.rb +++ b/railties/lib/generators/rails/metal/templates/metal.rb @@ -1,5 +1,5 @@ # Allow the metal piece to run in isolation -require File.expand_path('../../../config/environment', __FILE__) +require File.expand_path('../../../config/environment', __FILE__) unless defined?(Rails) class <%= class_name %> def self.call(env) diff --git a/railties/lib/rails/application/finisher.rb b/railties/lib/rails/application/finisher.rb index db19011b7f..6461b76d3d 100644 --- a/railties/lib/rails/application/finisher.rb +++ b/railties/lib/rails/application/finisher.rb @@ -17,7 +17,7 @@ module Rails initializer :add_builtin_route do |app| if Rails.env.development? - app.config.action_dispatch.route_paths << File.join(RAILTIES_PATH, 'builtin', 'routes.rb') + RoutesReloader.paths << File.join(RAILTIES_PATH, 'builtin', 'routes.rb') end end diff --git a/railties/lib/rails/application/routes_reloader.rb b/railties/lib/rails/application/routes_reloader.rb index d861d27465..fe0cfb7801 100644 --- a/railties/lib/rails/application/routes_reloader.rb +++ b/railties/lib/rails/application/routes_reloader.rb @@ -1,7 +1,11 @@ module Rails class Application + # TODO Write tests for this behavior extracted from Application class RoutesReloader - # TODO Write tests for this behavior extracted from Application + def self.paths + @paths ||= [] + end + def initialize(config) @config, @last_change_at = config, nil end @@ -9,7 +13,7 @@ module Rails def changed_at routes_changed_at = nil - paths.each do |path| + self.class.paths.each do |path| config_changed_at = File.stat(path).mtime if routes_changed_at.nil? || config_changed_at > routes_changed_at @@ -25,7 +29,7 @@ module Rails routes.disable_clear_and_finalize = true routes.clear! - paths.each { |path| load(path) } + self.class.paths.each { |path| load(path) } routes.finalize! nil @@ -40,10 +44,6 @@ module Rails reload! end end - - def paths - @config.action_dispatch.route_paths - end end end end \ No newline at end of file diff --git a/railties/lib/rails/configuration.rb b/railties/lib/rails/configuration.rb index c29cd0ef2c..3c5c1c1e16 100644 --- a/railties/lib/rails/configuration.rb +++ b/railties/lib/rails/configuration.rb @@ -16,7 +16,7 @@ module Rails middleware.use('::ActionDispatch::Cookies') middleware.use(lambda { ActionController::Base.session_store }, lambda { ActionController::Base.session_options }) middleware.use('::ActionDispatch::Flash', :if => lambda { ActionController::Base.session_store }) - middleware.use(lambda { Rails::Rack::Metal.new(Rails.application.config.paths.app.metals.to_a, Rails.application.config.metals) }) + middleware.use(lambda { Rails::Rack::Metal.new(Rails.application.config.metals) }) middleware.use('ActionDispatch::ParamsParser') middleware.use('::Rack::MethodOverride') middleware.use('::ActionDispatch::Head') diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index 842785875a..e40052e0f1 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -71,13 +71,13 @@ module Rails end initializer :add_routing_paths do - config.paths.config.routes.to_a.each do |route| - config.action_dispatch.route_paths.unshift(route) if File.exists?(route) + paths.config.routes.to_a.each do |route| + Rails::Application::RoutesReloader.paths.unshift(route) if File.exists?(route) end end initializer :add_routing_namespaces do |app| - config.paths.app.controllers.to_a.each do |load_path| + paths.app.controllers.to_a.each do |load_path| load_path = File.expand_path(load_path) Dir["#{load_path}/*/*_controller.rb"].collect do |path| namespace = File.dirname(path).sub(/#{load_path}\/?/, '') @@ -87,17 +87,21 @@ module Rails end initializer :add_locales do - config.i18n.load_path.unshift(*config.paths.config.locales.to_a) + config.i18n.load_path.unshift(*paths.config.locales.to_a) end initializer :add_view_paths do - views = config.paths.app.views.to_a + views = paths.app.views.to_a ActionController::Base.view_paths.concat(views) if defined?(ActionController) ActionMailer::Base.view_paths.concat(views) if defined?(ActionMailer) end + initializer :add_metals do + Rails::Rack::Metal.paths.concat(paths.app.metals.to_a) + end + initializer :load_application_initializers do - config.paths.config.initializers.each do |initializer| + paths.config.initializers.to_a.sort.each do |initializer| load(initializer) end end @@ -107,7 +111,7 @@ module Rails if app.config.cache_classes config.eager_load_paths.each do |load_path| - matcher = /\A#{Regexp.escape(load_path)}(.*)\.rb\Z/ + matcher = /\A#{Regexp.escape(load_path)}\/(.*)\.rb\Z/ Dir.glob("#{load_path}/**/*.rb").sort.each do |file| require_dependency file.sub(matcher, '\1') end diff --git a/railties/lib/rails/engine/configuration.rb b/railties/lib/rails/engine/configuration.rb index 93afdcf911..a328e14170 100644 --- a/railties/lib/rails/engine/configuration.rb +++ b/railties/lib/rails/engine/configuration.rb @@ -16,7 +16,7 @@ module Rails 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.metals "app/metal", :eager_load => true + paths.app.metals "app/metal" paths.app.views "app/views" paths.lib "lib", :load_path => true paths.lib.tasks "lib/tasks", :glob => "**/*.rake" diff --git a/railties/lib/rails/rack/metal.rb b/railties/lib/rails/rack/metal.rb index 565f95d7c4..732936da32 100644 --- a/railties/lib/rails/rack/metal.rb +++ b/railties/lib/rails/rack/metal.rb @@ -3,14 +3,29 @@ require 'action_dispatch' module Rails module Rack class Metal - def initialize(metal_roots, metals=nil) - load_list = metals || Dir["{#{metal_roots.join(",")}}/**/*.rb"] + def self.paths + @paths ||= [] + end + + def initialize(list=nil) + metals = [] + list = Array(list || :all).map(&:to_sym) + + self.class.paths.each do |path| + matcher = /\A#{Regexp.escape(path)}\/(.*)\.rb\Z/ + Dir.glob("#{path}/**/*.rb").sort.each do |metal_path| + metal = metal_path.sub(matcher, '\1').to_sym + next unless list.include?(metal) || list.include?(:all) + require_dependency metal + metals << metal + end + end + + metals = metals.sort_by do |m| + [list.index(m) || list.index(:all), m.to_s] + end - @metals = load_list.map { |metal| - metal = File.basename(metal, '.rb') - require_dependency metal - metal.camelize.constantize - }.compact + @metals = metals.map { |m| m.to_s.camelize.constantize } end def new(app) -- cgit v1.2.3 From 92126521551a7c62e9032eda4a2b8a6d92c0279f Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sun, 24 Jan 2010 10:29:38 -0600 Subject: Add Rails::Application pointer to the default app to add symmetry to Foo::Application --- railties/lib/rails/application.rb | 6 +++++- railties/lib/rails/tasks/middleware.rake | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) (limited to 'railties/lib') diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index e14719c758..ab66d1e90b 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -16,7 +16,11 @@ module Rails alias :configure :class_eval def instance - @instance ||= new + if instance_of?(Rails::Application) + Rails.application.instance + else + @instance ||= new + end end def inherited(base) diff --git a/railties/lib/rails/tasks/middleware.rake b/railties/lib/rails/tasks/middleware.rake index 5a5bd7a7e9..c3aaddb153 100644 --- a/railties/lib/rails/tasks/middleware.rake +++ b/railties/lib/rails/tasks/middleware.rake @@ -3,5 +3,5 @@ task :middleware => :environment do Rails.configuration.middleware.active.each do |middleware| puts "use #{middleware.inspect}" end - puts "run #{Rails.application.class.name}" + puts "run #{Rails::Application.class.name}" end -- cgit v1.2.3 From 9543298d02fbd28c5edaadeed0d14f1ba19263dc Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sun, 24 Jan 2010 10:35:45 -0600 Subject: Use Rails::Application ref in Rakefile and console scripts. Less places you need to change if you rename your application. --- railties/lib/generators/rails/app/templates/Rakefile | 2 +- railties/lib/generators/rails/app/templates/script/console.tt | 2 +- railties/lib/generators/rails/app/templates/script/dbconsole.tt | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) (limited to 'railties/lib') diff --git a/railties/lib/generators/rails/app/templates/Rakefile b/railties/lib/generators/rails/app/templates/Rakefile index c19ad0e945..9cb2046439 100755 --- a/railties/lib/generators/rails/app/templates/Rakefile +++ b/railties/lib/generators/rails/app/templates/Rakefile @@ -7,4 +7,4 @@ require 'rake' require 'rake/testtask' require 'rake/rdoctask' -<%= app_const %>.load_tasks +Rails::Application.load_tasks diff --git a/railties/lib/generators/rails/app/templates/script/console.tt b/railties/lib/generators/rails/app/templates/script/console.tt index 915c5b8294..47aa254f9f 100755 --- a/railties/lib/generators/rails/app/templates/script/console.tt +++ b/railties/lib/generators/rails/app/templates/script/console.tt @@ -2,4 +2,4 @@ require File.expand_path('../../config/boot', __FILE__) require 'rails/commands/console' require File.expand_path('../../config/application', __FILE__) -Rails::Console.start(<%= app_const %>) +Rails::Console.start(Rails::Application) diff --git a/railties/lib/generators/rails/app/templates/script/dbconsole.tt b/railties/lib/generators/rails/app/templates/script/dbconsole.tt index a92f6f2844..1e53c1d761 100755 --- a/railties/lib/generators/rails/app/templates/script/dbconsole.tt +++ b/railties/lib/generators/rails/app/templates/script/dbconsole.tt @@ -2,4 +2,4 @@ require File.expand_path('../../config/boot', __FILE__) require 'rails/commands/dbconsole' require File.expand_path('../../config/application', __FILE__) -Rails::DBConsole.start(<%= app_const %>) +Rails::DBConsole.start(Rails::Application) -- cgit v1.2.3 From 6e57b88c60fa3ad1ecfa740b8cd3173c4fe72680 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sun, 24 Jan 2010 21:21:37 +0100 Subject: Fix a couple failures on 1.9.1. --- railties/lib/rails/application/finisher.rb | 2 +- railties/lib/rails/console/app.rb | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) (limited to 'railties/lib') diff --git a/railties/lib/rails/application/finisher.rb b/railties/lib/rails/application/finisher.rb index 6461b76d3d..5cc5b4ae88 100644 --- a/railties/lib/rails/application/finisher.rb +++ b/railties/lib/rails/application/finisher.rb @@ -17,7 +17,7 @@ module Rails initializer :add_builtin_route do |app| if Rails.env.development? - RoutesReloader.paths << File.join(RAILTIES_PATH, 'builtin', 'routes.rb') + Rails::Application::RoutesReloader.paths << File.join(RAILTIES_PATH, 'builtin', 'routes.rb') end end diff --git a/railties/lib/rails/console/app.rb b/railties/lib/rails/console/app.rb index 98f7f774a9..7e8fd027e6 100644 --- a/railties/lib/rails/console/app.rb +++ b/railties/lib/rails/console/app.rb @@ -23,9 +23,11 @@ def new_session session end -#reloads the environment -def reload! - puts "Reloading..." - ActionDispatch::Callbacks.new(lambda {}, true).call({}) +# reloads the environment +def reload!(print=true) + puts "Reloading..." if print + ActionDispatch::Callbacks.new(lambda {}, false) true end + +reload!(false) -- cgit v1.2.3 From 396003fc48d7c0ba206ad059646c7414bee22a36 Mon Sep 17 00:00:00 2001 From: Rizwan Reza Date: Sun, 24 Jan 2010 12:29:36 +0330 Subject: Revamp of Rails documentation task Signed-off-by: Yehuda Katz --- railties/lib/rails/tasks/documentation.rake | 54 +++++++++++++++++------------ 1 file changed, 31 insertions(+), 23 deletions(-) (limited to 'railties/lib') diff --git a/railties/lib/rails/tasks/documentation.rake b/railties/lib/rails/tasks/documentation.rake index 65d0d476ba..3f49a9a720 100644 --- a/railties/lib/rails/tasks/documentation.rake +++ b/railties/lib/rails/tasks/documentation.rake @@ -11,35 +11,43 @@ namespace :doc do rdoc.rdoc_files.include('lib/**/*.rb') } - desc "Generate documentation for the Rails framework" + desc 'Generate documentation for the Rails framework. Specify path with PATH="/path/to/rails"' Rake::RDocTask.new("rails") { |rdoc| + path = ENV['RAILS_PATH'] || 'vendor/gems/gems' + version = '-3.0.pre' unless ENV['RAILS_PATH'] rdoc.rdoc_dir = 'doc/api' rdoc.template = "#{ENV['template']}.rb" if ENV['template'] rdoc.title = "Rails Framework Documentation" rdoc.options << '--line-numbers' << '--inline-source' rdoc.rdoc_files.include('README') - rdoc.rdoc_files.include('vendor/rails/railties/CHANGELOG') - rdoc.rdoc_files.include('vendor/rails/railties/MIT-LICENSE') - rdoc.rdoc_files.include('vendor/rails/railties/README') - rdoc.rdoc_files.include('vendor/rails/railties/lib/{*.rb,commands/*.rb,generators/*.rb}') - rdoc.rdoc_files.include('vendor/rails/activerecord/README') - rdoc.rdoc_files.include('vendor/rails/activerecord/CHANGELOG') - rdoc.rdoc_files.include('vendor/rails/activerecord/lib/active_record/**/*.rb') - rdoc.rdoc_files.exclude('vendor/rails/activerecord/lib/active_record/vendor/*') - rdoc.rdoc_files.include('vendor/rails/activeresource/README') - rdoc.rdoc_files.include('vendor/rails/activeresource/CHANGELOG') - rdoc.rdoc_files.include('vendor/rails/activeresource/lib/active_resource.rb') - rdoc.rdoc_files.include('vendor/rails/activeresource/lib/active_resource/*') - rdoc.rdoc_files.include('vendor/rails/actionpack/README') - rdoc.rdoc_files.include('vendor/rails/actionpack/CHANGELOG') - rdoc.rdoc_files.include('vendor/rails/actionpack/lib/action_controller/**/*.rb') - rdoc.rdoc_files.include('vendor/rails/actionpack/lib/action_view/**/*.rb') - rdoc.rdoc_files.include('vendor/rails/actionmailer/README') - rdoc.rdoc_files.include('vendor/rails/actionmailer/CHANGELOG') - rdoc.rdoc_files.include('vendor/rails/actionmailer/lib/action_mailer/base.rb') - rdoc.rdoc_files.include('vendor/rails/activesupport/README') - rdoc.rdoc_files.include('vendor/rails/activesupport/CHANGELOG') - rdoc.rdoc_files.include('vendor/rails/activesupport/lib/active_support/**/*.rb') + + %w(README CHANGELOG lib/action_mailer/base.rb).each do |file| + rdoc.rdoc_files.include("#{path}/actionmailer#{version}/#{file}") + end + + %w(README CHANGELOG lib/action_controller/**/*.rb lib/action_view/**/*.rb).each do |file| + rdoc.rdoc_files.include("#{path}/actionpack#{version}/#{file}") + end + + %w(README CHANGELOG lib/active_model/**/*.rb).each do |file| + rdoc.rdoc_files.include("#{path}/activemodel#{version}/#{file}") + end + + %w(README CHANGELOG lib/active_record/**/*.rb).each do |file| + rdoc.rdoc_files.include("#{path}/activerecord#{version}/#{file}") + end + + %w(README CHANGELOG lib/active_resource.rb lib/active_resource/*).each do |file| + rdoc.rdoc_files.include("#{path}/activeresource#{version}/#{file}") + end + + %w(README CHANGELOG lib/active_support/**/*.rb).each do |file| + rdoc.rdoc_files.include("#{path}/activesupport#{version}/#{file}") + end + + %w(README CHANGELOG MIT-LICENSE lib/{*.rb,commands/*.rb,generators/*.rb}).each do |file| + rdoc.rdoc_files.include("#{path}/railties#{version}/#{file}") + end } plugins = FileList['vendor/plugins/**'].collect { |plugin| File.basename(plugin) } -- cgit v1.2.3 From 3b6f659fb6b1ffd323c0bbad36630cc97b96bd5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Mon, 25 Jan 2010 01:06:12 +0100 Subject: Add active_model/railtie back to generated boot.rb, add models back to paths, load active_support/railtie since we need it and ensure default logger is set before config. --- railties/lib/generators/rails/app/templates/config/boot.rb | 2 ++ railties/lib/rails.rb | 1 + railties/lib/rails/engine/configuration.rb | 1 + 3 files changed, 4 insertions(+) (limited to 'railties/lib') diff --git a/railties/lib/generators/rails/app/templates/config/boot.rb b/railties/lib/generators/rails/app/templates/config/boot.rb index cbfa5ca3e9..e91304451b 100644 --- a/railties/lib/generators/rails/app/templates/config/boot.rb +++ b/railties/lib/generators/rails/app/templates/config/boot.rb @@ -20,6 +20,7 @@ require 'rails/all' # and list the framework railties that you want: # # require "active_support/railtie" +# require "active_model/railtie" # require "active_record/railtie" # require "action_controller/railtie" # require "action_view/railtie" @@ -27,6 +28,7 @@ require 'rails/all' # require "active_resource/railtie" <% else -%> # Pick the frameworks you want: +# require "active_model/railtie" # require "active_record/railtie" require "active_support/railtie" require "action_controller/railtie" diff --git a/railties/lib/rails.rb b/railties/lib/rails.rb index 623555e7c1..b7a39fd5a7 100644 --- a/railties/lib/rails.rb +++ b/railties/lib/rails.rb @@ -10,6 +10,7 @@ require 'rails/deprecation' require 'rails/subscriber' require 'rails/ruby_version_check' +require 'active_support/railtie' require 'action_dispatch/railtie' # For Ruby 1.8, this initialization sets $KCODE to 'u' to enable the diff --git a/railties/lib/rails/engine/configuration.rb b/railties/lib/rails/engine/configuration.rb index a328e14170..c4e34b11b8 100644 --- a/railties/lib/rails/engine/configuration.rb +++ b/railties/lib/rails/engine/configuration.rb @@ -16,6 +16,7 @@ module Rails 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.metals "app/metal" paths.app.views "app/views" paths.lib "lib", :load_path => true -- cgit v1.2.3 From 9cb3ca1d29eb770c1a7adac3798666847fceee2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?= Date: Mon, 25 Jan 2010 13:13:29 +0100 Subject: Change mailer generator templates and refactor the whole naming schema. --- .../lib/generators/erb/mailer/templates/view.erb | 2 +- .../generators/rails/mailer/templates/mailer.rb | 19 +++--- .../rails/resource/resource_generator.rb | 4 +- .../scaffold_controller_generator.rb | 6 +- .../generators/test_unit/mailer/templates/fixture | 2 +- .../test_unit/mailer/templates/functional_test.rb | 12 +++- railties/lib/rails/generators/named_base.rb | 79 ++++++++++------------ railties/lib/rails/generators/resource_helpers.rb | 43 ++++++------ 8 files changed, 83 insertions(+), 84 deletions(-) (limited to 'railties/lib') diff --git a/railties/lib/generators/erb/mailer/templates/view.erb b/railties/lib/generators/erb/mailer/templates/view.erb index fcce7bd805..6d597256a6 100644 --- a/railties/lib/generators/erb/mailer/templates/view.erb +++ b/railties/lib/generators/erb/mailer/templates/view.erb @@ -1,3 +1,3 @@ <%= class_name %>#<%= @action %> -Find me in app/views/<%= @path %> +<%%= @greeting %>, find me in app/views/<%= @path %> diff --git a/railties/lib/generators/rails/mailer/templates/mailer.rb b/railties/lib/generators/rails/mailer/templates/mailer.rb index 90e0b712d6..5e7ef42370 100644 --- a/railties/lib/generators/rails/mailer/templates/mailer.rb +++ b/railties/lib/generators/rails/mailer/templates/mailer.rb @@ -1,14 +1,15 @@ class <%= class_name %> < ActionMailer::Base + delivers_from "mail@<%= application_name %>.com" <% for action in actions -%> - def <%= action %>(sent_at = Time.now) - subject '<%= class_name %>#<%= action %>' - recipients '' - from '' - sent_on sent_at - - body :greeting => 'Hi,' + # Subject can be set in your I18n file at config/locales/en.yml + # with the following lookup: + # + # en.actionmailer.<%= file_name %>.<%= action %>.subject + # + def <%= action %> + @greeting = "Hi" + mail(:to => "") end - <% end -%> -end +end \ No newline at end of file diff --git a/railties/lib/generators/rails/resource/resource_generator.rb b/railties/lib/generators/rails/resource/resource_generator.rb index 43c7cc85f4..5acb839f39 100644 --- a/railties/lib/generators/rails/resource/resource_generator.rb +++ b/railties/lib/generators/rails/resource/resource_generator.rb @@ -6,8 +6,8 @@ module Rails class ResourceGenerator < ModelGenerator #metagenerator include ResourceHelpers - hook_for :resource_controller, :required => true do |base, controller| - base.invoke controller, [ base.controller_name, base.options[:actions] ] + hook_for :resource_controller, :required => true do |controller| + invoke controller, [ controller_name, options[:actions] ] end class_option :actions, :type => :array, :banner => "ACTION ACTION", :default => [], diff --git a/railties/lib/generators/rails/scaffold_controller/scaffold_controller_generator.rb b/railties/lib/generators/rails/scaffold_controller/scaffold_controller_generator.rb index e544e29892..49af2974cd 100644 --- a/railties/lib/generators/rails/scaffold_controller/scaffold_controller_generator.rb +++ b/railties/lib/generators/rails/scaffold_controller/scaffold_controller_generator.rb @@ -18,9 +18,9 @@ module Rails hook_for :template_engine, :test_framework, :as => :scaffold - # Invoke the helper using the controller (pluralized) name. - hook_for :helper, :as => :scaffold do |base, invoked| - base.invoke invoked, [ base.controller_name ] + # Invoke the helper using the controller name (pluralized) + hook_for :helper, :as => :scaffold do |invoked| + invoke invoked, [ controller_name ] end end end diff --git a/railties/lib/generators/test_unit/mailer/templates/fixture b/railties/lib/generators/test_unit/mailer/templates/fixture index fcce7bd805..171648d6fd 100644 --- a/railties/lib/generators/test_unit/mailer/templates/fixture +++ b/railties/lib/generators/test_unit/mailer/templates/fixture @@ -1,3 +1,3 @@ <%= class_name %>#<%= @action %> -Find me in app/views/<%= @path %> +Hi, find me in app/views/<%= @path %> diff --git a/railties/lib/generators/test_unit/mailer/templates/functional_test.rb b/railties/lib/generators/test_unit/mailer/templates/functional_test.rb index d7366fea5f..2f694e431c 100644 --- a/railties/lib/generators/test_unit/mailer/templates/functional_test.rb +++ b/railties/lib/generators/test_unit/mailer/templates/functional_test.rb @@ -3,11 +3,17 @@ require 'test_helper' class <%= class_name %>Test < ActionMailer::TestCase <% for action in actions -%> test "<%= action %>" do - @expected.subject = '<%= class_name %>#<%= action %>' - @expected.body = read_fixture('<%= action %>') + @actual = <%= class_name %>.<%= action %> + + @expected.subject = <%= action.to_s.humanize.inspect %> + @expected.body = read_fixture("<%= action %>") @expected.date = Time.now - assert_equal @expected, <%= class_name %>.create_<%= action %>(@expected.date) + assert_difference "<%= class_name %>.deliveries.size" do + @actual.deliver + end + + assert_equal @expected.encoded, @actual.encoded end <% end -%> diff --git a/railties/lib/rails/generators/named_base.rb b/railties/lib/rails/generators/named_base.rb index 3e851bf888..12e918731e 100644 --- a/railties/lib/rails/generators/named_base.rb +++ b/railties/lib/rails/generators/named_base.rb @@ -6,17 +6,9 @@ module Rails class NamedBase < Base argument :name, :type => :string - no_tasks { - attr_reader :class_name, :singular_name, :plural_name, :table_name, - :class_path, :file_path, :class_nesting_depth - - alias :file_name :singular_name - } - def initialize(args, *options) #:nodoc: # Unfreeze name in case it's given as a frozen string args[0] = args[0].dup if args[0].is_a?(String) && args[0].frozen? - super assign_names!(self.name) parse_attributes! if respond_to?(:attributes) @@ -24,28 +16,48 @@ module Rails protected - def assign_names!(given_name) #:nodoc: - base_name, @class_path, @file_path, class_nesting, @class_nesting_depth = extract_modules(given_name) - class_name_without_nesting, @singular_name, @plural_name = inflect_names(base_name) + attr_reader :class_path, :file_name + alias :singular_name :file_name - @table_name = if pluralize_table_names? - plural_name - else - singular_name + def file_path + @file_path ||= (class_path + [file_name]).join('/') + end + + def class_name + @class_name ||= (class_path + [file_name]).map!{ |m| m.camelize }.join('::') + end + + def plural_name + @plural_name ||= singular_name.pluralize + end + + def i18n_scope + @i18n_scope ||= file_path.gsub('/', '.') + end + + def table_name + @table_name ||= begin + base = pluralize_table_names? ? plural_name : singular_name + (class_path + [base]).join('_') end + end - if class_nesting.empty? - @class_name = class_name_without_nesting + # Tries to retrieve the application name or simple return application. + def application_name + if defined?(Rails) && Rails.application + Rails.application.class.name.split('::').first.underscore else - @table_name = class_nesting.underscore << "_" << @table_name - @class_name = "#{class_nesting}::#{class_name_without_nesting}" + "application" end + end - @table_name.gsub!('/', '_') + def assign_names!(name) #:nodoc: + @class_path = name.include?('/') ? name.split('/') : name.split('::') + @class_path.map! { |m| m.underscore } + @file_name = @class_path.pop end - # Convert attributes hash into an array with GeneratedAttribute objects. - # + # Convert attributes array into GeneratedAttribute objects. def parse_attributes! #:nodoc: self.attributes = (attributes || []).map do |key_value| name, type = key_value.split(':') @@ -53,29 +65,6 @@ module Rails end end - # Extract modules from filesystem-style or ruby-style path. Both - # good/fun/stuff and Good::Fun::Stuff produce the same results. - # - def extract_modules(name) #:nodoc: - modules = name.include?('/') ? name.split('/') : name.split('::') - name = modules.pop - path = modules.map { |m| m.underscore } - - file_path = (path + [name.underscore]).join('/') - nesting = modules.map { |m| m.camelize }.join('::') - - [name, path, file_path, nesting, modules.size] - end - - # Receives name and return camelized, underscored and pluralized names. - # - def inflect_names(name) #:nodoc: - camel = name.camelize - under = camel.underscore - plural = under.pluralize - [camel, under, plural] - end - def pluralize_table_names? !defined?(ActiveRecord::Base) || ActiveRecord::Base.pluralize_table_names end diff --git a/railties/lib/rails/generators/resource_helpers.rb b/railties/lib/rails/generators/resource_helpers.rb index 7e00a222ed..3a98a8f9c1 100644 --- a/railties/lib/rails/generators/resource_helpers.rb +++ b/railties/lib/rails/generators/resource_helpers.rb @@ -9,14 +9,7 @@ module Rails mattr_accessor :skip_warn def self.included(base) #:nodoc: - base.class_eval do - class_option :force_plural, :type => :boolean, :desc => "Forces the use of a plural ModelName" - - no_tasks { - attr_reader :controller_name, :controller_class_name, :controller_file_name, - :controller_class_path, :controller_file_path - } - end + base.class_option :force_plural, :type => :boolean, :desc => "Forces the use of a plural ModelName" end # Set controller variables on initialization. @@ -29,29 +22,40 @@ module Rails say "Plural version of the model detected, using singularized version. Override with --force-plural." ResourceHelpers.skip_warn = true end - name.replace name.singularize - assign_names!(self.name) + assign_names!(name) end @controller_name = name.pluralize + end - base_name, @controller_class_path, @controller_file_path, class_nesting, class_nesting_depth = extract_modules(@controller_name) - class_name_without_nesting, @controller_file_name, controller_plural_name = inflect_names(base_name) + protected + + attr_reader :controller_name - @controller_class_name = if class_nesting.empty? - class_name_without_nesting - else - "#{class_nesting}::#{class_name_without_nesting}" + def controller_class_path + @class_path end - end - protected + def controller_file_name + @controller_file_name ||= file_name.pluralize + end + + def controller_file_path + @controller_file_path ||= (controller_class_path + [controller_file_name]).join('/') + end + + def controller_class_name + @controller_class_name ||= (controller_class_path + [controller_file_name]).map!{ |m| m.camelize }.join('::') + end + + def controller_i18n_scope + @controller_i18n_scope ||= controller_file_path.gsub('/', '.') + end # Loads the ORM::Generators::ActiveModel class. This class is responsable # to tell scaffold entities how to generate an specific method for the # ORM. Check Rails::Generators::ActiveModel for more information. - # def orm_class @orm_class ||= begin # Raise an error if the class_option :orm was not defined. @@ -68,7 +72,6 @@ module Rails end # Initialize ORM::Generators::ActiveModel to access instance methods. - # def orm_instance(name=file_name) @orm_instance ||= @orm_class.new(name) end -- cgit v1.2.3 From 2d1f9fb98f6b4f7afa469eba57eac4041c8ee539 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 25 Jan 2010 11:06:39 -0600 Subject: Plugins need to load before app initializers --- railties/lib/rails/application.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'railties/lib') diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index ab66d1e90b..6633c36a21 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -91,8 +91,8 @@ module Rails def initializers initializers = Bootstrap.initializers - initializers += super railties.all { |r| initializers += r.initializers } + initializers += super initializers += Finisher.initializers initializers end -- cgit v1.2.3 From 1177a40e68b6661d6d2cb4aefdd9a805459cd936 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Mon, 25 Jan 2010 22:00:07 +0100 Subject: Fix i18n locales order test. --- railties/lib/rails/engine.rb | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'railties/lib') diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index e40052e0f1..a9c94bc020 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -57,12 +57,12 @@ module Rails # Set the paths from which Rails will automatically load source files, # and the load_once paths. initializer :set_autoload_paths do |app| - ActiveSupport::Dependencies.load_paths.concat(config.load_paths) + ActiveSupport::Dependencies.load_paths.unshift(*config.load_paths) if reloadable?(app) - ActiveSupport::Dependencies.load_once_paths.concat(config.load_once_paths) + ActiveSupport::Dependencies.load_once_paths.unshift(*config.load_once_paths) else - ActiveSupport::Dependencies.load_once_paths.concat(config.load_paths) + ActiveSupport::Dependencies.load_once_paths.unshift(*config.load_paths) end # Freeze so future modifications will fail rather than do nothing mysteriously @@ -86,18 +86,20 @@ module Rails end end + # I18n load paths are a special case since the ones added + # later have higher priority. initializer :add_locales do - config.i18n.load_path.unshift(*paths.config.locales.to_a) + config.i18n.engines_load_path.concat(paths.config.locales.to_a) end initializer :add_view_paths do views = paths.app.views.to_a - ActionController::Base.view_paths.concat(views) if defined?(ActionController) - ActionMailer::Base.view_paths.concat(views) if defined?(ActionMailer) + ActionController::Base.view_paths.unshift(*views) if defined?(ActionController) + ActionMailer::Base.view_paths.unshift(*views) if defined?(ActionMailer) end initializer :add_metals do - Rails::Rack::Metal.paths.concat(paths.app.metals.to_a) + Rails::Rack::Metal.paths.unshift(*paths.app.metals.to_a) end initializer :load_application_initializers do -- cgit v1.2.3 From 02908e11425069e5b91cbf8ec3c8344a58493ef8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Mon, 25 Jan 2010 22:59:08 +0100 Subject: As first step setup the load path and lazy compare middlewares. --- railties/lib/rails/application/bootstrap.rb | 4 ++++ railties/lib/rails/engine.rb | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) (limited to 'railties/lib') diff --git a/railties/lib/rails/application/bootstrap.rb b/railties/lib/rails/application/bootstrap.rb index 3c339ffc57..f038027c97 100644 --- a/railties/lib/rails/application/bootstrap.rb +++ b/railties/lib/rails/application/bootstrap.rb @@ -76,6 +76,10 @@ module Rails initializer :initialize_dependency_mechanism do |app| ActiveSupport::Dependencies.mechanism = app.config.cache_classes ? :require : :load end + + initializer :bootstrap_load_path do + # This is just an initializer used as hook so all load paths are loaded together + end end end end \ No newline at end of file diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index a9c94bc020..8cb938c2b9 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -47,7 +47,7 @@ module Rails end # Add configured load paths to ruby load paths and remove duplicates. - initializer :set_load_path do + initializer :set_load_path, :before => :bootstrap_load_path do config.load_paths.reverse_each do |path| $LOAD_PATH.unshift(path) if File.directory?(path) end @@ -56,7 +56,7 @@ module Rails # Set the paths from which Rails will automatically load source files, # and the load_once paths. - initializer :set_autoload_paths do |app| + initializer :set_autoload_paths, :before => :bootstrap_load_path do |app| ActiveSupport::Dependencies.load_paths.unshift(*config.load_paths) if reloadable?(app) -- cgit v1.2.3 From 5d078692453c454289823700b67e64bcd4c8de7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 26 Jan 2010 00:08:08 +0100 Subject: Ensure all initializers are collections. --- railties/lib/rails/application.rb | 4 +-- railties/lib/rails/application/bootstrap.rb | 42 ++++++++++++++--------------- railties/lib/rails/application/finisher.rb | 16 +++++------ railties/lib/rails/initializable.rb | 9 ++++--- 4 files changed, 36 insertions(+), 35 deletions(-) (limited to 'railties/lib') diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index 6633c36a21..b7e5eb7a1d 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -90,10 +90,10 @@ module Rails end def initializers - initializers = Bootstrap.initializers + initializers = Bootstrap.initializers_for(self) railties.all { |r| initializers += r.initializers } initializers += super - initializers += Finisher.initializers + initializers += Finisher.initializers_for(self) initializers end diff --git a/railties/lib/rails/application/bootstrap.rb b/railties/lib/rails/application/bootstrap.rb index f038027c97..b20e53f2de 100644 --- a/railties/lib/rails/application/bootstrap.rb +++ b/railties/lib/rails/application/bootstrap.rb @@ -3,28 +3,28 @@ module Rails module Bootstrap include Initializable - initializer :load_environment_config do |app| - app.require_environment! + initializer :load_environment_config do + require_environment! end - initializer :load_all_active_support do |app| - require "active_support/all" unless app.config.active_support.bare + initializer :load_all_active_support do + require "active_support/all" unless config.active_support.bare end # Preload all frameworks specified by the Configuration#frameworks. # Used by Passenger to ensure everything's loaded before forking and # to avoid autoload race conditions in JRuby. - initializer :preload_frameworks do |app| + initializer :preload_frameworks do require 'active_support/dependencies' - ActiveSupport::Autoload.eager_autoload! if app.config.preload_frameworks + ActiveSupport::Autoload.eager_autoload! if config.preload_frameworks end # Initialize the logger early in the stack in case we need to log some deprecation. - initializer :initialize_logger do |app| - Rails.logger ||= app.config.logger || begin - path = app.config.paths.log.to_a.first + initializer :initialize_logger do + Rails.logger ||= config.logger || begin + path = config.paths.log.to_a.first logger = ActiveSupport::BufferedLogger.new(path) - logger.level = ActiveSupport::BufferedLogger.const_get(app.config.log_level.to_s.upcase) + logger.level = ActiveSupport::BufferedLogger.const_get(config.log_level.to_s.upcase) logger.auto_flushing = false if Rails.env.production? logger rescue StandardError => e @@ -39,23 +39,23 @@ module Rails end # Initialize cache early in the stack so railties can make use of it. - initializer :initialize_cache do |app| + initializer :initialize_cache do unless defined?(RAILS_CACHE) - silence_warnings { Object.const_set "RAILS_CACHE", ActiveSupport::Cache.lookup_store(app.config.cache_store) } + silence_warnings { Object.const_set "RAILS_CACHE", ActiveSupport::Cache.lookup_store(config.cache_store) } if RAILS_CACHE.respond_to?(:middleware) - app.config.middleware.insert_after(:"Rack::Lock", RAILS_CACHE.middleware) + config.middleware.insert_after(:"Rack::Lock", RAILS_CACHE.middleware) end end end # Initialize rails subscriber on top of notifications. - initializer :initialize_subscriber do |app| + initializer :initialize_subscriber do require 'active_support/notifications' - if app.config.colorize_logging == false - Rails::Subscriber.colorize_logging = false - app.config.generators.colorize_logging = false + if config.colorize_logging == false + Rails::Subscriber.colorize_logging = false + config.generators.colorize_logging = false end ActiveSupport::Notifications.subscribe do |*args| @@ -63,8 +63,8 @@ module Rails end end - initializer :set_clear_dependencies_hook do |app| - unless app.config.cache_classes + initializer :set_clear_dependencies_hook do + unless config.cache_classes ActionDispatch::Callbacks.after do ActiveSupport::Dependencies.clear end @@ -73,8 +73,8 @@ module Rails # Sets the dependency loading mechanism. # TODO: Remove files from the $" and always use require. - initializer :initialize_dependency_mechanism do |app| - ActiveSupport::Dependencies.mechanism = app.config.cache_classes ? :require : :load + initializer :initialize_dependency_mechanism do + ActiveSupport::Dependencies.mechanism = config.cache_classes ? :require : :load end initializer :bootstrap_load_path do diff --git a/railties/lib/rails/application/finisher.rb b/railties/lib/rails/application/finisher.rb index 5cc5b4ae88..d67420938a 100644 --- a/railties/lib/rails/application/finisher.rb +++ b/railties/lib/rails/application/finisher.rb @@ -15,26 +15,26 @@ module Rails end end - initializer :add_builtin_route do |app| + initializer :add_builtin_route do if Rails.env.development? Rails::Application::RoutesReloader.paths << File.join(RAILTIES_PATH, 'builtin', 'routes.rb') end end - initializer :build_middleware_stack do |app| - app.app + initializer :build_middleware_stack do + app end # Fires the user-supplied after_initialize block (config#after_initialize) - initializer :after_initialize do |app| - app.config.after_initialize_blocks.each do |block| - block.call(app) + initializer :after_initialize do + config.after_initialize_blocks.each do |block| + block.call(self) end end # Disable dependency loading during request cycle - initializer :disable_dependency_loading do |app| - if app.config.cache_classes && !app.config.dependency_loading + initializer :disable_dependency_loading do + if config.cache_classes && !config.dependency_loading ActiveSupport::Dependencies.unhook! end end diff --git a/railties/lib/rails/initializable.rb b/railties/lib/rails/initializable.rb index cea4a0fdf7..d91f67823f 100644 --- a/railties/lib/rails/initializable.rb +++ b/railties/lib/rails/initializable.rb @@ -64,10 +64,7 @@ module Rails end def initializers - @initializers ||= begin - initializers = self.class.initializers_chain - Collection.new(initializers.map { |i| i.bind(self) }) - end + @initializers ||= self.class.initializers_for(self) end module ClassMethods @@ -84,6 +81,10 @@ module Rails initializers end + def initializers_for(binding) + Collection.new(initializers_chain.map { |i| i.bind(binding) }) + end + def initializer(name, opts = {}, &blk) raise ArgumentError, "A block must be passed when defining an initializer" unless blk initializers << Initializer.new(name, nil, opts, &blk) -- cgit v1.2.3 From 1b3cb54ebae685d4db9eefc99ce68b36d5641751 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?= Date: Tue, 26 Jan 2010 01:06:48 +0100 Subject: More work on generated mailer templates. --- railties/lib/generators/erb/mailer/mailer_generator.rb | 2 +- railties/lib/generators/erb/mailer/templates/view.erb | 3 --- railties/lib/generators/erb/mailer/templates/view.text.erb | 3 +++ railties/lib/generators/rails/mailer/templates/mailer.rb | 4 ++-- .../generators/test_unit/mailer/templates/functional_test.rb | 10 +++------- 5 files changed, 9 insertions(+), 13 deletions(-) delete mode 100644 railties/lib/generators/erb/mailer/templates/view.erb create mode 100644 railties/lib/generators/erb/mailer/templates/view.text.erb (limited to 'railties/lib') diff --git a/railties/lib/generators/erb/mailer/mailer_generator.rb b/railties/lib/generators/erb/mailer/mailer_generator.rb index 4ec2f4c9f4..408c942cef 100644 --- a/railties/lib/generators/erb/mailer/mailer_generator.rb +++ b/railties/lib/generators/erb/mailer/mailer_generator.rb @@ -12,7 +12,7 @@ module Erb def create_view_files actions.each do |action| @action, @path = action, File.join(file_path, action) - template "view.erb", File.join("app/views", "#{@path}.erb") + template "view.text.erb", File.join("app/views", "#{@path}.text.erb") end end end diff --git a/railties/lib/generators/erb/mailer/templates/view.erb b/railties/lib/generators/erb/mailer/templates/view.erb deleted file mode 100644 index 6d597256a6..0000000000 --- a/railties/lib/generators/erb/mailer/templates/view.erb +++ /dev/null @@ -1,3 +0,0 @@ -<%= class_name %>#<%= @action %> - -<%%= @greeting %>, find me in app/views/<%= @path %> diff --git a/railties/lib/generators/erb/mailer/templates/view.text.erb b/railties/lib/generators/erb/mailer/templates/view.text.erb new file mode 100644 index 0000000000..6d597256a6 --- /dev/null +++ b/railties/lib/generators/erb/mailer/templates/view.text.erb @@ -0,0 +1,3 @@ +<%= class_name %>#<%= @action %> + +<%%= @greeting %>, find me in app/views/<%= @path %> diff --git a/railties/lib/generators/rails/mailer/templates/mailer.rb b/railties/lib/generators/rails/mailer/templates/mailer.rb index 5e7ef42370..1685b73633 100644 --- a/railties/lib/generators/rails/mailer/templates/mailer.rb +++ b/railties/lib/generators/rails/mailer/templates/mailer.rb @@ -1,5 +1,5 @@ class <%= class_name %> < ActionMailer::Base - delivers_from "mail@<%= application_name %>.com" + delivers_from "from@example.com" <% for action in actions -%> # Subject can be set in your I18n file at config/locales/en.yml @@ -9,7 +9,7 @@ class <%= class_name %> < ActionMailer::Base # def <%= action %> @greeting = "Hi" - mail(:to => "") + mail(:to => "to@example.com") end <% end -%> end \ No newline at end of file diff --git a/railties/lib/generators/test_unit/mailer/templates/functional_test.rb b/railties/lib/generators/test_unit/mailer/templates/functional_test.rb index 2f694e431c..fcebb40135 100644 --- a/railties/lib/generators/test_unit/mailer/templates/functional_test.rb +++ b/railties/lib/generators/test_unit/mailer/templates/functional_test.rb @@ -3,17 +3,13 @@ require 'test_helper' class <%= class_name %>Test < ActionMailer::TestCase <% for action in actions -%> test "<%= action %>" do - @actual = <%= class_name %>.<%= action %> - @expected.subject = <%= action.to_s.humanize.inspect %> + @expected.to = "to@example.com" + @expected.from = "from@example.com" @expected.body = read_fixture("<%= action %>") @expected.date = Time.now - assert_difference "<%= class_name %>.deliveries.size" do - @actual.deliver - end - - assert_equal @expected.encoded, @actual.encoded + assert_equal @expected, <%= class_name %>.<%= action %> end <% end -%> -- cgit v1.2.3 From b8c82edc1f5b95bceea3d40de778ec0cd5a37d15 Mon Sep 17 00:00:00 2001 From: Mikel Lindsaar Date: Tue, 26 Jan 2010 18:59:52 +1100 Subject: Updating generators for mailer to reflect changes in API --- railties/lib/generators/rails/mailer/templates/mailer.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'railties/lib') diff --git a/railties/lib/generators/rails/mailer/templates/mailer.rb b/railties/lib/generators/rails/mailer/templates/mailer.rb index 1685b73633..df9d0a0923 100644 --- a/railties/lib/generators/rails/mailer/templates/mailer.rb +++ b/railties/lib/generators/rails/mailer/templates/mailer.rb @@ -1,5 +1,5 @@ class <%= class_name %> < ActionMailer::Base - delivers_from "from@example.com" + self.defaults = { :from => "from@example.com" } <% for action in actions -%> # Subject can be set in your I18n file at config/locales/en.yml @@ -9,7 +9,7 @@ class <%= class_name %> < ActionMailer::Base # def <%= action %> @greeting = "Hi" - mail(:to => "to@example.com") + mail(:to => "to@example.org") end <% end -%> end \ No newline at end of file -- cgit v1.2.3 From 8fabcb2eca03150b1c0c3dbc88dd13123f76894f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?= Date: Tue, 26 Jan 2010 11:48:25 +0100 Subject: Update generators to use new defaults. --- railties/lib/generators/rails/mailer/templates/mailer.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'railties/lib') diff --git a/railties/lib/generators/rails/mailer/templates/mailer.rb b/railties/lib/generators/rails/mailer/templates/mailer.rb index df9d0a0923..cdc6e41266 100644 --- a/railties/lib/generators/rails/mailer/templates/mailer.rb +++ b/railties/lib/generators/rails/mailer/templates/mailer.rb @@ -1,5 +1,5 @@ class <%= class_name %> < ActionMailer::Base - self.defaults = { :from => "from@example.com" } + self.defaults :from => "from@example.com" <% for action in actions -%> # Subject can be set in your I18n file at config/locales/en.yml -- cgit v1.2.3 From db99324a89a2a681c6f6b0957dac7a309bfca574 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 26 Jan 2010 12:14:48 +0100 Subject: Ensure calling a method in Rails::Application does not instantiate a void application. --- railties/lib/rails/application.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'railties/lib') diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index b7e5eb7a1d..eba49e1520 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -16,10 +16,10 @@ module Rails alias :configure :class_eval def instance - if instance_of?(Rails::Application) - Rails.application.instance + if self == Rails::Application + Rails.application else - @instance ||= new + @@instance ||= new end end -- cgit v1.2.3 From 007c0bb3b0e678f685fb82c1eb6c20e73f601b16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 26 Jan 2010 12:16:37 +0100 Subject: Ensure proper class is shown on rake middleware. --- railties/lib/rails/tasks/middleware.rake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'railties/lib') diff --git a/railties/lib/rails/tasks/middleware.rake b/railties/lib/rails/tasks/middleware.rake index c3aaddb153..62e10e2beb 100644 --- a/railties/lib/rails/tasks/middleware.rake +++ b/railties/lib/rails/tasks/middleware.rake @@ -3,5 +3,5 @@ task :middleware => :environment do Rails.configuration.middleware.active.each do |middleware| puts "use #{middleware.inspect}" end - puts "run #{Rails::Application.class.name}" + puts "run #{Rails::Application.instance.class.name}" end -- cgit v1.2.3 From e6da2f651ffc24e5be3842051df73493d158a6b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 26 Jan 2010 12:23:02 +0100 Subject: Ensure app does not show up in generators. --- railties/lib/rails/generators.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'railties/lib') diff --git a/railties/lib/rails/generators.rb b/railties/lib/rails/generators.rb index 83b8c74966..2281746b00 100644 --- a/railties/lib/rails/generators.rb +++ b/railties/lib/rails/generators.rb @@ -200,6 +200,7 @@ module Rails # Print Rails defaults first. rails = groups.delete("rails") rails.map! { |n| n.sub(/^rails:/, '') } + rails.delete("app") print_list("rails", rails) groups.sort.each { |b, n| print_list(b, n) } -- cgit v1.2.3 From f8bf1982dff9cf0f35fb7a121932c794ecdc1cb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 26 Jan 2010 14:58:00 +0100 Subject: Add tests for explicit engines. --- railties/lib/rails/application.rb | 5 +---- railties/lib/rails/application/configurable.rb | 19 +++++++++++++++++++ railties/lib/rails/railtie.rb | 10 +--------- 3 files changed, 21 insertions(+), 13 deletions(-) create mode 100644 railties/lib/rails/application/configurable.rb (limited to 'railties/lib') diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index eba49e1520..12aa279959 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -6,6 +6,7 @@ require 'rails/engine' module Rails class Application < Engine autoload :Bootstrap, 'rails/application/bootstrap' + autoload :Configurable, 'rails/application/configurable' autoload :Configuration, 'rails/application/configuration' autoload :Finisher, 'rails/application/finisher' autoload :Railties, 'rails/application/railties' @@ -41,10 +42,6 @@ module Rails require environment if environment end - def config - @config ||= Application::Configuration.new(self.class.find_root_with_flag("config.ru", Dir.pwd)) - end - def routes ::ActionController::Routing::Routes end diff --git a/railties/lib/rails/application/configurable.rb b/railties/lib/rails/application/configurable.rb new file mode 100644 index 0000000000..f598e33965 --- /dev/null +++ b/railties/lib/rails/application/configurable.rb @@ -0,0 +1,19 @@ +module Rails + class Application + module Configurable + def self.included(base) + base.extend ClassMethods + end + + module ClassMethods + def inherited(base) + raise "You cannot inherit from a Rails::Application child" + end + end + + def config + @config ||= Application::Configuration.new(self.class.find_root_with_flag("config.ru", Dir.pwd)) + end + end + end +end \ No newline at end of file diff --git a/railties/lib/rails/railtie.rb b/railties/lib/rails/railtie.rb index 3cf358d75f..c038d0ac70 100644 --- a/railties/lib/rails/railtie.rb +++ b/railties/lib/rails/railtie.rb @@ -17,7 +17,7 @@ module Rails def inherited(base) unless abstract_railtie?(base) - base.send(:include, self::Configurable) if add_configurable?(base) + base.send(:include, self::Configurable) subclasses << base end end @@ -53,14 +53,6 @@ module Rails def abstract_railtie?(base) ABSTRACT_RAILTIES.include?(base.name) end - - # Just add configurable behavior if a Configurable module is defined - # and the class is a direct child from self. This is required to avoid - # application or plugins getting class configuration method from Railties - # and/or Engines. - def add_configurable?(base) - defined?(self::Configurable) && base.ancestors[1] == self - end end def rake_tasks -- cgit v1.2.3 From 517b35a2bbe5ec381fecbbc54e67ba053f9da420 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 26 Jan 2010 15:00:16 +0100 Subject: Middleware stack actually runs routes and not the application. --- railties/lib/rails/tasks/middleware.rake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'railties/lib') diff --git a/railties/lib/rails/tasks/middleware.rake b/railties/lib/rails/tasks/middleware.rake index 62e10e2beb..251da67c96 100644 --- a/railties/lib/rails/tasks/middleware.rake +++ b/railties/lib/rails/tasks/middleware.rake @@ -3,5 +3,5 @@ task :middleware => :environment do Rails.configuration.middleware.active.each do |middleware| puts "use #{middleware.inspect}" end - puts "run #{Rails::Application.instance.class.name}" + puts "run #{Rails::Application.instance.class.name}.routes" end -- cgit v1.2.3 From edb8131535c74ac215bf36c0d58d2019ed091a74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 26 Jan 2010 15:27:40 +0100 Subject: Move Rails::Rack::Metal to Rails::Application::Metal and just add cascade if any metal was declared. --- railties/lib/rails/application.rb | 1 + railties/lib/rails/application/metal.rb | 46 +++++++++++++++++++++++++++++++++ railties/lib/rails/configuration.rb | 2 +- railties/lib/rails/engine.rb | 2 +- railties/lib/rails/rack.rb | 1 - railties/lib/rails/rack/metal.rb | 41 ----------------------------- 6 files changed, 49 insertions(+), 44 deletions(-) create mode 100644 railties/lib/rails/application/metal.rb delete mode 100644 railties/lib/rails/rack/metal.rb (limited to 'railties/lib') diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index 12aa279959..9e41210119 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -9,6 +9,7 @@ module Rails autoload :Configurable, 'rails/application/configurable' autoload :Configuration, 'rails/application/configuration' autoload :Finisher, 'rails/application/finisher' + autoload :Metal, 'rails/application/metal' autoload :Railties, 'rails/application/railties' autoload :RoutesReloader, 'rails/application/routes_reloader' diff --git a/railties/lib/rails/application/metal.rb b/railties/lib/rails/application/metal.rb new file mode 100644 index 0000000000..17786dd4ba --- /dev/null +++ b/railties/lib/rails/application/metal.rb @@ -0,0 +1,46 @@ +require 'action_dispatch' + +module Rails + class Application + class Metal + def self.paths + @paths ||= [] + end + + def self.metals + @metals ||= [] + end + + def initialize(list=nil) + metals = [] + list = Array(list || :all).map(&:to_sym) + + self.class.paths.each do |path| + matcher = /\A#{Regexp.escape(path)}\/(.*)\.rb\Z/ + Dir.glob("#{path}/**/*.rb").sort.each do |metal_path| + metal = metal_path.sub(matcher, '\1').to_sym + next unless list.include?(metal) || list.include?(:all) + require_dependency metal + metals << metal + end + end + + metals = metals.sort_by do |m| + [list.index(m) || list.index(:all), m.to_s] + end + + @metals = metals.map { |m| m.to_s.camelize.constantize } + self.class.metals.concat(@metals) + end + + def new(app) + ActionDispatch::Cascade.new(@metals, app) + end + + def name + ActionDispatch::Cascade.name + end + alias_method :to_s, :name + end + end +end diff --git a/railties/lib/rails/configuration.rb b/railties/lib/rails/configuration.rb index 3c5c1c1e16..c5cb7b2d09 100644 --- a/railties/lib/rails/configuration.rb +++ b/railties/lib/rails/configuration.rb @@ -16,7 +16,7 @@ module Rails middleware.use('::ActionDispatch::Cookies') middleware.use(lambda { ActionController::Base.session_store }, lambda { ActionController::Base.session_options }) middleware.use('::ActionDispatch::Flash', :if => lambda { ActionController::Base.session_store }) - middleware.use(lambda { Rails::Rack::Metal.new(Rails.application.config.metals) }) + middleware.use(lambda { Rails::Application::Metal.new(Rails.application.config.metals) }, :if => lambda { Rails::Application::Metal.metals.any? }) middleware.use('ActionDispatch::ParamsParser') middleware.use('::Rack::MethodOverride') middleware.use('::ActionDispatch::Head') diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index 8cb938c2b9..ebbee67cf4 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -99,7 +99,7 @@ module Rails end initializer :add_metals do - Rails::Rack::Metal.paths.unshift(*paths.app.metals.to_a) + Rails::Application::Metal.paths.unshift(*paths.app.metals.to_a) end initializer :load_application_initializers do diff --git a/railties/lib/rails/rack.rb b/railties/lib/rails/rack.rb index 4bc0c2c88b..1f20ceae44 100644 --- a/railties/lib/rails/rack.rb +++ b/railties/lib/rails/rack.rb @@ -3,7 +3,6 @@ module Rails autoload :Debugger, "rails/rack/debugger" autoload :Logger, "rails/rack/logger" autoload :LogTailer, "rails/rack/log_tailer" - autoload :Metal, "rails/rack/metal" autoload :Static, "rails/rack/static" end end diff --git a/railties/lib/rails/rack/metal.rb b/railties/lib/rails/rack/metal.rb deleted file mode 100644 index 732936da32..0000000000 --- a/railties/lib/rails/rack/metal.rb +++ /dev/null @@ -1,41 +0,0 @@ -require 'action_dispatch' - -module Rails - module Rack - class Metal - def self.paths - @paths ||= [] - end - - def initialize(list=nil) - metals = [] - list = Array(list || :all).map(&:to_sym) - - self.class.paths.each do |path| - matcher = /\A#{Regexp.escape(path)}\/(.*)\.rb\Z/ - Dir.glob("#{path}/**/*.rb").sort.each do |metal_path| - metal = metal_path.sub(matcher, '\1').to_sym - next unless list.include?(metal) || list.include?(:all) - require_dependency metal - metals << metal - end - end - - metals = metals.sort_by do |m| - [list.index(m) || list.index(:all), m.to_s] - end - - @metals = metals.map { |m| m.to_s.camelize.constantize } - end - - def new(app) - ActionDispatch::Cascade.new(@metals, app) - end - - def name - ActionDispatch::Cascade.name - end - alias_method :to_s, :name - end - end -end -- cgit v1.2.3 From 081dfca33a7254ae86baa8feeb31f2c293b5f165 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 26 Jan 2010 15:37:45 +0100 Subject: Clean up Rails::Rack::Logger. --- railties/lib/rails/rack/logger.rb | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) (limited to 'railties/lib') diff --git a/railties/lib/rails/rack/logger.rb b/railties/lib/rails/rack/logger.rb index 91a613092f..de21fb4f10 100644 --- a/railties/lib/rails/rack/logger.rb +++ b/railties/lib/rails/rack/logger.rb @@ -9,27 +9,23 @@ module Rails end def call(env) - @env = env - before_dispatch - result = @app.call(@env) - after_dispatch - result + before_dispatch(env) + @app.call(env) + ensure + after_dispatch(env) end protected - def request - @request ||= ActionDispatch::Request.new(@env) - end - - def before_dispatch + def before_dispatch(env) + request = ActionDispatch::Request.new(env) path = request.request_uri.inspect rescue "unknown" info "\n\nStarted #{request.method.to_s.upcase} #{path} " << "for #{request.remote_ip} at #{Time.now.to_s(:db)}" end - def after_dispatch + def after_dispatch(env) Rails::Subscriber.flush_all! end -- cgit v1.2.3 From 458b6a7fc905a23643fc0d26146e6bf230c9c472 Mon Sep 17 00:00:00 2001 From: Mikel Lindsaar Date: Wed, 27 Jan 2010 10:35:56 +1100 Subject: Fixing mailer generators to use the right email address --- railties/lib/generators/test_unit/mailer/templates/functional_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'railties/lib') diff --git a/railties/lib/generators/test_unit/mailer/templates/functional_test.rb b/railties/lib/generators/test_unit/mailer/templates/functional_test.rb index fcebb40135..e1aeb2db90 100644 --- a/railties/lib/generators/test_unit/mailer/templates/functional_test.rb +++ b/railties/lib/generators/test_unit/mailer/templates/functional_test.rb @@ -4,7 +4,7 @@ class <%= class_name %>Test < ActionMailer::TestCase <% for action in actions -%> test "<%= action %>" do @expected.subject = <%= action.to_s.humanize.inspect %> - @expected.to = "to@example.com" + @expected.to = "to@example.org" @expected.from = "from@example.com" @expected.body = read_fixture("<%= action %>") @expected.date = Time.now -- cgit v1.2.3 From 04ad12d681b13569628f95b44fb13953305e4ddf Mon Sep 17 00:00:00 2001 From: "Stephen St. Martin" Date: Mon, 25 Jan 2010 17:39:00 -0500 Subject: initial jquery driver and compat, right now the only supported method is form_remote_tag --- .../templates/public/javascripts/jquery-1.4.min.js | 151 +++++++++++++++++++++ .../templates/public/javascripts/jquery.compat.js | 34 +++++ .../templates/public/javascripts/jquery.driver.js | 48 +++++++ 3 files changed, 233 insertions(+) create mode 100644 railties/lib/generators/rails/app/templates/public/javascripts/jquery-1.4.min.js create mode 100644 railties/lib/generators/rails/app/templates/public/javascripts/jquery.compat.js create mode 100644 railties/lib/generators/rails/app/templates/public/javascripts/jquery.driver.js (limited to 'railties/lib') diff --git a/railties/lib/generators/rails/app/templates/public/javascripts/jquery-1.4.min.js b/railties/lib/generators/rails/app/templates/public/javascripts/jquery-1.4.min.js new file mode 100644 index 0000000000..5c70e4c5f3 --- /dev/null +++ b/railties/lib/generators/rails/app/templates/public/javascripts/jquery-1.4.min.js @@ -0,0 +1,151 @@ +/*! + * jQuery JavaScript Library v1.4 + * http://jquery.com/ + * + * Copyright 2010, John Resig + * Dual licensed under the MIT or GPL Version 2 licenses. + * http://docs.jquery.com/License + * + * Includes Sizzle.js + * http://sizzlejs.com/ + * Copyright 2010, The Dojo Foundation + * Released under the MIT, BSD, and GPL Licenses. + * + * Date: Wed Jan 13 15:23:05 2010 -0500 + */ +(function(A,w){function oa(){if(!c.isReady){try{s.documentElement.doScroll("left")}catch(a){setTimeout(oa,1);return}c.ready()}}function La(a,b){b.src?c.ajax({url:b.src,async:false,dataType:"script"}):c.globalEval(b.text||b.textContent||b.innerHTML||"");b.parentNode&&b.parentNode.removeChild(b)}function $(a,b,d,f,e,i){var j=a.length;if(typeof b==="object"){for(var o in b)$(a,o,b[o],f,e,d);return a}if(d!==w){f=!i&&f&&c.isFunction(d);for(o=0;o-1){i=j.data;i.beforeFilter&&i.beforeFilter[a.type]&&!i.beforeFilter[a.type](a)||f.push(j.selector)}else delete t[p]}i=c(a.target).closest(f,a.currentTarget); +n=0;for(l=i.length;n)[^>]*$|^#([\w-]+)$/,Pa=/^.[^:#\[\.,]*$/,Qa=/\S/, +Ra=/^(\s|\u00A0)+|(\s|\u00A0)+$/g,Sa=/^<(\w+)\s*\/?>(?:<\/\1>)?$/,P=navigator.userAgent,xa=false,Q=[],M,ca=Object.prototype.toString,da=Object.prototype.hasOwnProperty,ea=Array.prototype.push,R=Array.prototype.slice,V=Array.prototype.indexOf;c.fn=c.prototype={init:function(a,b){var d,f;if(!a)return this;if(a.nodeType){this.context=this[0]=a;this.length=1;return this}if(typeof a==="string")if((d=Oa.exec(a))&&(d[1]||!b))if(d[1]){f=b?b.ownerDocument||b:s;if(a=Sa.exec(a))if(c.isPlainObject(b)){a=[s.createElement(a[1])]; +c.fn.attr.call(a,b,true)}else a=[f.createElement(a[1])];else{a=ua([d[1]],[f]);a=(a.cacheable?a.fragment.cloneNode(true):a.fragment).childNodes}}else{if(b=s.getElementById(d[2])){if(b.id!==d[2])return U.find(a);this.length=1;this[0]=b}this.context=s;this.selector=a;return this}else if(!b&&/^\w+$/.test(a)){this.selector=a;this.context=s;a=s.getElementsByTagName(a)}else return!b||b.jquery?(b||U).find(a):c(b).find(a);else if(c.isFunction(a))return U.ready(a);if(a.selector!==w){this.selector=a.selector; +this.context=a.context}return c.isArray(a)?this.setArray(a):c.makeArray(a,this)},selector:"",jquery:"1.4",length:0,size:function(){return this.length},toArray:function(){return R.call(this,0)},get:function(a){return a==null?this.toArray():a<0?this.slice(a)[0]:this[a]},pushStack:function(a,b,d){a=c(a||null);a.prevObject=this;a.context=this.context;if(b==="find")a.selector=this.selector+(this.selector?" ":"")+d;else if(b)a.selector=this.selector+"."+b+"("+d+")";return a},setArray:function(a){this.length= +0;ea.apply(this,a);return this},each:function(a,b){return c.each(this,a,b)},ready:function(a){c.bindReady();if(c.isReady)a.call(s,c);else Q&&Q.push(a);return this},eq:function(a){return a===-1?this.slice(a):this.slice(a,+a+1)},first:function(){return this.eq(0)},last:function(){return this.eq(-1)},slice:function(){return this.pushStack(R.apply(this,arguments),"slice",R.call(arguments).join(","))},map:function(a){return this.pushStack(c.map(this,function(b,d){return a.call(b,d,b)}))},end:function(){return this.prevObject|| +c(null)},push:ea,sort:[].sort,splice:[].splice};c.fn.init.prototype=c.fn;c.extend=c.fn.extend=function(){var a=arguments[0]||{},b=1,d=arguments.length,f=false,e,i,j,o;if(typeof a==="boolean"){f=a;a=arguments[1]||{};b=2}if(typeof a!=="object"&&!c.isFunction(a))a={};if(d===b){a=this;--b}for(;b
a";var e=d.getElementsByTagName("*"),i=d.getElementsByTagName("a")[0];if(!(!e||!e.length||!i)){c.support={leadingWhitespace:d.firstChild.nodeType===3,tbody:!d.getElementsByTagName("tbody").length, +htmlSerialize:!!d.getElementsByTagName("link").length,style:/red/.test(i.getAttribute("style")),hrefNormalized:i.getAttribute("href")==="/a",opacity:/^0.55$/.test(i.style.opacity),cssFloat:!!i.style.cssFloat,checkOn:d.getElementsByTagName("input")[0].value==="on",optSelected:s.createElement("select").appendChild(s.createElement("option")).selected,scriptEval:false,noCloneEvent:true,boxModel:null};b.type="text/javascript";try{b.appendChild(s.createTextNode("window."+f+"=1;"))}catch(j){}a.insertBefore(b, +a.firstChild);if(A[f]){c.support.scriptEval=true;delete A[f]}a.removeChild(b);if(d.attachEvent&&d.fireEvent){d.attachEvent("onclick",function o(){c.support.noCloneEvent=false;d.detachEvent("onclick",o)});d.cloneNode(true).fireEvent("onclick")}c(function(){var o=s.createElement("div");o.style.width=o.style.paddingLeft="1px";s.body.appendChild(o);c.boxModel=c.support.boxModel=o.offsetWidth===2;s.body.removeChild(o).style.display="none"});a=function(o){var p=s.createElement("div");o="on"+o;var n=o in +p;if(!n){p.setAttribute(o,"return;");n=typeof p[o]==="function"}return n};c.support.submitBubbles=a("submit");c.support.changeBubbles=a("change");a=b=d=e=i=null}})();c.props={"for":"htmlFor","class":"className",readonly:"readOnly",maxlength:"maxLength",cellspacing:"cellSpacing",rowspan:"rowSpan",colspan:"colSpan",tabindex:"tabIndex",usemap:"useMap",frameborder:"frameBorder"};var H="jQuery"+K(),Ta=0,ya={},Ua={};c.extend({cache:{},expando:H,noData:{embed:true,object:true,applet:true},data:function(a, +b,d){if(!(a.nodeName&&c.noData[a.nodeName.toLowerCase()])){a=a==A?ya:a;var f=a[H],e=c.cache;if(!b&&!f)return null;f||(f=++Ta);if(typeof b==="object"){a[H]=f;e=e[f]=c.extend(true,{},b)}else e=e[f]?e[f]:typeof d==="undefined"?Ua:(e[f]={});if(d!==w){a[H]=f;e[b]=d}return typeof b==="string"?e[b]:e}},removeData:function(a,b){if(!(a.nodeName&&c.noData[a.nodeName.toLowerCase()])){a=a==A?ya:a;var d=a[H],f=c.cache,e=f[d];if(b){if(e){delete e[b];c.isEmptyObject(e)&&c.removeData(a)}}else{try{delete a[H]}catch(i){a.removeAttribute&& +a.removeAttribute(H)}delete f[d]}}}});c.fn.extend({data:function(a,b){if(typeof a==="undefined"&&this.length)return c.data(this[0]);else if(typeof a==="object")return this.each(function(){c.data(this,a)});var d=a.split(".");d[1]=d[1]?"."+d[1]:"";if(b===w){var f=this.triggerHandler("getData"+d[1]+"!",[d[0]]);if(f===w&&this.length)f=c.data(this[0],a);return f===w&&d[1]?this.data(d[0]):f}else return this.trigger("setData"+d[1]+"!",[d[0],b]).each(function(){c.data(this,a,b)})},removeData:function(a){return this.each(function(){c.removeData(this, +a)})}});c.extend({queue:function(a,b,d){if(a){b=(b||"fx")+"queue";var f=c.data(a,b);if(!d)return f||[];if(!f||c.isArray(d))f=c.data(a,b,c.makeArray(d));else f.push(d);return f}},dequeue:function(a,b){b=b||"fx";var d=c.queue(a,b),f=d.shift();if(f==="inprogress")f=d.shift();if(f){b==="fx"&&d.unshift("inprogress");f.call(a,function(){c.dequeue(a,b)})}}});c.fn.extend({queue:function(a,b){if(typeof a!=="string"){b=a;a="fx"}if(b===w)return c.queue(this[0],a);return this.each(function(){var d=c.queue(this, +a,b);a==="fx"&&d[0]!=="inprogress"&&c.dequeue(this,a)})},dequeue:function(a){return this.each(function(){c.dequeue(this,a)})},delay:function(a,b){a=c.fx?c.fx.speeds[a]||a:a;b=b||"fx";return this.queue(b,function(){var d=this;setTimeout(function(){c.dequeue(d,b)},a)})},clearQueue:function(a){return this.queue(a||"fx",[])}});var za=/[\n\t]/g,fa=/\s+/,Va=/\r/g,Wa=/href|src|style/,Xa=/(button|input)/i,Ya=/(button|input|object|select|textarea)/i,Za=/^(a|area)$/i,Aa=/radio|checkbox/;c.fn.extend({attr:function(a, +b){return $(this,a,b,true,c.attr)},removeAttr:function(a){return this.each(function(){c.attr(this,a,"");this.nodeType===1&&this.removeAttribute(a)})},addClass:function(a){if(c.isFunction(a))return this.each(function(p){var n=c(this);n.addClass(a.call(this,p,n.attr("class")))});if(a&&typeof a==="string")for(var b=(a||"").split(fa),d=0,f=this.length;d-1)return true;return false},val:function(a){if(a===w){var b=this[0];if(b){if(c.nodeName(b,"option"))return(b.attributes.value||{}).specified?b.value:b.text;if(c.nodeName(b,"select")){var d=b.selectedIndex,f=[],e=b.options;b=b.type==="select-one";if(d<0)return null;var i=b?d:0;for(d=b?d+1:e.length;i=0;else if(c.nodeName(this,"select")){var z=c.makeArray(t);c("option",this).each(function(){this.selected=c.inArray(c(this).val(),z)>=0});if(!z.length)this.selectedIndex= +-1}else this.value=t}})}});c.extend({attrFn:{val:true,css:true,html:true,text:true,data:true,width:true,height:true,offset:true},attr:function(a,b,d,f){if(!a||a.nodeType===3||a.nodeType===8)return w;if(f&&b in c.attrFn)return c(a)[b](d);f=a.nodeType!==1||!c.isXMLDoc(a);var e=d!==w;b=f&&c.props[b]||b;if(a.nodeType===1){var i=Wa.test(b);if(b in a&&f&&!i){if(e){if(b==="type"&&Xa.test(a.nodeName)&&a.parentNode)throw"type property can't be changed";a[b]=d}if(c.nodeName(a,"form")&&a.getAttributeNode(b))return a.getAttributeNode(b).nodeValue; +if(b==="tabIndex")return(b=a.getAttributeNode("tabIndex"))&&b.specified?b.value:Ya.test(a.nodeName)||Za.test(a.nodeName)&&a.href?0:w;return a[b]}if(!c.support.style&&f&&b==="style"){if(e)a.style.cssText=""+d;return a.style.cssText}e&&a.setAttribute(b,""+d);a=!c.support.hrefNormalized&&f&&i?a.getAttribute(b,2):a.getAttribute(b);return a===null?w:a}return c.style(a,b,d)}});var $a=function(a){return a.replace(/[^\w\s\.\|`]/g,function(b){return"\\"+b})};c.event={add:function(a,b,d,f){if(!(a.nodeType=== +3||a.nodeType===8)){if(a.setInterval&&a!==A&&!a.frameElement)a=A;if(!d.guid)d.guid=c.guid++;if(f!==w){d=c.proxy(d);d.data=f}var e=c.data(a,"events")||c.data(a,"events",{}),i=c.data(a,"handle"),j;if(!i){j=function(){return typeof c!=="undefined"&&!c.event.triggered?c.event.handle.apply(j.elem,arguments):w};i=c.data(a,"handle",j)}if(i){i.elem=a;b=b.split(/\s+/);for(var o,p=0;o=b[p++];){var n=o.split(".");o=n.shift();d.type=n.slice(0).sort().join(".");var t=e[o],z=this.special[o]||{};if(!t){t=e[o]={}; +if(!z.setup||z.setup.call(a,f,n,d)===false)if(a.addEventListener)a.addEventListener(o,i,false);else a.attachEvent&&a.attachEvent("on"+o,i)}if(z.add)if((n=z.add.call(a,d,f,n,t))&&c.isFunction(n)){n.guid=n.guid||d.guid;d=n}t[d.guid]=d;this.global[o]=true}a=null}}},global:{},remove:function(a,b,d){if(!(a.nodeType===3||a.nodeType===8)){var f=c.data(a,"events"),e,i,j;if(f){if(b===w||typeof b==="string"&&b.charAt(0)===".")for(i in f)this.remove(a,i+(b||""));else{if(b.type){d=b.handler;b=b.type}b=b.split(/\s+/); +for(var o=0;i=b[o++];){var p=i.split(".");i=p.shift();var n=!p.length,t=c.map(p.slice(0).sort(),$a);t=new RegExp("(^|\\.)"+t.join("\\.(?:.*\\.)?")+"(\\.|$)");var z=this.special[i]||{};if(f[i]){if(d){j=f[i][d.guid];delete f[i][d.guid]}else for(var B in f[i])if(n||t.test(f[i][B].type))delete f[i][B];z.remove&&z.remove.call(a,p,j);for(e in f[i])break;if(!e){if(!z.teardown||z.teardown.call(a,p)===false)if(a.removeEventListener)a.removeEventListener(i,c.data(a,"handle"),false);else a.detachEvent&&a.detachEvent("on"+ +i,c.data(a,"handle"));e=null;delete f[i]}}}}for(e in f)break;if(!e){if(B=c.data(a,"handle"))B.elem=null;c.removeData(a,"events");c.removeData(a,"handle")}}}},trigger:function(a,b,d,f){var e=a.type||a;if(!f){a=typeof a==="object"?a[H]?a:c.extend(c.Event(e),a):c.Event(e);if(e.indexOf("!")>=0){a.type=e=e.slice(0,-1);a.exclusive=true}if(!d){a.stopPropagation();this.global[e]&&c.each(c.cache,function(){this.events&&this.events[e]&&c.event.trigger(a,b,this.handle.elem)})}if(!d||d.nodeType===3||d.nodeType=== +8)return w;a.result=w;a.target=d;b=c.makeArray(b);b.unshift(a)}a.currentTarget=d;var i=c.data(d,"handle");i&&i.apply(d,b);var j,o;try{if(!(d&&d.nodeName&&c.noData[d.nodeName.toLowerCase()])){j=d[e];o=d["on"+e]}}catch(p){}i=c.nodeName(d,"a")&&e==="click";if(!f&&j&&!a.isDefaultPrevented()&&!i){this.triggered=true;try{d[e]()}catch(n){}}else if(o&&d["on"+e].apply(d,b)===false)a.result=false;this.triggered=false;if(!a.isPropagationStopped())(d=d.parentNode||d.ownerDocument)&&c.event.trigger(a,b,d,true)}, +handle:function(a){var b,d;a=arguments[0]=c.event.fix(a||A.event);a.currentTarget=this;d=a.type.split(".");a.type=d.shift();b=!d.length&&!a.exclusive;var f=new RegExp("(^|\\.)"+d.slice(0).sort().join("\\.(?:.*\\.)?")+"(\\.|$)");d=(c.data(this,"events")||{})[a.type];for(var e in d){var i=d[e];if(b||f.test(i.type)){a.handler=i;a.data=i.data;i=i.apply(this,arguments);if(i!==w){a.result=i;if(i===false){a.preventDefault();a.stopPropagation()}}if(a.isImmediatePropagationStopped())break}}return a.result}, +props:"altKey attrChange attrName bubbles button cancelable charCode clientX clientY ctrlKey currentTarget data detail eventPhase fromElement handler keyCode layerX layerY metaKey newValue offsetX offsetY originalTarget pageX pageY prevValue relatedNode relatedTarget screenX screenY shiftKey srcElement target toElement view wheelDelta which".split(" "),fix:function(a){if(a[H])return a;var b=a;a=c.Event(b);for(var d=this.props.length,f;d;){f=this.props[--d];a[f]=b[f]}if(!a.target)a.target=a.srcElement|| +s;if(a.target.nodeType===3)a.target=a.target.parentNode;if(!a.relatedTarget&&a.fromElement)a.relatedTarget=a.fromElement===a.target?a.toElement:a.fromElement;if(a.pageX==null&&a.clientX!=null){b=s.documentElement;d=s.body;a.pageX=a.clientX+(b&&b.scrollLeft||d&&d.scrollLeft||0)-(b&&b.clientLeft||d&&d.clientLeft||0);a.pageY=a.clientY+(b&&b.scrollTop||d&&d.scrollTop||0)-(b&&b.clientTop||d&&d.clientTop||0)}if(!a.which&&(a.charCode||a.charCode===0?a.charCode:a.keyCode))a.which=a.charCode||a.keyCode;if(!a.metaKey&& +a.ctrlKey)a.metaKey=a.ctrlKey;if(!a.which&&a.button!==w)a.which=a.button&1?1:a.button&2?3:a.button&4?2:0;return a},guid:1E8,proxy:c.proxy,special:{ready:{setup:c.bindReady,teardown:c.noop},live:{add:function(a,b){c.extend(a,b||{});a.guid+=b.selector+b.live;c.event.add(this,b.live,qa,b)},remove:function(a){if(a.length){var b=0,d=new RegExp("(^|\\.)"+a[0]+"(\\.|$)");c.each(c.data(this,"events").live||{},function(){d.test(this.type)&&b++});b<1&&c.event.remove(this,a[0],qa)}},special:{}},beforeunload:{setup:function(a, +b,d){if(this.setInterval)this.onbeforeunload=d;return false},teardown:function(a,b){if(this.onbeforeunload===b)this.onbeforeunload=null}}}};c.Event=function(a){if(!this.preventDefault)return new c.Event(a);if(a&&a.type){this.originalEvent=a;this.type=a.type}else this.type=a;this.timeStamp=K();this[H]=true};c.Event.prototype={preventDefault:function(){this.isDefaultPrevented=ba;var a=this.originalEvent;if(a){a.preventDefault&&a.preventDefault();a.returnValue=false}},stopPropagation:function(){this.isPropagationStopped= +ba;var a=this.originalEvent;if(a){a.stopPropagation&&a.stopPropagation();a.cancelBubble=true}},stopImmediatePropagation:function(){this.isImmediatePropagationStopped=ba;this.stopPropagation()},isDefaultPrevented:aa,isPropagationStopped:aa,isImmediatePropagationStopped:aa};var Ba=function(a){for(var b=a.relatedTarget;b&&b!==this;)try{b=b.parentNode}catch(d){break}if(b!==this){a.type=a.data;c.event.handle.apply(this,arguments)}},Ca=function(a){a.type=a.data;c.event.handle.apply(this,arguments)};c.each({mouseenter:"mouseover", +mouseleave:"mouseout"},function(a,b){c.event.special[a]={setup:function(d){c.event.add(this,b,d&&d.selector?Ca:Ba,a)},teardown:function(d){c.event.remove(this,b,d&&d.selector?Ca:Ba)}}});if(!c.support.submitBubbles)c.event.special.submit={setup:function(a,b,d){if(this.nodeName.toLowerCase()!=="form"){c.event.add(this,"click.specialSubmit."+d.guid,function(f){var e=f.target,i=e.type;if((i==="submit"||i==="image")&&c(e).closest("form").length)return pa("submit",this,arguments)});c.event.add(this,"keypress.specialSubmit."+ +d.guid,function(f){var e=f.target,i=e.type;if((i==="text"||i==="password")&&c(e).closest("form").length&&f.keyCode===13)return pa("submit",this,arguments)})}else return false},remove:function(a,b){c.event.remove(this,"click.specialSubmit"+(b?"."+b.guid:""));c.event.remove(this,"keypress.specialSubmit"+(b?"."+b.guid:""))}};if(!c.support.changeBubbles){var ga=/textarea|input|select/i;function Da(a){var b=a.type,d=a.value;if(b==="radio"||b==="checkbox")d=a.checked;else if(b==="select-multiple")d=a.selectedIndex> +-1?c.map(a.options,function(f){return f.selected}).join("-"):"";else if(a.nodeName.toLowerCase()==="select")d=a.selectedIndex;return d}function ha(a,b){var d=a.target,f,e;if(!(!ga.test(d.nodeName)||d.readOnly)){f=c.data(d,"_change_data");e=Da(d);if(e!==f){if(a.type!=="focusout"||d.type!=="radio")c.data(d,"_change_data",e);if(d.type!=="select"&&(f!=null||e)){a.type="change";return c.event.trigger(a,b,this)}}}}c.event.special.change={filters:{focusout:ha,click:function(a){var b=a.target,d=b.type;if(d=== +"radio"||d==="checkbox"||b.nodeName.toLowerCase()==="select")return ha.call(this,a)},keydown:function(a){var b=a.target,d=b.type;if(a.keyCode===13&&b.nodeName.toLowerCase()!=="textarea"||a.keyCode===32&&(d==="checkbox"||d==="radio")||d==="select-multiple")return ha.call(this,a)},beforeactivate:function(a){a=a.target;a.nodeName.toLowerCase()==="input"&&a.type==="radio"&&c.data(a,"_change_data",Da(a))}},setup:function(a,b,d){for(var f in W)c.event.add(this,f+".specialChange."+d.guid,W[f]);return ga.test(this.nodeName)}, +remove:function(a,b){for(var d in W)c.event.remove(this,d+".specialChange"+(b?"."+b.guid:""),W[d]);return ga.test(this.nodeName)}};var W=c.event.special.change.filters}s.addEventListener&&c.each({focus:"focusin",blur:"focusout"},function(a,b){function d(f){f=c.event.fix(f);f.type=b;return c.event.handle.call(this,f)}c.event.special[b]={setup:function(){this.addEventListener(a,d,true)},teardown:function(){this.removeEventListener(a,d,true)}}});c.each(["bind","one"],function(a,b){c.fn[b]=function(d, +f,e){if(typeof d==="object"){for(var i in d)this[b](i,f,d[i],e);return this}if(c.isFunction(f)){thisObject=e;e=f;f=w}var j=b==="one"?c.proxy(e,function(o){c(this).unbind(o,j);return e.apply(this,arguments)}):e;return d==="unload"&&b!=="one"?this.one(d,f,e,thisObject):this.each(function(){c.event.add(this,d,j,f)})}});c.fn.extend({unbind:function(a,b){if(typeof a==="object"&&!a.preventDefault){for(var d in a)this.unbind(d,a[d]);return this}return this.each(function(){c.event.remove(this,a,b)})},trigger:function(a, +b){return this.each(function(){c.event.trigger(a,b,this)})},triggerHandler:function(a,b){if(this[0]){a=c.Event(a);a.preventDefault();a.stopPropagation();c.event.trigger(a,b,this[0]);return a.result}},toggle:function(a){for(var b=arguments,d=1;d0){y=u;break}}u=u[g]}m[r]=y}}}var f=/((?:\((?:\([^()]+\)|[^()]+)+\)|\[(?:\[[^[\]]*\]|['"][^'"]*['"]|[^[\]'"]+)+\]|\\.|[^ >+~,(\[\\]+)+|[>+~])(\s*,\s*)?((?:.|\r|\n)*)/g, +e=0,i=Object.prototype.toString,j=false,o=true;[0,0].sort(function(){o=false;return 0});var p=function(g,h,k,m){k=k||[];var r=h=h||s;if(h.nodeType!==1&&h.nodeType!==9)return[];if(!g||typeof g!=="string")return k;for(var q=[],v,u,y,S,I=true,N=x(h),J=g;(f.exec(""),v=f.exec(J))!==null;){J=v[3];q.push(v[1]);if(v[2]){S=v[3];break}}if(q.length>1&&t.exec(g))if(q.length===2&&n.relative[q[0]])u=ia(q[0]+q[1],h);else for(u=n.relative[q[0]]?[h]:p(q.shift(),h);q.length;){g=q.shift();if(n.relative[g])g+=q.shift(); +u=ia(g,u)}else{if(!m&&q.length>1&&h.nodeType===9&&!N&&n.match.ID.test(q[0])&&!n.match.ID.test(q[q.length-1])){v=p.find(q.shift(),h,N);h=v.expr?p.filter(v.expr,v.set)[0]:v.set[0]}if(h){v=m?{expr:q.pop(),set:B(m)}:p.find(q.pop(),q.length===1&&(q[0]==="~"||q[0]==="+")&&h.parentNode?h.parentNode:h,N);u=v.expr?p.filter(v.expr,v.set):v.set;if(q.length>0)y=B(u);else I=false;for(;q.length;){var E=q.pop();v=E;if(n.relative[E])v=q.pop();else E="";if(v==null)v=h;n.relative[E](y,v,N)}}else y=[]}y||(y=u);if(!y)throw"Syntax error, unrecognized expression: "+ +(E||g);if(i.call(y)==="[object Array]")if(I)if(h&&h.nodeType===1)for(g=0;y[g]!=null;g++){if(y[g]&&(y[g]===true||y[g].nodeType===1&&F(h,y[g])))k.push(u[g])}else for(g=0;y[g]!=null;g++)y[g]&&y[g].nodeType===1&&k.push(u[g]);else k.push.apply(k,y);else B(y,k);if(S){p(S,r,k,m);p.uniqueSort(k)}return k};p.uniqueSort=function(g){if(D){j=o;g.sort(D);if(j)for(var h=1;h":function(g,h){var k=typeof h==="string";if(k&&!/\W/.test(h)){h=h.toLowerCase();for(var m=0,r=g.length;m=0))k||m.push(v);else if(k)h[q]=false;return false},ID:function(g){return g[1].replace(/\\/g,"")},TAG:function(g){return g[1].toLowerCase()}, +CHILD:function(g){if(g[1]==="nth"){var h=/(-?)(\d*)n((?:\+|-)?\d*)/.exec(g[2]==="even"&&"2n"||g[2]==="odd"&&"2n+1"||!/\D/.test(g[2])&&"0n+"+g[2]||g[2]);g[2]=h[1]+(h[2]||1)-0;g[3]=h[3]-0}g[0]=e++;return g},ATTR:function(g,h,k,m,r,q){h=g[1].replace(/\\/g,"");if(!q&&n.attrMap[h])g[1]=n.attrMap[h];if(g[2]==="~=")g[4]=" "+g[4]+" ";return g},PSEUDO:function(g,h,k,m,r){if(g[1]==="not")if((f.exec(g[3])||"").length>1||/^\w/.test(g[3]))g[3]=p(g[3],null,null,h);else{g=p.filter(g[3],h,k,true^r);k||m.push.apply(m, +g);return false}else if(n.match.POS.test(g[0])||n.match.CHILD.test(g[0]))return true;return g},POS:function(g){g.unshift(true);return g}},filters:{enabled:function(g){return g.disabled===false&&g.type!=="hidden"},disabled:function(g){return g.disabled===true},checked:function(g){return g.checked===true},selected:function(g){return g.selected===true},parent:function(g){return!!g.firstChild},empty:function(g){return!g.firstChild},has:function(g,h,k){return!!p(k[3],g).length},header:function(g){return/h\d/i.test(g.nodeName)}, +text:function(g){return"text"===g.type},radio:function(g){return"radio"===g.type},checkbox:function(g){return"checkbox"===g.type},file:function(g){return"file"===g.type},password:function(g){return"password"===g.type},submit:function(g){return"submit"===g.type},image:function(g){return"image"===g.type},reset:function(g){return"reset"===g.type},button:function(g){return"button"===g.type||g.nodeName.toLowerCase()==="button"},input:function(g){return/input|select|textarea|button/i.test(g.nodeName)}}, +setFilters:{first:function(g,h){return h===0},last:function(g,h,k,m){return h===m.length-1},even:function(g,h){return h%2===0},odd:function(g,h){return h%2===1},lt:function(g,h,k){return hk[3]-0},nth:function(g,h,k){return k[3]-0===h},eq:function(g,h,k){return k[3]-0===h}},filter:{PSEUDO:function(g,h,k,m){var r=h[1],q=n.filters[r];if(q)return q(g,k,h,m);else if(r==="contains")return(g.textContent||g.innerText||a([g])||"").indexOf(h[3])>=0;else if(r==="not"){h= +h[3];k=0;for(m=h.length;k=0}},ID:function(g,h){return g.nodeType===1&&g.getAttribute("id")===h},TAG:function(g,h){return h==="*"&&g.nodeType===1||g.nodeName.toLowerCase()===h},CLASS:function(g,h){return(" "+(g.className||g.getAttribute("class"))+" ").indexOf(h)>-1},ATTR:function(g,h){var k=h[1];g=n.attrHandle[k]?n.attrHandle[k](g):g[k]!=null?g[k]:g.getAttribute(k);k=g+"";var m=h[2];h=h[4];return g==null?m==="!=":m=== +"="?k===h:m==="*="?k.indexOf(h)>=0:m==="~="?(" "+k+" ").indexOf(h)>=0:!h?k&&g!==false:m==="!="?k!==h:m==="^="?k.indexOf(h)===0:m==="$="?k.substr(k.length-h.length)===h:m==="|="?k===h||k.substr(0,h.length+1)===h+"-":false},POS:function(g,h,k,m){var r=n.setFilters[h[2]];if(r)return r(g,k,h,m)}}},t=n.match.POS;for(var z in n.match){n.match[z]=new RegExp(n.match[z].source+/(?![^\[]*\])(?![^\(]*\))/.source);n.leftMatch[z]=new RegExp(/(^(?:.|\r|\n)*?)/.source+n.match[z].source.replace(/\\(\d+)/g,function(g, +h){return"\\"+(h-0+1)}))}var B=function(g,h){g=Array.prototype.slice.call(g,0);if(h){h.push.apply(h,g);return h}return g};try{Array.prototype.slice.call(s.documentElement.childNodes,0)}catch(C){B=function(g,h){h=h||[];if(i.call(g)==="[object Array]")Array.prototype.push.apply(h,g);else if(typeof g.length==="number")for(var k=0,m=g.length;k";var k=s.documentElement;k.insertBefore(g,k.firstChild);if(s.getElementById(h)){n.find.ID=function(m,r,q){if(typeof r.getElementById!=="undefined"&&!q)return(r=r.getElementById(m[1]))?r.id===m[1]||typeof r.getAttributeNode!=="undefined"&& +r.getAttributeNode("id").nodeValue===m[1]?[r]:w:[]};n.filter.ID=function(m,r){var q=typeof m.getAttributeNode!=="undefined"&&m.getAttributeNode("id");return m.nodeType===1&&q&&q.nodeValue===r}}k.removeChild(g);k=g=null})();(function(){var g=s.createElement("div");g.appendChild(s.createComment(""));if(g.getElementsByTagName("*").length>0)n.find.TAG=function(h,k){k=k.getElementsByTagName(h[1]);if(h[1]==="*"){h=[];for(var m=0;k[m];m++)k[m].nodeType===1&&h.push(k[m]);k=h}return k};g.innerHTML=""; +if(g.firstChild&&typeof g.firstChild.getAttribute!=="undefined"&&g.firstChild.getAttribute("href")!=="#")n.attrHandle.href=function(h){return h.getAttribute("href",2)};g=null})();s.querySelectorAll&&function(){var g=p,h=s.createElement("div");h.innerHTML="

";if(!(h.querySelectorAll&&h.querySelectorAll(".TEST").length===0)){p=function(m,r,q,v){r=r||s;if(!v&&r.nodeType===9&&!x(r))try{return B(r.querySelectorAll(m),q)}catch(u){}return g(m,r,q,v)};for(var k in g)p[k]=g[k];h=null}}(); +(function(){var g=s.createElement("div");g.innerHTML="
";if(!(!g.getElementsByClassName||g.getElementsByClassName("e").length===0)){g.lastChild.className="e";if(g.getElementsByClassName("e").length!==1){n.order.splice(1,0,"CLASS");n.find.CLASS=function(h,k,m){if(typeof k.getElementsByClassName!=="undefined"&&!m)return k.getElementsByClassName(h[1])};g=null}}})();var F=s.compareDocumentPosition?function(g,h){return g.compareDocumentPosition(h)&16}:function(g, +h){return g!==h&&(g.contains?g.contains(h):true)},x=function(g){return(g=(g?g.ownerDocument||g:0).documentElement)?g.nodeName!=="HTML":false},ia=function(g,h){var k=[],m="",r;for(h=h.nodeType?[h]:h;r=n.match.PSEUDO.exec(g);){m+=r[0];g=g.replace(n.match.PSEUDO,"")}g=n.relative[g]?g+"*":g;r=0;for(var q=h.length;r=0===d})};c.fn.extend({find:function(a){for(var b=this.pushStack("","find",a),d=0,f=0,e=this.length;f0)for(var i=d;i0},closest:function(a,b){if(c.isArray(a)){var d=[],f=this[0],e,i= +{},j;if(f&&a.length){e=0;for(var o=a.length;e-1:c(f).is(e)){d.push({selector:j,elem:f});delete i[j]}}f=f.parentNode}}return d}var p=c.expr.match.POS.test(a)?c(a,b||this.context):null;return this.map(function(n,t){for(;t&&t.ownerDocument&&t!==b;){if(p?p.index(t)>-1:c(t).is(a))return t;t=t.parentNode}return null})},index:function(a){if(!a||typeof a=== +"string")return c.inArray(this[0],a?c(a):this.parent().children());return c.inArray(a.jquery?a[0]:a,this)},add:function(a,b){a=typeof a==="string"?c(a,b||this.context):c.makeArray(a);b=c.merge(this.get(),a);return this.pushStack(sa(a[0])||sa(b[0])?b:c.unique(b))},andSelf:function(){return this.add(this.prevObject)}});c.each({parent:function(a){return(a=a.parentNode)&&a.nodeType!==11?a:null},parents:function(a){return c.dir(a,"parentNode")},parentsUntil:function(a,b,d){return c.dir(a,"parentNode", +d)},next:function(a){return c.nth(a,2,"nextSibling")},prev:function(a){return c.nth(a,2,"previousSibling")},nextAll:function(a){return c.dir(a,"nextSibling")},prevAll:function(a){return c.dir(a,"previousSibling")},nextUntil:function(a,b,d){return c.dir(a,"nextSibling",d)},prevUntil:function(a,b,d){return c.dir(a,"previousSibling",d)},siblings:function(a){return c.sibling(a.parentNode.firstChild,a)},children:function(a){return c.sibling(a.firstChild)},contents:function(a){return c.nodeName(a,"iframe")? +a.contentDocument||a.contentWindow.document:c.makeArray(a.childNodes)}},function(a,b){c.fn[a]=function(d,f){var e=c.map(this,b,d);ab.test(a)||(f=d);if(f&&typeof f==="string")e=c.filter(f,e);e=this.length>1?c.unique(e):e;if((this.length>1||cb.test(f))&&bb.test(a))e=e.reverse();return this.pushStack(e,a,R.call(arguments).join(","))}});c.extend({filter:function(a,b,d){if(d)a=":not("+a+")";return c.find.matches(a,b)},dir:function(a,b,d){var f=[];for(a=a[b];a&&a.nodeType!==9&&(d===w||!c(a).is(d));){a.nodeType=== +1&&f.push(a);a=a[b]}return f},nth:function(a,b,d){b=b||1;for(var f=0;a;a=a[d])if(a.nodeType===1&&++f===b)break;return a},sibling:function(a,b){for(var d=[];a;a=a.nextSibling)a.nodeType===1&&a!==b&&d.push(a);return d}});var Ga=/ jQuery\d+="(?:\d+|null)"/g,Y=/^\s+/,db=/(<([\w:]+)[^>]*?)\/>/g,eb=/^(?:area|br|col|embed|hr|img|input|link|meta|param)$/i,Ha=/<([\w:]+)/,fb=/"},G={option:[1,""], +legend:[1,"
","
"],thead:[1,"","
"],tr:[2,"","
"],td:[3,"","
"],col:[2,"","
"],area:[1,"",""],_default:[0,"",""]};G.optgroup=G.option;G.tbody=G.tfoot=G.colgroup=G.caption=G.thead;G.th=G.td;if(!c.support.htmlSerialize)G._default=[1,"div
","
"];c.fn.extend({text:function(a){if(c.isFunction(a))return this.each(function(b){var d=c(this); +return d.text(a.call(this,b,d.text()))});if(typeof a!=="object"&&a!==w)return this.empty().append((this[0]&&this[0].ownerDocument||s).createTextNode(a));return c.getText(this)},wrapAll:function(a){if(c.isFunction(a))return this.each(function(d){c(this).wrapAll(a.call(this,d))});if(this[0]){var b=c(a,this[0].ownerDocument).eq(0).clone(true);this[0].parentNode&&b.insertBefore(this[0]);b.map(function(){for(var d=this;d.firstChild&&d.firstChild.nodeType===1;)d=d.firstChild;return d}).append(this)}return this}, +wrapInner:function(a){return this.each(function(){var b=c(this),d=b.contents();d.length?d.wrapAll(a):b.append(a)})},wrap:function(a){return this.each(function(){c(this).wrapAll(a)})},unwrap:function(){return this.parent().each(function(){c.nodeName(this,"body")||c(this).replaceWith(this.childNodes)}).end()},append:function(){return this.domManip(arguments,true,function(a){this.nodeType===1&&this.appendChild(a)})},prepend:function(){return this.domManip(arguments,true,function(a){this.nodeType===1&& +this.insertBefore(a,this.firstChild)})},before:function(){if(this[0]&&this[0].parentNode)return this.domManip(arguments,false,function(b){this.parentNode.insertBefore(b,this)});else if(arguments.length){var a=c(arguments[0]);a.push.apply(a,this.toArray());return this.pushStack(a,"before",arguments)}},after:function(){if(this[0]&&this[0].parentNode)return this.domManip(arguments,false,function(b){this.parentNode.insertBefore(b,this.nextSibling)});else if(arguments.length){var a=this.pushStack(this, +"after",arguments);a.push.apply(a,c(arguments[0]).toArray());return a}},clone:function(a){var b=this.map(function(){if(!c.support.noCloneEvent&&!c.isXMLDoc(this)){var d=this.outerHTML,f=this.ownerDocument;if(!d){d=f.createElement("div");d.appendChild(this.cloneNode(true));d=d.innerHTML}return c.clean([d.replace(Ga,"").replace(Y,"")],f)[0]}else return this.cloneNode(true)});if(a===true){ta(this,b);ta(this.find("*"),b.find("*"))}return b},html:function(a){if(a===w)return this[0]&&this[0].nodeType=== +1?this[0].innerHTML.replace(Ga,""):null;else if(typeof a==="string"&&!/