mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
Create configurable modules and ensure that they are added only on direct children.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
24
railties/lib/rails/engine/configurable.rb
Normal file
24
railties/lib/rails/engine/configurable.rb
Normal file
@@ -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
|
||||
@@ -1,27 +1,21 @@
|
||||
module Rails
|
||||
class Plugin < Engine
|
||||
class << self
|
||||
def inherited(base)
|
||||
raise "You should not inherit from Rails::Plugin"
|
||||
def self.inherited(base)
|
||||
raise "You cannot inherit from Rails::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
|
||||
|
||||
def config
|
||||
raise "Plugins does not provide configuration at the class level"
|
||||
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
|
||||
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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
23
railties/lib/rails/railtie/configurable.rb
Normal file
23
railties/lib/rails/railtie/configurable.rb
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user