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-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-23 11:51:48 -05:00
|
|
|
class Plugin < Engine
|
2010-07-14 07:38:36 -04:00
|
|
|
def self.global_plugins
|
|
|
|
@global_plugins ||= []
|
|
|
|
end
|
|
|
|
|
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)
|
2010-07-14 07:38:36 -04:00
|
|
|
if global_plugins.include?(plugin.name)
|
|
|
|
warn "WARNING: plugin #{plugin.name} from #{path} was not loaded. Plugin with the same name has been already loaded."
|
|
|
|
next
|
|
|
|
end
|
|
|
|
global_plugins << plugin.name
|
2010-01-23 12:41:53 -05:00
|
|
|
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-07-26 11:05:04 -04:00
|
|
|
def railtie_name
|
|
|
|
name.to_s
|
|
|
|
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-06-27 18:57:47 -04:00
|
|
|
initializer :handle_lib_autoload, :before => :set_load_path do |app|
|
2010-10-06 11:18:59 -04:00
|
|
|
autoload = if app.config.reload_plugins
|
2010-06-27 18:57:47 -04:00
|
|
|
config.autoload_paths
|
|
|
|
else
|
|
|
|
config.autoload_once_paths
|
|
|
|
end
|
|
|
|
|
2010-10-06 11:18:59 -04:00
|
|
|
autoload.concat paths["lib"].existent
|
2010-06-27 18:57:47 -04:00
|
|
|
end
|
|
|
|
|
2010-04-05 04:52:47 -04:00
|
|
|
initializer :load_init_rb, :before => :load_config_initializers do |app|
|
2010-09-03 19:11:22 -04:00
|
|
|
init_rb = File.expand_path("init.rb", root)
|
|
|
|
if File.file?(init_rb)
|
2010-02-26 16:29:44 -05:00
|
|
|
config = app.config
|
2010-06-24 04:46:33 -04:00
|
|
|
# TODO: think about evaling initrb in context of Engine (currently it's
|
|
|
|
# always evaled in context of Rails::Application)
|
2010-09-03 19:11:22 -04:00
|
|
|
eval(File.read(init_rb), binding, init_rb)
|
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-27 09:12:02 -05:00
|
|
|
raise "\"#{name}\" is a Railtie/Engine and cannot be installed as a plugin"
|
2010-01-24 03:00:18 -05:00
|
|
|
end
|
|
|
|
end
|
2008-03-27 14:37:53 -04:00
|
|
|
end
|
2010-01-18 17:48:04 -05:00
|
|
|
end
|