To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / vendor / plugins / engines / lib / engines / plugin.rb @ 442:753f1380d6bc
History | View | Annotate | Download (3.25 KB)
| 1 |
# An instance of Plugin is created for each plugin loaded by Rails, and
|
|---|---|
| 2 |
# stored in the <tt>Engines.plugins</tt> PluginList
|
| 3 |
# (see Engines::RailsExtensions::RailsInitializer for more details).
|
| 4 |
#
|
| 5 |
# Engines.plugins[:plugin_name]
|
| 6 |
#
|
| 7 |
# Other properties of the Plugin instance can also be set.
|
| 8 |
module Engines |
| 9 |
class Plugin < Rails::Plugin |
| 10 |
# Plugins can add paths to this attribute in init.rb if they need
|
| 11 |
# controllers loaded from additional locations.
|
| 12 |
attr_accessor :controller_paths
|
| 13 |
|
| 14 |
# The directory in this plugin to mirror into the shared directory
|
| 15 |
# under +public+.
|
| 16 |
#
|
| 17 |
# Defaults to "assets" (see default_public_directory).
|
| 18 |
attr_accessor :public_directory
|
| 19 |
|
| 20 |
protected |
| 21 |
# The default set of code paths which will be added to the routing system
|
| 22 |
def default_controller_paths |
| 23 |
%w(app/controllers components)
|
| 24 |
end
|
| 25 |
|
| 26 |
# Attempts to detect the directory to use for public files.
|
| 27 |
# If +assets+ exists in the plugin, this will be used. If +assets+ is missing
|
| 28 |
# but +public+ is found, +public+ will be used.
|
| 29 |
def default_public_directory |
| 30 |
Engines.select_existing_paths(%w(assets public).map { |p| File.join(directory, p) }).first |
| 31 |
end
|
| 32 |
|
| 33 |
public |
| 34 |
|
| 35 |
def initialize(directory) |
| 36 |
super directory
|
| 37 |
@controller_paths = default_controller_paths
|
| 38 |
@public_directory = default_public_directory
|
| 39 |
end
|
| 40 |
|
| 41 |
# Extends the superclass' load method to additionally mirror public assets
|
| 42 |
def load(initializer) |
| 43 |
return if loaded? |
| 44 |
super initializer
|
| 45 |
add_plugin_locale_paths |
| 46 |
Assets.mirror_files_for(self) |
| 47 |
end
|
| 48 |
|
| 49 |
# select those paths that actually exist in the plugin's directory
|
| 50 |
def select_existing_paths(name) |
| 51 |
Engines.select_existing_paths(self.send(name).map { |p| File.join(directory, p) }) |
| 52 |
end
|
| 53 |
|
| 54 |
def add_plugin_locale_paths |
| 55 |
locale_path = File.join(directory, 'locales') |
| 56 |
return unless File.exists?(locale_path) |
| 57 |
|
| 58 |
locale_files = Dir[File.join(locale_path, '*.{rb,yml}')] |
| 59 |
return if locale_files.blank? |
| 60 |
|
| 61 |
first_app_element = |
| 62 |
I18n.load_path.select{ |e| e =~ /^#{ RAILS_ROOT }/ }.reject{ |e| e =~ /^#{ RAILS_ROOT }\/vendor\/plugins/ }.first |
| 63 |
app_index = I18n.load_path.index(first_app_element) || - 1 |
| 64 |
|
| 65 |
I18n.load_path.insert(app_index, *locale_files)
|
| 66 |
end
|
| 67 |
|
| 68 |
# The path to this plugin's public files
|
| 69 |
def public_asset_directory |
| 70 |
"#{File.basename(Engines.public_directory)}/#{name}"
|
| 71 |
end
|
| 72 |
|
| 73 |
# The directory containing this plugin's migrations (<tt>plugin/db/migrate</tt>)
|
| 74 |
def migration_directory |
| 75 |
File.join(self.directory, 'db', 'migrate') |
| 76 |
end
|
| 77 |
|
| 78 |
# Returns the version number of the latest migration for this plugin. Returns
|
| 79 |
# nil if this plugin has no migrations.
|
| 80 |
def latest_migration |
| 81 |
migrations.last |
| 82 |
end
|
| 83 |
|
| 84 |
# Returns the version numbers of all migrations for this plugin.
|
| 85 |
def migrations |
| 86 |
migrations = Dir[migration_directory+"/*.rb"] |
| 87 |
migrations.map { |p| File.basename(p).match(/0*(\d+)\_/)[1].to_i }.sort
|
| 88 |
end
|
| 89 |
|
| 90 |
# Migrate this plugin to the given version. See Engines::Plugin::Migrator for more
|
| 91 |
# information.
|
| 92 |
def migrate(version = nil) |
| 93 |
Engines::Plugin::Migrator.migrate_plugin(self, version) |
| 94 |
end
|
| 95 |
end
|
| 96 |
end
|
| 97 |
|