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).
5 # Engines.plugins[:plugin_name]
7 # Other properties of the Plugin instance can also be set.
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
14 # The directory in this plugin to mirror into the shared directory
17 # Defaults to "assets" (see default_public_directory).
18 attr_accessor :public_directory
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)
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
35 def initialize(directory)
37 @controller_paths = default_controller_paths
38 @public_directory = default_public_directory
41 # Extends the superclass' load method to additionally mirror public assets
45 add_plugin_locale_paths
46 Assets.mirror_files_for(self)
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) })
54 def add_plugin_locale_paths
55 locale_path = File.join(directory, 'locales')
56 return unless File.exists?(locale_path)
58 locale_files = Dir[File.join(locale_path, '*.{rb,yml}')]
59 return if locale_files.blank?
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
65 I18n.load_path.insert(app_index, *locale_files)
68 # The path to this plugin's public files
69 def public_asset_directory
70 "#{File.basename(Engines.public_directory)}/#{name}"
73 # The directory containing this plugin's migrations (<tt>plugin/db/migrate</tt>)
74 def migration_directory
75 File.join(self.directory, 'db', 'migrate')
78 # Returns the version number of the latest migration for this plugin. Returns
79 # nil if this plugin has no migrations.
84 # Returns the version numbers of all migrations for this plugin.
86 migrations = Dir[migration_directory+"/*.rb"]
87 migrations.map { |p| File.basename(p).match(/0*(\d+)\_/)[1].to_i }.sort
90 # Migrate this plugin to the given version. See Engines::Plugin::Migrator for more
92 def migrate(version = nil)
93 Engines::Plugin::Migrator.migrate_plugin(self, version)