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