@@ -1,126 +1,126 | |||
|
1 | 1 | # An instance of Plugin is created for each plugin loaded by Rails, and |
|
2 | 2 | # stored in the <tt>Engines.plugins</tt> PluginList |
|
3 | 3 | # (see Engines::RailsExtensions::RailsInitializer for more details). |
|
4 | 4 | # |
|
5 | 5 | # Engines.plugins[:plugin_name] |
|
6 | 6 | # |
|
7 | 7 | # If this plugin contains paths in directories other than <tt>app/controllers</tt>, |
|
8 | 8 | # <tt>app/helpers</tt>, <tt>app/models</tt> and <tt>components</tt>, authors can |
|
9 | 9 | # declare this by adding extra paths to #code_paths: |
|
10 | 10 | # |
|
11 | 11 | # Rails.plugin[:my_plugin].code_paths << "app/sweepers" << "vendor/my_lib" |
|
12 | 12 | # |
|
13 | 13 | # Other properties of the Plugin instance can also be set. |
|
14 | 14 | module Engines |
|
15 | 15 | class Plugin < Rails::Plugin |
|
16 | 16 | # Plugins can add code paths to this attribute in init.rb if they |
|
17 | 17 | # need plugin directories to be added to the load path, i.e. |
|
18 | 18 | # |
|
19 | 19 | # plugin.code_paths << 'app/other_classes' |
|
20 | 20 | # |
|
21 | 21 | # Defaults to ["app/controllers", "app/helpers", "app/models", "components"] |
|
22 | 22 | attr_accessor :code_paths |
|
23 | 23 | |
|
24 | 24 | # Plugins can add paths to this attribute in init.rb if they need |
|
25 | 25 | # controllers loaded from additional locations. |
|
26 | 26 | attr_accessor :controller_paths |
|
27 | 27 | |
|
28 | 28 | # The directory in this plugin to mirror into the shared directory |
|
29 | 29 | # under +public+. |
|
30 | 30 | # |
|
31 | 31 | # Defaults to "assets" (see default_public_directory). |
|
32 | 32 | attr_accessor :public_directory |
|
33 | 33 | |
|
34 | 34 | protected |
|
35 | 35 | |
|
36 | 36 | # The default set of code paths which will be added to $LOAD_PATH |
|
37 | 37 | # and Dependencies.load_paths |
|
38 | 38 | def default_code_paths |
|
39 | 39 | # lib will actually be removed from the load paths when we call |
|
40 | 40 | # uniq! in #inject_into_load_paths, but it's important to keep it |
|
41 | 41 | # around (for the documentation tasks, for instance). |
|
42 | 42 | %w(app/controllers app/helpers app/models components lib) |
|
43 | 43 | end |
|
44 | 44 | |
|
45 | 45 | # The default set of code paths which will be added to the routing system |
|
46 | 46 | def default_controller_paths |
|
47 | 47 | %w(app/controllers components) |
|
48 | 48 | end |
|
49 | 49 | |
|
50 | 50 | # Attempts to detect the directory to use for public files. |
|
51 | 51 | # If +assets+ exists in the plugin, this will be used. If +assets+ is missing |
|
52 | 52 | # but +public+ is found, +public+ will be used. |
|
53 | 53 | def default_public_directory |
|
54 | 54 | Engines.select_existing_paths(%w(assets public).map { |p| File.join(directory, p) }).first |
|
55 | 55 | end |
|
56 | 56 | |
|
57 | 57 | public |
|
58 | 58 | |
|
59 | 59 | def initialize(directory) |
|
60 | 60 | super directory |
|
61 | 61 | @code_paths = default_code_paths |
|
62 | 62 | @controller_paths = default_controller_paths |
|
63 | 63 | @public_directory = default_public_directory |
|
64 | 64 | end |
|
65 | 65 | |
|
66 | 66 | # Returns a list of paths this plugin wishes to make available in $LOAD_PATH |
|
67 | 67 | # |
|
68 | 68 | # Overwrites the correspondend method in the superclass |
|
69 | 69 | def load_paths |
|
70 | 70 | report_nonexistant_or_empty_plugin! unless valid? |
|
71 | 71 | select_existing_paths :code_paths |
|
72 | 72 | end |
|
73 | 73 | |
|
74 | 74 | # Extends the superclass' load method to additionally mirror public assets |
|
75 | 75 | def load(initializer) |
|
76 | 76 | return if loaded? |
|
77 | 77 | super initializer |
|
78 | 78 | add_plugin_view_paths |
|
79 | 79 | Assets.mirror_files_for(self) |
|
80 | 80 | end |
|
81 | 81 | |
|
82 | 82 | # for code_paths and controller_paths select those paths that actually |
|
83 | 83 | # exist in the plugin's directory |
|
84 | 84 | def select_existing_paths(name) |
|
85 | 85 | Engines.select_existing_paths(self.send(name).map { |p| File.join(directory, p) }) |
|
86 | 86 | end |
|
87 | 87 | |
|
88 | 88 | def add_plugin_view_paths |
|
89 | 89 | view_path = File.join(directory, 'app', 'views') |
|
90 | 90 | if File.exist?(view_path) |
|
91 |
ActionController::Base.view_path |
|
|
91 | ActionController::Base.prepend_view_path(view_path) # push it just underneath the app | |
|
92 | 92 | ActionView::TemplateFinder.process_view_paths(view_path) |
|
93 | 93 | end |
|
94 | 94 | end |
|
95 | 95 | |
|
96 | 96 | # The path to this plugin's public files |
|
97 | 97 | def public_asset_directory |
|
98 | 98 | "#{File.basename(Engines.public_directory)}/#{name}" |
|
99 | 99 | end |
|
100 | 100 | |
|
101 | 101 | # The path to this plugin's routes file |
|
102 | 102 | def routes_path |
|
103 | 103 | File.join(directory, "routes.rb") |
|
104 | 104 | end |
|
105 | 105 | |
|
106 | 106 | # The directory containing this plugin's migrations (<tt>plugin/db/migrate</tt>) |
|
107 | 107 | def migration_directory |
|
108 | 108 | File.join(self.directory, 'db', 'migrate') |
|
109 | 109 | end |
|
110 | 110 | |
|
111 | 111 | # Returns the version number of the latest migration for this plugin. Returns |
|
112 | 112 | # nil if this plugin has no migrations. |
|
113 | 113 | def latest_migration |
|
114 | 114 | migrations = Dir[migration_directory+"/*.rb"] |
|
115 | 115 | return nil if migrations.empty? |
|
116 | 116 | migrations.map { |p| File.basename(p) }.sort.last.match(/0*(\d+)\_/)[1].to_i |
|
117 | 117 | end |
|
118 | 118 | |
|
119 | 119 | # Migrate this plugin to the given version. See Engines::Plugin::Migrator for more |
|
120 | 120 | # information. |
|
121 | 121 | def migrate(version = nil) |
|
122 | 122 | Engines::Plugin::Migrator.migrate_plugin(self, version) |
|
123 | 123 | end |
|
124 | 124 | end |
|
125 | 125 | end |
|
126 | 126 |
General Comments 0
You need to be logged in to leave comments.
Login now