rails--rails/railties/lib/rails/plugin.rb

65 lines
1.6 KiB
Ruby
Raw Normal View History

2010-01-23 21:30:17 +00:00
require 'rails/engine'
module Rails
2010-01-23 16:51:48 +00:00
class Plugin < Engine
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
2010-01-23 16:51:48 +00:00
end
end
plugins.sort_by do |p|
[list.index(p.name) || list.index(:all), p.name.to_s]
end
end
2010-01-23 16:51:48 +00:00
attr_reader :name, :path
2010-01-24 08:47:55 +00:00
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
2010-01-23 16:51:48 +00:00
def initialize(root)
@name = File.basename(root).to_sym
config.root = root
end
2010-01-23 16:51:48 +00:00
def config
@config ||= Engine::Configuration.new
end
initializer :load_init_rb, :before => :load_application_initializers do |app|
2010-01-23 16:51:48 +00:00
file = Dir["#{root}/{rails/init,init}.rb"].first
config = app.config
eval(File.read(file), binding, file) if file && File.file?(file)
end
2010-01-24 08:00:18 +00:00
initializer :sanity_check_railties_collision do
if Engine.subclasses.map { |k| k.root.to_s }.include?(root.to_s)
2010-01-24 11:49:12 +00:00
raise "\"#{name}\" is a Railtie/Engine and cannot be installed as plugin"
2010-01-24 08:00:18 +00:00
end
end
protected
def reloadable?(app)
app.config.reload_plugins
end
end
end