2010-01-23 16:30:17 -05:00
|
|
|
require 'rails/engine'
|
2010-02-25 19:32:24 -05:00
|
|
|
require 'active_support/core_ext/array/conversions'
|
2010-01-23 16:30:17 -05:00
|
|
|
|
2007-11-08 00:29:44 -05:00
|
|
|
module Rails
|
2010-02-02 14:05:26 -05:00
|
|
|
# Rails::Plugin is nothing more than a Rails::Engine, but since it's loaded too late
|
|
|
|
# in the boot process, it does not have the same configuration powers as a bare
|
|
|
|
# Rails::Engine.
|
|
|
|
#
|
|
|
|
# Opposite to Rails::Railtie and Rails::Engine, you are not supposed to inherit from
|
|
|
|
# Rails::Plugin. Rails::Plugin is automatically configured to be an engine by simply
|
|
|
|
# placing inside vendor/plugins. Since this is done automatically, you actually cannot
|
|
|
|
# declare a Rails::Engine inside your Plugin, otherwise it would cause the same files
|
|
|
|
# to be loaded twice. This means that if you want to ship an Engine as gem it cannot
|
|
|
|
# be used as plugin and vice-versa.
|
|
|
|
#
|
|
|
|
# Besides this conceptual difference, the only difference between Rails::Engine and
|
|
|
|
# Rails::Plugin is that plugins automatically load the file "init.rb" at the plugin
|
|
|
|
# root during the boot process.
|
|
|
|
#
|
2010-01-23 11:51:48 -05:00
|
|
|
class Plugin < Engine
|
2010-01-23 12:41:53 -05:00
|
|
|
def self.inherited(base)
|
|
|
|
raise "You cannot inherit from Rails::Plugin"
|
|
|
|
end
|
2007-11-08 00:29:44 -05:00
|
|
|
|
2010-01-23 12:41:53 -05:00
|
|
|
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
|
2010-01-23 11:51:48 -05:00
|
|
|
end
|
2010-01-23 12:41:53 -05:00
|
|
|
end
|
2008-11-26 08:41:20 -05:00
|
|
|
|
2010-01-23 12:41:53 -05:00
|
|
|
plugins.sort_by do |p|
|
|
|
|
[list.index(p.name) || list.index(:all), p.name.to_s]
|
2009-11-02 20:19:03 -05:00
|
|
|
end
|
2009-12-31 16:11:54 -05:00
|
|
|
end
|
2009-11-02 20:19:03 -05:00
|
|
|
|
2010-01-23 11:51:48 -05:00
|
|
|
attr_reader :name, :path
|
|
|
|
|
2010-01-24 03:47:55 -05:00
|
|
|
def load_tasks
|
|
|
|
super
|
2010-02-26 16:29:44 -05:00
|
|
|
load_deprecated_tasks
|
|
|
|
end
|
2010-01-24 03:47:55 -05:00
|
|
|
|
2010-02-26 16:29:44 -05:00
|
|
|
def load_deprecated_tasks
|
|
|
|
tasks = Dir["#{root}/{tasks,rails/tasks}/**/*.rake"].sort
|
|
|
|
if tasks.any?
|
|
|
|
ActiveSupport::Deprecation.warn "Rake tasks in #{tasks.to_sentence} are deprecated. Use lib/tasks instead"
|
|
|
|
tasks.each { |ext| load(ext) }
|
2010-01-24 03:47:55 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-23 11:51:48 -05:00
|
|
|
def initialize(root)
|
|
|
|
@name = File.basename(root).to_sym
|
|
|
|
config.root = root
|
2009-12-31 16:11:54 -05:00
|
|
|
end
|
2008-03-27 14:37:53 -04:00
|
|
|
|
2010-01-23 11:51:48 -05:00
|
|
|
def config
|
|
|
|
@config ||= Engine::Configuration.new
|
2009-12-31 16:11:54 -05:00
|
|
|
end
|
2008-03-27 14:37:53 -04:00
|
|
|
|
2010-04-05 04:52:47 -04:00
|
|
|
initializer :load_init_rb, :before => :load_config_initializers do |app|
|
2010-02-26 16:29:44 -05:00
|
|
|
files = %w(rails/init.rb init.rb).map { |path| File.expand_path path, root }
|
|
|
|
if initrb = files.find { |path| File.file? path }
|
|
|
|
if initrb == files.first
|
|
|
|
ActiveSupport::Deprecation.warn "Use toplevel init.rb; rails/init.rb is deprecated: #{initrb}"
|
|
|
|
end
|
|
|
|
config = app.config
|
|
|
|
eval(File.read(initrb), binding, initrb)
|
2010-02-16 18:15:21 -05:00
|
|
|
end
|
2008-03-27 14:37:53 -04:00
|
|
|
end
|
2010-01-24 03:00:18 -05:00
|
|
|
|
|
|
|
initializer :sanity_check_railties_collision do
|
|
|
|
if Engine.subclasses.map { |k| k.root.to_s }.include?(root.to_s)
|
2010-01-24 06:49:12 -05:00
|
|
|
raise "\"#{name}\" is a Railtie/Engine and cannot be installed as plugin"
|
2010-01-24 03:00:18 -05:00
|
|
|
end
|
|
|
|
end
|
2010-01-27 12:56:31 -05:00
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def reloadable?(app)
|
|
|
|
app.config.reload_plugins
|
|
|
|
end
|
2008-03-27 14:37:53 -04:00
|
|
|
end
|
2010-01-18 17:48:04 -05:00
|
|
|
end
|