2
# The Plugin class should be an object which provides the following methods:
4
# * +name+ - Used during initialisation to order the plugin (based on name and
5
# the contents of <tt>config.plugins</tt>).
6
# * +valid?+ - Returns true if this plugin can be loaded.
7
# * +load_paths+ - Each path within the returned array will be added to the <tt>$LOAD_PATH</tt>.
8
# * +load+ - Finally 'load' the plugin.
10
# These methods are expected by the Rails::Plugin::Locator and Rails::Plugin::Loader classes.
11
# The default implementation returns the <tt>lib</tt> directory as its <tt>load_paths</tt>,
12
# and evaluates <tt>init.rb</tt> when <tt>load</tt> is called.
14
# You can also inspect the about.yml data programmatically:
16
# plugin = Rails::Plugin.new(path_to_my_plugin)
17
# plugin.about["author"] # => "James Adam"
18
# plugin.about["url"] # => "http://interblah.net"
22
attr_reader :directory, :name
24
def initialize(directory)
25
@directory = directory
26
@name = File.basename(@directory) rescue nil
31
File.directory?(directory) && (has_app_directory? || has_lib_directory? || has_init_file?)
34
# Returns a list of paths this plugin wishes to make available in <tt>$LOAD_PATH</tt>.
36
report_nonexistant_or_empty_plugin! unless valid?
38
returning [] do |load_paths|
39
load_paths << lib_path if has_lib_directory?
40
load_paths << app_paths if has_app_directory?
44
# Evaluates a plugin's init.rb file.
47
report_nonexistant_or_empty_plugin! unless valid?
48
evaluate_init_rb(initializer)
57
name <=> other_plugin.name
61
@about ||= load_about_information
64
# Engines are plugins with an app/ directory.
69
# Returns true if the engine ships with a routing file
71
File.exist?(routing_file)
74
# Returns true if there is any localization file in locale_path
80
File.join(directory, 'app', 'views')
84
File.join(directory, 'app', 'controllers')
88
File.join(directory, 'app', 'metal')
92
File.join(directory, 'config', 'routes.rb')
96
File.join(directory, 'config', 'locales')
100
Dir[ File.join(locale_path, '*.{rb,yml}') ]
105
def load_about_information
106
about_yml_path = File.join(@directory, "about.yml")
107
parsed_yml = File.exist?(about_yml_path) ? YAML.load(File.read(about_yml_path)) : {}
113
def report_nonexistant_or_empty_plugin!
114
raise LoadError, "Can not find the plugin named: #{name}"
119
[ File.join(directory, 'app', 'models'), File.join(directory, 'app', 'helpers'), controller_path, metal_path ]
123
File.join(directory, 'lib')
126
def classic_init_path
127
File.join(directory, 'init.rb')
131
File.join(directory, 'rails', 'init.rb')
135
File.file?(gem_init_path) ? gem_init_path : classic_init_path
139
def has_app_directory?
140
File.directory?(File.join(directory, 'app'))
143
def has_lib_directory?
144
File.directory?(lib_path)
148
File.file?(init_path)
152
def evaluate_init_rb(initializer)
155
# Allow plugins to reference the current configuration object
156
config = initializer.configuration
158
eval(IO.read(init_path), binding, init_path)
164
# This Plugin subclass represents a Gem plugin. Although RubyGems has already
165
# taken care of $LOAD_PATHs, it exposes its load_paths to add them
166
# to Dependencies.load_paths.
167
class GemPlugin < Plugin
168
# Initialize this plugin from a Gem::Specification.
169
def initialize(spec, gem)
170
directory = spec.full_gem_path
176
File.join(directory, 'rails', 'init.rb')