@@ -1,475 +1,481 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2014 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | module Redmine #:nodoc: |
|
19 | 19 | |
|
20 | 20 | class PluginNotFound < StandardError; end |
|
21 | 21 | class PluginRequirementError < StandardError; end |
|
22 | 22 | |
|
23 | 23 | # Base class for Redmine plugins. |
|
24 | 24 | # Plugins are registered using the <tt>register</tt> class method that acts as the public constructor. |
|
25 | 25 | # |
|
26 | 26 | # Redmine::Plugin.register :example do |
|
27 | 27 | # name 'Example plugin' |
|
28 | 28 | # author 'John Smith' |
|
29 | 29 | # description 'This is an example plugin for Redmine' |
|
30 | 30 | # version '0.0.1' |
|
31 | 31 | # settings :default => {'foo'=>'bar'}, :partial => 'settings/settings' |
|
32 | 32 | # end |
|
33 | 33 | # |
|
34 | 34 | # === Plugin attributes |
|
35 | 35 | # |
|
36 | 36 | # +settings+ is an optional attribute that let the plugin be configurable. |
|
37 | 37 | # It must be a hash with the following keys: |
|
38 | 38 | # * <tt>:default</tt>: default value for the plugin settings |
|
39 | 39 | # * <tt>:partial</tt>: path of the configuration partial view, relative to the plugin <tt>app/views</tt> directory |
|
40 | 40 | # Example: |
|
41 | 41 | # settings :default => {'foo'=>'bar'}, :partial => 'settings/settings' |
|
42 | 42 | # In this example, the settings partial will be found here in the plugin directory: <tt>app/views/settings/_settings.rhtml</tt>. |
|
43 | 43 | # |
|
44 | 44 | # When rendered, the plugin settings value is available as the local variable +settings+ |
|
45 | 45 | class Plugin |
|
46 | 46 | cattr_accessor :directory |
|
47 | 47 | self.directory = File.join(Rails.root, 'plugins') |
|
48 | 48 | |
|
49 | 49 | cattr_accessor :public_directory |
|
50 | 50 | self.public_directory = File.join(Rails.root, 'public', 'plugin_assets') |
|
51 | 51 | |
|
52 | 52 | @registered_plugins = {} |
|
53 | 53 | class << self |
|
54 | 54 | attr_reader :registered_plugins |
|
55 | 55 | private :new |
|
56 | 56 | |
|
57 | 57 | def def_field(*names) |
|
58 | 58 | class_eval do |
|
59 | 59 | names.each do |name| |
|
60 | 60 | define_method(name) do |*args| |
|
61 | 61 | args.empty? ? instance_variable_get("@#{name}") : instance_variable_set("@#{name}", *args) |
|
62 | 62 | end |
|
63 | 63 | end |
|
64 | 64 | end |
|
65 | 65 | end |
|
66 | 66 | end |
|
67 | 67 | def_field :name, :description, :url, :author, :author_url, :version, :settings, :directory |
|
68 | 68 | attr_reader :id |
|
69 | 69 | |
|
70 | 70 | # Plugin constructor |
|
71 | 71 | def self.register(id, &block) |
|
72 | 72 | p = new(id) |
|
73 | 73 | p.instance_eval(&block) |
|
74 | 74 | |
|
75 | 75 | # Set a default name if it was not provided during registration |
|
76 | 76 | p.name(id.to_s.humanize) if p.name.nil? |
|
77 | 77 | # Set a default directory if it was not provided during registration |
|
78 | 78 | p.directory(File.join(self.directory, id.to_s)) if p.directory.nil? |
|
79 | 79 | |
|
80 | 80 | # Adds plugin locales if any |
|
81 | 81 | # YAML translation files should be found under <plugin>/config/locales/ |
|
82 | 82 | Rails.application.config.i18n.load_path += Dir.glob(File.join(p.directory, 'config', 'locales', '*.yml')) |
|
83 | 83 | |
|
84 | 84 | # Prepends the app/views directory of the plugin to the view path |
|
85 | 85 | view_path = File.join(p.directory, 'app', 'views') |
|
86 | 86 | if File.directory?(view_path) |
|
87 | 87 | ActionController::Base.prepend_view_path(view_path) |
|
88 | 88 | ActionMailer::Base.prepend_view_path(view_path) |
|
89 | 89 | end |
|
90 | 90 | |
|
91 | 91 | # Adds the app/{controllers,helpers,models} directories of the plugin to the autoload path |
|
92 | 92 | Dir.glob File.expand_path(File.join(p.directory, 'app', '{controllers,helpers,models}')) do |dir| |
|
93 | 93 | ActiveSupport::Dependencies.autoload_paths += [dir] |
|
94 | 94 | end |
|
95 | 95 | |
|
96 | 96 | registered_plugins[id] = p |
|
97 | 97 | end |
|
98 | 98 | |
|
99 | 99 | # Returns an array of all registered plugins |
|
100 | 100 | def self.all |
|
101 | 101 | registered_plugins.values.sort |
|
102 | 102 | end |
|
103 | 103 | |
|
104 | 104 | # Finds a plugin by its id |
|
105 | 105 | # Returns a PluginNotFound exception if the plugin doesn't exist |
|
106 | 106 | def self.find(id) |
|
107 | 107 | registered_plugins[id.to_sym] || raise(PluginNotFound) |
|
108 | 108 | end |
|
109 | 109 | |
|
110 | 110 | # Clears the registered plugins hash |
|
111 | 111 | # It doesn't unload installed plugins |
|
112 | 112 | def self.clear |
|
113 | 113 | @registered_plugins = {} |
|
114 | 114 | end |
|
115 | 115 | |
|
116 | # Removes a plugin from the registered plugins | |
|
117 | # It doesn't unload the plugin | |
|
118 | def self.unregister(id) | |
|
119 | @registered_plugins.delete(id) | |
|
120 | end | |
|
121 | ||
|
116 | 122 | # Checks if a plugin is installed |
|
117 | 123 | # |
|
118 | 124 | # @param [String] id name of the plugin |
|
119 | 125 | def self.installed?(id) |
|
120 | 126 | registered_plugins[id.to_sym].present? |
|
121 | 127 | end |
|
122 | 128 | |
|
123 | 129 | def self.load |
|
124 | 130 | Dir.glob(File.join(self.directory, '*')).sort.each do |directory| |
|
125 | 131 | if File.directory?(directory) |
|
126 | 132 | lib = File.join(directory, "lib") |
|
127 | 133 | if File.directory?(lib) |
|
128 | 134 | $:.unshift lib |
|
129 | 135 | ActiveSupport::Dependencies.autoload_paths += [lib] |
|
130 | 136 | end |
|
131 | 137 | initializer = File.join(directory, "init.rb") |
|
132 | 138 | if File.file?(initializer) |
|
133 | 139 | require initializer |
|
134 | 140 | end |
|
135 | 141 | end |
|
136 | 142 | end |
|
137 | 143 | end |
|
138 | 144 | |
|
139 | 145 | def initialize(id) |
|
140 | 146 | @id = id.to_sym |
|
141 | 147 | end |
|
142 | 148 | |
|
143 | 149 | def public_directory |
|
144 | 150 | File.join(self.class.public_directory, id.to_s) |
|
145 | 151 | end |
|
146 | 152 | |
|
147 | 153 | def to_param |
|
148 | 154 | id |
|
149 | 155 | end |
|
150 | 156 | |
|
151 | 157 | def assets_directory |
|
152 | 158 | File.join(directory, 'assets') |
|
153 | 159 | end |
|
154 | 160 | |
|
155 | 161 | def <=>(plugin) |
|
156 | 162 | self.id.to_s <=> plugin.id.to_s |
|
157 | 163 | end |
|
158 | 164 | |
|
159 | 165 | # Sets a requirement on Redmine version |
|
160 | 166 | # Raises a PluginRequirementError exception if the requirement is not met |
|
161 | 167 | # |
|
162 | 168 | # Examples |
|
163 | 169 | # # Requires Redmine 0.7.3 or higher |
|
164 | 170 | # requires_redmine :version_or_higher => '0.7.3' |
|
165 | 171 | # requires_redmine '0.7.3' |
|
166 | 172 | # |
|
167 | 173 | # # Requires Redmine 0.7.x or higher |
|
168 | 174 | # requires_redmine '0.7' |
|
169 | 175 | # |
|
170 | 176 | # # Requires a specific Redmine version |
|
171 | 177 | # requires_redmine :version => '0.7.3' # 0.7.3 only |
|
172 | 178 | # requires_redmine :version => '0.7' # 0.7.x |
|
173 | 179 | # requires_redmine :version => ['0.7.3', '0.8.0'] # 0.7.3 or 0.8.0 |
|
174 | 180 | # |
|
175 | 181 | # # Requires a Redmine version within a range |
|
176 | 182 | # requires_redmine :version => '0.7.3'..'0.9.1' # >= 0.7.3 and <= 0.9.1 |
|
177 | 183 | # requires_redmine :version => '0.7'..'0.9' # >= 0.7.x and <= 0.9.x |
|
178 | 184 | def requires_redmine(arg) |
|
179 | 185 | arg = { :version_or_higher => arg } unless arg.is_a?(Hash) |
|
180 | 186 | arg.assert_valid_keys(:version, :version_or_higher) |
|
181 | 187 | |
|
182 | 188 | current = Redmine::VERSION.to_a |
|
183 | 189 | arg.each do |k, req| |
|
184 | 190 | case k |
|
185 | 191 | when :version_or_higher |
|
186 | 192 | raise ArgumentError.new(":version_or_higher accepts a version string only") unless req.is_a?(String) |
|
187 | 193 | unless compare_versions(req, current) <= 0 |
|
188 | 194 | raise PluginRequirementError.new("#{id} plugin requires Redmine #{req} or higher but current is #{current.join('.')}") |
|
189 | 195 | end |
|
190 | 196 | when :version |
|
191 | 197 | req = [req] if req.is_a?(String) |
|
192 | 198 | if req.is_a?(Array) |
|
193 | 199 | unless req.detect {|ver| compare_versions(ver, current) == 0} |
|
194 | 200 | raise PluginRequirementError.new("#{id} plugin requires one the following Redmine versions: #{req.join(', ')} but current is #{current.join('.')}") |
|
195 | 201 | end |
|
196 | 202 | elsif req.is_a?(Range) |
|
197 | 203 | unless compare_versions(req.first, current) <= 0 && compare_versions(req.last, current) >= 0 |
|
198 | 204 | raise PluginRequirementError.new("#{id} plugin requires a Redmine version between #{req.first} and #{req.last} but current is #{current.join('.')}") |
|
199 | 205 | end |
|
200 | 206 | else |
|
201 | 207 | raise ArgumentError.new(":version option accepts a version string, an array or a range of versions") |
|
202 | 208 | end |
|
203 | 209 | end |
|
204 | 210 | end |
|
205 | 211 | true |
|
206 | 212 | end |
|
207 | 213 | |
|
208 | 214 | def compare_versions(requirement, current) |
|
209 | 215 | requirement = requirement.split('.').collect(&:to_i) |
|
210 | 216 | requirement <=> current.slice(0, requirement.size) |
|
211 | 217 | end |
|
212 | 218 | private :compare_versions |
|
213 | 219 | |
|
214 | 220 | # Sets a requirement on a Redmine plugin version |
|
215 | 221 | # Raises a PluginRequirementError exception if the requirement is not met |
|
216 | 222 | # |
|
217 | 223 | # Examples |
|
218 | 224 | # # Requires a plugin named :foo version 0.7.3 or higher |
|
219 | 225 | # requires_redmine_plugin :foo, :version_or_higher => '0.7.3' |
|
220 | 226 | # requires_redmine_plugin :foo, '0.7.3' |
|
221 | 227 | # |
|
222 | 228 | # # Requires a specific version of a Redmine plugin |
|
223 | 229 | # requires_redmine_plugin :foo, :version => '0.7.3' # 0.7.3 only |
|
224 | 230 | # requires_redmine_plugin :foo, :version => ['0.7.3', '0.8.0'] # 0.7.3 or 0.8.0 |
|
225 | 231 | def requires_redmine_plugin(plugin_name, arg) |
|
226 | 232 | arg = { :version_or_higher => arg } unless arg.is_a?(Hash) |
|
227 | 233 | arg.assert_valid_keys(:version, :version_or_higher) |
|
228 | 234 | |
|
229 | 235 | plugin = Plugin.find(plugin_name) |
|
230 | 236 | current = plugin.version.split('.').collect(&:to_i) |
|
231 | 237 | |
|
232 | 238 | arg.each do |k, v| |
|
233 | 239 | v = [] << v unless v.is_a?(Array) |
|
234 | 240 | versions = v.collect {|s| s.split('.').collect(&:to_i)} |
|
235 | 241 | case k |
|
236 | 242 | when :version_or_higher |
|
237 | 243 | raise ArgumentError.new("wrong number of versions (#{versions.size} for 1)") unless versions.size == 1 |
|
238 | 244 | unless (current <=> versions.first) >= 0 |
|
239 | 245 | raise PluginRequirementError.new("#{id} plugin requires the #{plugin_name} plugin #{v} or higher but current is #{current.join('.')}") |
|
240 | 246 | end |
|
241 | 247 | when :version |
|
242 | 248 | unless versions.include?(current.slice(0,3)) |
|
243 | 249 | raise PluginRequirementError.new("#{id} plugin requires one the following versions of #{plugin_name}: #{v.join(', ')} but current is #{current.join('.')}") |
|
244 | 250 | end |
|
245 | 251 | end |
|
246 | 252 | end |
|
247 | 253 | true |
|
248 | 254 | end |
|
249 | 255 | |
|
250 | 256 | # Adds an item to the given +menu+. |
|
251 | 257 | # The +id+ parameter (equals to the project id) is automatically added to the url. |
|
252 | 258 | # menu :project_menu, :plugin_example, { :controller => 'example', :action => 'say_hello' }, :caption => 'Sample' |
|
253 | 259 | # |
|
254 | 260 | # +name+ parameter can be: :top_menu, :account_menu, :application_menu or :project_menu |
|
255 | 261 | # |
|
256 | 262 | def menu(menu, item, url, options={}) |
|
257 | 263 | Redmine::MenuManager.map(menu).push(item, url, options) |
|
258 | 264 | end |
|
259 | 265 | alias :add_menu_item :menu |
|
260 | 266 | |
|
261 | 267 | # Removes +item+ from the given +menu+. |
|
262 | 268 | def delete_menu_item(menu, item) |
|
263 | 269 | Redmine::MenuManager.map(menu).delete(item) |
|
264 | 270 | end |
|
265 | 271 | |
|
266 | 272 | # Defines a permission called +name+ for the given +actions+. |
|
267 | 273 | # |
|
268 | 274 | # The +actions+ argument is a hash with controllers as keys and actions as values (a single value or an array): |
|
269 | 275 | # permission :destroy_contacts, { :contacts => :destroy } |
|
270 | 276 | # permission :view_contacts, { :contacts => [:index, :show] } |
|
271 | 277 | # |
|
272 | 278 | # The +options+ argument is a hash that accept the following keys: |
|
273 | 279 | # * :public => the permission is public if set to true (implicitly given to any user) |
|
274 | 280 | # * :require => can be set to one of the following values to restrict users the permission can be given to: :loggedin, :member |
|
275 | 281 | # * :read => set it to true so that the permission is still granted on closed projects |
|
276 | 282 | # |
|
277 | 283 | # Examples |
|
278 | 284 | # # A permission that is implicitly given to any user |
|
279 | 285 | # # This permission won't appear on the Roles & Permissions setup screen |
|
280 | 286 | # permission :say_hello, { :example => :say_hello }, :public => true, :read => true |
|
281 | 287 | # |
|
282 | 288 | # # A permission that can be given to any user |
|
283 | 289 | # permission :say_hello, { :example => :say_hello } |
|
284 | 290 | # |
|
285 | 291 | # # A permission that can be given to registered users only |
|
286 | 292 | # permission :say_hello, { :example => :say_hello }, :require => :loggedin |
|
287 | 293 | # |
|
288 | 294 | # # A permission that can be given to project members only |
|
289 | 295 | # permission :say_hello, { :example => :say_hello }, :require => :member |
|
290 | 296 | def permission(name, actions, options = {}) |
|
291 | 297 | if @project_module |
|
292 | 298 | Redmine::AccessControl.map {|map| map.project_module(@project_module) {|map|map.permission(name, actions, options)}} |
|
293 | 299 | else |
|
294 | 300 | Redmine::AccessControl.map {|map| map.permission(name, actions, options)} |
|
295 | 301 | end |
|
296 | 302 | end |
|
297 | 303 | |
|
298 | 304 | # Defines a project module, that can be enabled/disabled for each project. |
|
299 | 305 | # Permissions defined inside +block+ will be bind to the module. |
|
300 | 306 | # |
|
301 | 307 | # project_module :things do |
|
302 | 308 | # permission :view_contacts, { :contacts => [:list, :show] }, :public => true |
|
303 | 309 | # permission :destroy_contacts, { :contacts => :destroy } |
|
304 | 310 | # end |
|
305 | 311 | def project_module(name, &block) |
|
306 | 312 | @project_module = name |
|
307 | 313 | self.instance_eval(&block) |
|
308 | 314 | @project_module = nil |
|
309 | 315 | end |
|
310 | 316 | |
|
311 | 317 | # Registers an activity provider. |
|
312 | 318 | # |
|
313 | 319 | # Options: |
|
314 | 320 | # * <tt>:class_name</tt> - one or more model(s) that provide these events (inferred from event_type by default) |
|
315 | 321 | # * <tt>:default</tt> - setting this option to false will make the events not displayed by default |
|
316 | 322 | # |
|
317 | 323 | # A model can provide several activity event types. |
|
318 | 324 | # |
|
319 | 325 | # Examples: |
|
320 | 326 | # register :news |
|
321 | 327 | # register :scrums, :class_name => 'Meeting' |
|
322 | 328 | # register :issues, :class_name => ['Issue', 'Journal'] |
|
323 | 329 | # |
|
324 | 330 | # Retrieving events: |
|
325 | 331 | # Associated model(s) must implement the find_events class method. |
|
326 | 332 | # ActiveRecord models can use acts_as_activity_provider as a way to implement this class method. |
|
327 | 333 | # |
|
328 | 334 | # The following call should return all the scrum events visible by current user that occured in the 5 last days: |
|
329 | 335 | # Meeting.find_events('scrums', User.current, 5.days.ago, Date.today) |
|
330 | 336 | # Meeting.find_events('scrums', User.current, 5.days.ago, Date.today, :project => foo) # events for project foo only |
|
331 | 337 | # |
|
332 | 338 | # Note that :view_scrums permission is required to view these events in the activity view. |
|
333 | 339 | def activity_provider(*args) |
|
334 | 340 | Redmine::Activity.register(*args) |
|
335 | 341 | end |
|
336 | 342 | |
|
337 | 343 | # Registers a wiki formatter. |
|
338 | 344 | # |
|
339 | 345 | # Parameters: |
|
340 | 346 | # * +name+ - human-readable name |
|
341 | 347 | # * +formatter+ - formatter class, which should have an instance method +to_html+ |
|
342 | 348 | # * +helper+ - helper module, which will be included by wiki pages |
|
343 | 349 | def wiki_format_provider(name, formatter, helper) |
|
344 | 350 | Redmine::WikiFormatting.register(name, formatter, helper) |
|
345 | 351 | end |
|
346 | 352 | |
|
347 | 353 | # Returns +true+ if the plugin can be configured. |
|
348 | 354 | def configurable? |
|
349 | 355 | settings && settings.is_a?(Hash) && !settings[:partial].blank? |
|
350 | 356 | end |
|
351 | 357 | |
|
352 | 358 | def mirror_assets |
|
353 | 359 | source = assets_directory |
|
354 | 360 | destination = public_directory |
|
355 | 361 | return unless File.directory?(source) |
|
356 | 362 | |
|
357 | 363 | source_files = Dir[source + "/**/*"] |
|
358 | 364 | source_dirs = source_files.select { |d| File.directory?(d) } |
|
359 | 365 | source_files -= source_dirs |
|
360 | 366 | |
|
361 | 367 | unless source_files.empty? |
|
362 | 368 | base_target_dir = File.join(destination, File.dirname(source_files.first).gsub(source, '')) |
|
363 | 369 | begin |
|
364 | 370 | FileUtils.mkdir_p(base_target_dir) |
|
365 | 371 | rescue Exception => e |
|
366 | 372 | raise "Could not create directory #{base_target_dir}: " + e.message |
|
367 | 373 | end |
|
368 | 374 | end |
|
369 | 375 | |
|
370 | 376 | source_dirs.each do |dir| |
|
371 | 377 | # strip down these paths so we have simple, relative paths we can |
|
372 | 378 | # add to the destination |
|
373 | 379 | target_dir = File.join(destination, dir.gsub(source, '')) |
|
374 | 380 | begin |
|
375 | 381 | FileUtils.mkdir_p(target_dir) |
|
376 | 382 | rescue Exception => e |
|
377 | 383 | raise "Could not create directory #{target_dir}: " + e.message |
|
378 | 384 | end |
|
379 | 385 | end |
|
380 | 386 | |
|
381 | 387 | source_files.each do |file| |
|
382 | 388 | begin |
|
383 | 389 | target = File.join(destination, file.gsub(source, '')) |
|
384 | 390 | unless File.exist?(target) && FileUtils.identical?(file, target) |
|
385 | 391 | FileUtils.cp(file, target) |
|
386 | 392 | end |
|
387 | 393 | rescue Exception => e |
|
388 | 394 | raise "Could not copy #{file} to #{target}: " + e.message |
|
389 | 395 | end |
|
390 | 396 | end |
|
391 | 397 | end |
|
392 | 398 | |
|
393 | 399 | # Mirrors assets from one or all plugins to public/plugin_assets |
|
394 | 400 | def self.mirror_assets(name=nil) |
|
395 | 401 | if name.present? |
|
396 | 402 | find(name).mirror_assets |
|
397 | 403 | else |
|
398 | 404 | all.each do |plugin| |
|
399 | 405 | plugin.mirror_assets |
|
400 | 406 | end |
|
401 | 407 | end |
|
402 | 408 | end |
|
403 | 409 | |
|
404 | 410 | # The directory containing this plugin's migrations (<tt>plugin/db/migrate</tt>) |
|
405 | 411 | def migration_directory |
|
406 | 412 | File.join(Rails.root, 'plugins', id.to_s, 'db', 'migrate') |
|
407 | 413 | end |
|
408 | 414 | |
|
409 | 415 | # Returns the version number of the latest migration for this plugin. Returns |
|
410 | 416 | # nil if this plugin has no migrations. |
|
411 | 417 | def latest_migration |
|
412 | 418 | migrations.last |
|
413 | 419 | end |
|
414 | 420 | |
|
415 | 421 | # Returns the version numbers of all migrations for this plugin. |
|
416 | 422 | def migrations |
|
417 | 423 | migrations = Dir[migration_directory+"/*.rb"] |
|
418 | 424 | migrations.map { |p| File.basename(p).match(/0*(\d+)\_/)[1].to_i }.sort |
|
419 | 425 | end |
|
420 | 426 | |
|
421 | 427 | # Migrate this plugin to the given version |
|
422 | 428 | def migrate(version = nil) |
|
423 | 429 | puts "Migrating #{id} (#{name})..." |
|
424 | 430 | Redmine::Plugin::Migrator.migrate_plugin(self, version) |
|
425 | 431 | end |
|
426 | 432 | |
|
427 | 433 | # Migrates all plugins or a single plugin to a given version |
|
428 | 434 | # Exemples: |
|
429 | 435 | # Plugin.migrate |
|
430 | 436 | # Plugin.migrate('sample_plugin') |
|
431 | 437 | # Plugin.migrate('sample_plugin', 1) |
|
432 | 438 | # |
|
433 | 439 | def self.migrate(name=nil, version=nil) |
|
434 | 440 | if name.present? |
|
435 | 441 | find(name).migrate(version) |
|
436 | 442 | else |
|
437 | 443 | all.each do |plugin| |
|
438 | 444 | plugin.migrate |
|
439 | 445 | end |
|
440 | 446 | end |
|
441 | 447 | end |
|
442 | 448 | |
|
443 | 449 | class Migrator < ActiveRecord::Migrator |
|
444 | 450 | # We need to be able to set the 'current' plugin being migrated. |
|
445 | 451 | cattr_accessor :current_plugin |
|
446 | 452 | |
|
447 | 453 | class << self |
|
448 | 454 | # Runs the migrations from a plugin, up (or down) to the version given |
|
449 | 455 | def migrate_plugin(plugin, version) |
|
450 | 456 | self.current_plugin = plugin |
|
451 | 457 | return if current_version(plugin) == version |
|
452 | 458 | migrate(plugin.migration_directory, version) |
|
453 | 459 | end |
|
454 | 460 | |
|
455 | 461 | def current_version(plugin=current_plugin) |
|
456 | 462 | # Delete migrations that don't match .. to_i will work because the number comes first |
|
457 | 463 | ::ActiveRecord::Base.connection.select_values( |
|
458 | 464 | "SELECT version FROM #{schema_migrations_table_name}" |
|
459 | 465 | ).delete_if{ |v| v.match(/-#{plugin.id}/) == nil }.map(&:to_i).max || 0 |
|
460 | 466 | end |
|
461 | 467 | end |
|
462 | 468 | |
|
463 | 469 | def migrated |
|
464 | 470 | sm_table = self.class.schema_migrations_table_name |
|
465 | 471 | ::ActiveRecord::Base.connection.select_values( |
|
466 | 472 | "SELECT version FROM #{sm_table}" |
|
467 | 473 | ).delete_if{ |v| v.match(/-#{current_plugin.id}/) == nil }.map(&:to_i).sort |
|
468 | 474 | end |
|
469 | 475 | |
|
470 | 476 | def record_version_state_after_migrating(version) |
|
471 | 477 | super(version.to_s + "-" + current_plugin.id.to_s) |
|
472 | 478 | end |
|
473 | 479 | end |
|
474 | 480 | end |
|
475 | 481 | end |
@@ -1,186 +1,189 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2014 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | require File.expand_path('../../test_helper', __FILE__) |
|
19 | 19 | |
|
20 | 20 | class SettingsControllerTest < ActionController::TestCase |
|
21 | 21 | fixtures :users |
|
22 | 22 | |
|
23 | 23 | def setup |
|
24 | 24 | User.current = nil |
|
25 | 25 | @request.session[:user_id] = 1 # admin |
|
26 | 26 | end |
|
27 | 27 | |
|
28 | 28 | def test_index |
|
29 | 29 | get :index |
|
30 | 30 | assert_response :success |
|
31 | 31 | assert_template 'edit' |
|
32 | 32 | end |
|
33 | 33 | |
|
34 | 34 | def test_get_edit |
|
35 | 35 | get :edit |
|
36 | 36 | assert_response :success |
|
37 | 37 | assert_template 'edit' |
|
38 | 38 | |
|
39 | 39 | assert_tag 'input', :attributes => {:name => 'settings[enabled_scm][]', :value => ''} |
|
40 | 40 | end |
|
41 | 41 | |
|
42 | 42 | def test_get_edit_should_preselect_default_issue_list_columns |
|
43 | 43 | with_settings :issue_list_default_columns => %w(tracker subject status updated_on) do |
|
44 | 44 | get :edit |
|
45 | 45 | assert_response :success |
|
46 | 46 | end |
|
47 | 47 | |
|
48 | 48 | assert_select 'select[id=selected_columns][name=?]', 'settings[issue_list_default_columns][]' do |
|
49 | 49 | assert_select 'option', 4 |
|
50 | 50 | assert_select 'option[value=tracker]', :text => 'Tracker' |
|
51 | 51 | assert_select 'option[value=subject]', :text => 'Subject' |
|
52 | 52 | assert_select 'option[value=status]', :text => 'Status' |
|
53 | 53 | assert_select 'option[value=updated_on]', :text => 'Updated' |
|
54 | 54 | end |
|
55 | 55 | |
|
56 | 56 | assert_select 'select[id=available_columns]' do |
|
57 | 57 | assert_select 'option[value=tracker]', 0 |
|
58 | 58 | assert_select 'option[value=priority]', :text => 'Priority' |
|
59 | 59 | end |
|
60 | 60 | end |
|
61 | 61 | |
|
62 | 62 | def test_get_edit_without_trackers_should_succeed |
|
63 | 63 | Tracker.delete_all |
|
64 | 64 | |
|
65 | 65 | get :edit |
|
66 | 66 | assert_response :success |
|
67 | 67 | end |
|
68 | 68 | |
|
69 | 69 | def test_post_edit_notifications |
|
70 | 70 | post :edit, :settings => {:mail_from => 'functional@test.foo', |
|
71 | 71 | :bcc_recipients => '0', |
|
72 | 72 | :notified_events => %w(issue_added issue_updated news_added), |
|
73 | 73 | :emails_footer => 'Test footer' |
|
74 | 74 | } |
|
75 | 75 | assert_redirected_to '/settings' |
|
76 | 76 | assert_equal 'functional@test.foo', Setting.mail_from |
|
77 | 77 | assert !Setting.bcc_recipients? |
|
78 | 78 | assert_equal %w(issue_added issue_updated news_added), Setting.notified_events |
|
79 | 79 | assert_equal 'Test footer', Setting.emails_footer |
|
80 | 80 | Setting.clear_cache |
|
81 | 81 | end |
|
82 | 82 | |
|
83 | 83 | def test_edit_commit_update_keywords |
|
84 | 84 | with_settings :commit_update_keywords => [ |
|
85 | 85 | {"keywords" => "fixes, resolves", "status_id" => "3"}, |
|
86 | 86 | {"keywords" => "closes", "status_id" => "5", "done_ratio" => "100", "if_tracker_id" => "2"} |
|
87 | 87 | ] do |
|
88 | 88 | get :edit |
|
89 | 89 | end |
|
90 | 90 | assert_response :success |
|
91 | 91 | assert_select 'tr.commit-keywords', 2 |
|
92 | 92 | assert_select 'tr.commit-keywords:nth-child(1)' do |
|
93 | 93 | assert_select 'input[name=?][value=?]', 'settings[commit_update_keywords][keywords][]', 'fixes, resolves' |
|
94 | 94 | assert_select 'select[name=?]', 'settings[commit_update_keywords][status_id][]' do |
|
95 | 95 | assert_select 'option[value=3][selected=selected]' |
|
96 | 96 | end |
|
97 | 97 | end |
|
98 | 98 | assert_select 'tr.commit-keywords:nth-child(2)' do |
|
99 | 99 | assert_select 'input[name=?][value=?]', 'settings[commit_update_keywords][keywords][]', 'closes' |
|
100 | 100 | assert_select 'select[name=?]', 'settings[commit_update_keywords][status_id][]' do |
|
101 | 101 | assert_select 'option[value=5][selected=selected]', :text => 'Closed' |
|
102 | 102 | end |
|
103 | 103 | assert_select 'select[name=?]', 'settings[commit_update_keywords][done_ratio][]' do |
|
104 | 104 | assert_select 'option[value=100][selected=selected]', :text => '100 %' |
|
105 | 105 | end |
|
106 | 106 | assert_select 'select[name=?]', 'settings[commit_update_keywords][if_tracker_id][]' do |
|
107 | 107 | assert_select 'option[value=2][selected=selected]', :text => 'Feature request' |
|
108 | 108 | end |
|
109 | 109 | end |
|
110 | 110 | end |
|
111 | 111 | |
|
112 | 112 | def test_edit_without_commit_update_keywords_should_show_blank_line |
|
113 | 113 | with_settings :commit_update_keywords => [] do |
|
114 | 114 | get :edit |
|
115 | 115 | end |
|
116 | 116 | assert_response :success |
|
117 | 117 | assert_select 'tr.commit-keywords', 1 do |
|
118 | 118 | assert_select 'input[name=?]:not([value])', 'settings[commit_update_keywords][keywords][]' |
|
119 | 119 | end |
|
120 | 120 | end |
|
121 | 121 | |
|
122 | 122 | def test_post_edit_commit_update_keywords |
|
123 | 123 | post :edit, :settings => { |
|
124 | 124 | :commit_update_keywords => { |
|
125 | 125 | :keywords => ["resolves", "closes"], |
|
126 | 126 | :status_id => ["3", "5"], |
|
127 | 127 | :done_ratio => ["", "100"], |
|
128 | 128 | :if_tracker_id => ["", "2"] |
|
129 | 129 | } |
|
130 | 130 | } |
|
131 | 131 | assert_redirected_to '/settings' |
|
132 | 132 | assert_equal([ |
|
133 | 133 | {"keywords" => "resolves", "status_id" => "3"}, |
|
134 | 134 | {"keywords" => "closes", "status_id" => "5", "done_ratio" => "100", "if_tracker_id" => "2"} |
|
135 | 135 | ], Setting.commit_update_keywords) |
|
136 | 136 | end |
|
137 | 137 | |
|
138 | 138 | def test_get_plugin_settings |
|
139 | 139 | Setting.stubs(:plugin_foo).returns({'sample_setting' => 'Plugin setting value'}) |
|
140 | 140 | ActionController::Base.append_view_path(File.join(Rails.root, "test/fixtures/plugins")) |
|
141 | 141 | Redmine::Plugin.register :foo do |
|
142 | 142 | settings :partial => "foo_plugin/foo_plugin_settings" |
|
143 | 143 | end |
|
144 | 144 | |
|
145 | 145 | get :plugin, :id => 'foo' |
|
146 | 146 | assert_response :success |
|
147 | 147 | assert_template 'plugin' |
|
148 | 148 | assert_tag 'form', :attributes => {:action => '/settings/plugin/foo'}, |
|
149 | 149 | :descendant => {:tag => 'input', :attributes => {:name => 'settings[sample_setting]', :value => 'Plugin setting value'}} |
|
150 | 150 | |
|
151 | Redmine::Plugin.clear | |
|
151 | ensure | |
|
152 | Redmine::Plugin.unregister(:foo) | |
|
152 | 153 | end |
|
153 | 154 | |
|
154 | 155 | def test_get_invalid_plugin_settings |
|
155 | 156 | get :plugin, :id => 'none' |
|
156 | 157 | assert_response 404 |
|
157 | 158 | end |
|
158 | 159 | |
|
159 | 160 | def test_get_non_configurable_plugin_settings |
|
160 | 161 | Redmine::Plugin.register(:foo) {} |
|
161 | 162 | |
|
162 | 163 | get :plugin, :id => 'foo' |
|
163 | 164 | assert_response 404 |
|
164 | 165 | |
|
165 | Redmine::Plugin.clear | |
|
166 | ensure | |
|
167 | Redmine::Plugin.unregister(:foo) | |
|
166 | 168 | end |
|
167 | 169 | |
|
168 | 170 | def test_post_plugin_settings |
|
169 | 171 | Setting.expects(:plugin_foo=).with({'sample_setting' => 'Value'}).returns(true) |
|
170 | 172 | Redmine::Plugin.register(:foo) do |
|
171 | 173 | settings :partial => 'not blank' # so that configurable? is true |
|
172 | 174 | end |
|
173 | 175 | |
|
174 | 176 | post :plugin, :id => 'foo', :settings => {'sample_setting' => 'Value'} |
|
175 | 177 | assert_redirected_to '/settings/plugin/foo' |
|
176 | 178 | end |
|
177 | 179 | |
|
178 | 180 | def test_post_non_configurable_plugin_settings |
|
179 | 181 | Redmine::Plugin.register(:foo) {} |
|
180 | 182 | |
|
181 | 183 | post :plugin, :id => 'foo', :settings => {'sample_setting' => 'Value'} |
|
182 | 184 | assert_response 404 |
|
183 | 185 | |
|
184 | Redmine::Plugin.clear | |
|
186 | ensure | |
|
187 | Redmine::Plugin.unregister(:foo) | |
|
185 | 188 | end |
|
186 | 189 | end |
General Comments 0
You need to be logged in to leave comments.
Login now