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