| 
									
										
										
										
											2019-03-16 09:37:35 +00:00
										 |  |  | # frozen_string_literal: true | 
					
						
							| 
									
										
										
										
											2019-03-15 01:32:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-17 01:19:27 +00:00
										 |  |  | # Redmine - project management software | 
					
						
							| 
									
										
										
										
											2021-03-25 06:58:56 +00:00
										 |  |  | # Copyright (C) 2006-2021  Jean-Philippe Lang | 
					
						
							| 
									
										
										
										
											2007-09-23 17:19:27 +00:00
										 |  |  | # | 
					
						
							|  |  |  | # This program is free software; you can redistribute it and/or | 
					
						
							|  |  |  | # modify it under the terms of the GNU General Public License | 
					
						
							|  |  |  | # as published by the Free Software Foundation; either version 2 | 
					
						
							|  |  |  | # of the License, or (at your option) any later version. | 
					
						
							| 
									
										
										
										
											2011-06-17 01:19:27 +00:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											2007-09-23 17:19:27 +00:00
										 |  |  | # This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | # but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  | # GNU General Public License for more details. | 
					
						
							| 
									
										
										
										
											2011-06-17 01:19:27 +00:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											2007-09-23 17:19:27 +00:00
										 |  |  | # You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  | # along with this program; if not, write to the Free Software | 
					
						
							|  |  |  | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-08 00:55:41 +00:00
										 |  |  | module Redmine | 
					
						
							| 
									
										
										
										
											2007-09-23 17:19:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-29 17:19:08 +00:00
										 |  |  |   # Exception raised when a plugin cannot be found given its id. | 
					
						
							| 
									
										
										
										
											2008-11-16 15:38:37 +00:00
										 |  |  |   class PluginNotFound < StandardError; end | 
					
						
							| 
									
										
										
										
											2018-11-29 17:19:08 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   # Exception raised when a plugin requirement is not met. | 
					
						
							| 
									
										
										
										
											2008-11-16 20:00:20 +00:00
										 |  |  |   class PluginRequirementError < StandardError; end | 
					
						
							| 
									
										
										
										
											2011-09-20 02:43:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-23 17:19:27 +00:00
										 |  |  |   # Base class for Redmine plugins. | 
					
						
							|  |  |  |   # Plugins are registered using the <tt>register</tt> class method that acts as the public constructor. | 
					
						
							| 
									
										
										
										
											2011-09-20 02:43:47 +00:00
										 |  |  |   # | 
					
						
							| 
									
										
										
										
											2007-09-23 17:19:27 +00:00
										 |  |  |   #   Redmine::Plugin.register :example do | 
					
						
							|  |  |  |   #     name 'Example plugin' | 
					
						
							|  |  |  |   #     author 'John Smith' | 
					
						
							|  |  |  |   #     description 'This is an example plugin for Redmine' | 
					
						
							|  |  |  |   #     version '0.0.1' | 
					
						
							|  |  |  |   #     settings :default => {'foo'=>'bar'}, :partial => 'settings/settings' | 
					
						
							|  |  |  |   #   end | 
					
						
							| 
									
										
										
										
											2011-09-20 02:43:47 +00:00
										 |  |  |   # | 
					
						
							| 
									
										
										
										
											2007-09-23 17:19:27 +00:00
										 |  |  |   # === Plugin attributes | 
					
						
							| 
									
										
										
										
											2011-09-20 02:43:47 +00:00
										 |  |  |   # | 
					
						
							| 
									
										
										
										
											2007-09-23 17:19:27 +00:00
										 |  |  |   # +settings+ is an optional attribute that let the plugin be configurable. | 
					
						
							|  |  |  |   # It must be a hash with the following keys: | 
					
						
							|  |  |  |   # * <tt>:default</tt>: default value for the plugin settings | 
					
						
							|  |  |  |   # * <tt>:partial</tt>: path of the configuration partial view, relative to the plugin <tt>app/views</tt> directory | 
					
						
							|  |  |  |   # Example: | 
					
						
							|  |  |  |   #   settings :default => {'foo'=>'bar'}, :partial => 'settings/settings' | 
					
						
							|  |  |  |   # In this example, the settings partial will be found here in the plugin directory: <tt>app/views/settings/_settings.rhtml</tt>. | 
					
						
							| 
									
										
										
										
											2011-09-20 02:43:47 +00:00
										 |  |  |   # | 
					
						
							| 
									
										
										
										
											2007-09-23 17:19:27 +00:00
										 |  |  |   # When rendered, the plugin settings value is available as the local variable +settings+ | 
					
						
							| 
									
										
										
										
											2018-11-29 17:19:08 +00:00
										 |  |  |   # | 
					
						
							|  |  |  |   # See: http://www.redmine.org/projects/redmine/wiki/Plugin_Tutorial | 
					
						
							| 
									
										
										
										
											2007-09-23 17:19:27 +00:00
										 |  |  |   class Plugin | 
					
						
							| 
									
										
										
										
											2018-11-29 17:19:08 +00:00
										 |  |  |     # Absolute path to the directory where plugins are located | 
					
						
							| 
									
										
										
										
											2012-04-25 17:17:49 +00:00
										 |  |  |     cattr_accessor :directory | 
					
						
							| 
									
										
										
										
											2017-06-17 10:40:51 +00:00
										 |  |  |     self.directory = File.join(Rails.root, 'plugins') | 
					
						
							| 
									
										
										
										
											2012-04-25 17:17:49 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-29 17:19:08 +00:00
										 |  |  |     # Absolute path to the plublic directory where plugins assets are copied | 
					
						
							| 
									
										
										
										
											2012-03-04 10:29:47 +00:00
										 |  |  |     cattr_accessor :public_directory | 
					
						
							|  |  |  |     self.public_directory = File.join(Rails.root, 'public', 'plugin_assets') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-23 17:19:27 +00:00
										 |  |  |     @registered_plugins = {} | 
					
						
							| 
									
										
										
										
											2014-08-13 04:56:44 +00:00
										 |  |  |     @used_partials = {} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-23 17:19:27 +00:00
										 |  |  |     class << self | 
					
						
							|  |  |  |       attr_reader :registered_plugins | 
					
						
							|  |  |  |       private :new | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def def_field(*names) | 
					
						
							| 
									
										
										
										
											2011-09-20 02:43:47 +00:00
										 |  |  |         class_eval do | 
					
						
							| 
									
										
										
										
											2007-09-23 17:19:27 +00:00
										 |  |  |           names.each do |name| | 
					
						
							| 
									
										
										
										
											2011-09-20 02:43:47 +00:00
										 |  |  |             define_method(name) do |*args| | 
					
						
							| 
									
										
										
										
											2007-09-23 17:19:27 +00:00
										 |  |  |               args.empty? ? instance_variable_get("@#{name}") : instance_variable_set("@#{name}", *args) | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2013-05-02 17:59:34 +00:00
										 |  |  |     def_field :name, :description, :url, :author, :author_url, :version, :settings, :directory | 
					
						
							| 
									
										
										
										
											2008-11-16 15:22:48 +00:00
										 |  |  |     attr_reader :id | 
					
						
							| 
									
										
										
										
											2011-09-20 02:43:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-29 17:19:08 +00:00
										 |  |  |     # Plugin constructor: instanciates a new Redmine::Plugin with given +id+ | 
					
						
							|  |  |  |     # and make it evaluate the given +block+ | 
					
						
							|  |  |  |     # | 
					
						
							|  |  |  |     # Example | 
					
						
							|  |  |  |     #   Redmine::Plugin.register :example do | 
					
						
							|  |  |  |     #     name 'Example plugin' | 
					
						
							|  |  |  |     #     author 'John Smith' | 
					
						
							|  |  |  |     #     description 'This is an example plugin for Redmine' | 
					
						
							|  |  |  |     #     version '0.0.1' | 
					
						
							|  |  |  |     #     requires_redmine version_or_higher: '3.0.0' | 
					
						
							|  |  |  |     #   end | 
					
						
							| 
									
										
										
										
											2008-11-16 15:22:48 +00:00
										 |  |  |     def self.register(id, &block) | 
					
						
							|  |  |  |       p = new(id) | 
					
						
							| 
									
										
										
										
											2007-09-23 17:19:27 +00:00
										 |  |  |       p.instance_eval(&block) | 
					
						
							| 
									
										
										
										
											2013-05-02 17:59:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-16 16:08:25 +00:00
										 |  |  |       # Set a default name if it was not provided during registration | 
					
						
							|  |  |  |       p.name(id.to_s.humanize) if p.name.nil? | 
					
						
							| 
									
										
										
										
											2013-05-02 17:59:34 +00:00
										 |  |  |       # Set a default directory if it was not provided during registration | 
					
						
							|  |  |  |       p.directory(File.join(self.directory, id.to_s)) if p.directory.nil? | 
					
						
							| 
									
										
										
										
											2012-04-25 17:17:49 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-19 15:30:41 +00:00
										 |  |  |       unless File.directory?(p.directory) | 
					
						
							|  |  |  |         raise PluginNotFound, "Plugin not found. The directory for plugin #{p.id} should be #{p.directory}." | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-21 11:04:50 +00:00
										 |  |  |       # Adds plugin locales if any | 
					
						
							|  |  |  |       # YAML translation files should be found under <plugin>/config/locales/ | 
					
						
							| 
									
										
										
										
											2014-01-05 14:46:22 +00:00
										 |  |  |       Rails.application.config.i18n.load_path += Dir.glob(File.join(p.directory, 'config', 'locales', '*.yml')) | 
					
						
							| 
									
										
										
										
											2012-04-25 17:17:49 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |       # Prepends the app/views directory of the plugin to the view path | 
					
						
							|  |  |  |       view_path = File.join(p.directory, 'app', 'views') | 
					
						
							|  |  |  |       if File.directory?(view_path) | 
					
						
							|  |  |  |         ActionController::Base.prepend_view_path(view_path) | 
					
						
							| 
									
										
										
										
											2012-09-06 17:34:14 +00:00
										 |  |  |         ActionMailer::Base.prepend_view_path(view_path) | 
					
						
							| 
									
										
										
										
											2012-04-25 17:17:49 +00:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-04 11:33:21 +00:00
										 |  |  |       # Add the plugin directories to rails autoload paths | 
					
						
							|  |  |  |       engine_cfg = Rails::Engine::Configuration.new(p.directory) | 
					
						
							| 
									
										
										
										
											2019-03-15 07:58:07 +00:00
										 |  |  |       engine_cfg.paths.add 'lib', eager_load: true | 
					
						
							| 
									
										
										
										
											2019-03-04 11:33:21 +00:00
										 |  |  |       Rails.application.config.eager_load_paths += engine_cfg.eager_load_paths | 
					
						
							|  |  |  |       Rails.application.config.autoload_once_paths += engine_cfg.autoload_once_paths | 
					
						
							|  |  |  |       Rails.application.config.autoload_paths += engine_cfg.autoload_paths | 
					
						
							| 
									
										
										
										
											2020-12-09 14:11:45 +00:00
										 |  |  |       ActiveSupport::Dependencies.autoload_paths += | 
					
						
							|  |  |  |         engine_cfg.eager_load_paths + engine_cfg.autoload_once_paths + engine_cfg.autoload_paths | 
					
						
							| 
									
										
										
										
											2012-04-25 17:17:49 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-05 15:00:27 +00:00
										 |  |  |       # Defines plugin setting if present | 
					
						
							|  |  |  |       if p.settings | 
					
						
							|  |  |  |         Setting.define_plugin_setting p | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-13 04:56:44 +00:00
										 |  |  |       # Warn for potential settings[:partial] collisions | 
					
						
							|  |  |  |       if p.configurable? | 
					
						
							|  |  |  |         partial = p.settings[:partial] | 
					
						
							|  |  |  |         if @used_partials[partial] | 
					
						
							| 
									
										
										
										
											2020-12-09 14:11:45 +00:00
										 |  |  |           Rails.logger.warn( | 
					
						
							|  |  |  |             "WARNING: settings partial '#{partial}' is declared in '#{p.id}' plugin " \ | 
					
						
							|  |  |  |               "but it is already used by plugin '#{@used_partials[partial]}'. " \ | 
					
						
							|  |  |  |               "Only one settings view will be used. " \ | 
					
						
							|  |  |  |               "You may want to contact those plugins authors to fix this." | 
					
						
							|  |  |  |           ) | 
					
						
							| 
									
										
										
										
											2014-08-13 04:56:44 +00:00
										 |  |  |         end | 
					
						
							|  |  |  |         @used_partials[partial] = p.id | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-16 15:38:37 +00:00
										 |  |  |       registered_plugins[id] = p | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2011-08-11 02:40:08 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Returns an array of all registered plugins | 
					
						
							| 
									
										
										
										
											2008-11-16 15:38:37 +00:00
										 |  |  |     def self.all | 
					
						
							|  |  |  |       registered_plugins.values.sort | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2011-08-11 02:40:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-16 15:38:37 +00:00
										 |  |  |     # Finds a plugin by its id | 
					
						
							| 
									
										
										
										
											2008-11-16 16:08:25 +00:00
										 |  |  |     # Returns a PluginNotFound exception if the plugin doesn't exist | 
					
						
							| 
									
										
										
										
											2008-11-16 15:38:37 +00:00
										 |  |  |     def self.find(id) | 
					
						
							|  |  |  |       registered_plugins[id.to_sym] || raise(PluginNotFound) | 
					
						
							| 
									
										
										
										
											2008-11-16 15:22:48 +00:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2011-09-20 02:43:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-16 16:08:25 +00:00
										 |  |  |     # Clears the registered plugins hash | 
					
						
							|  |  |  |     # It doesn't unload installed plugins | 
					
						
							|  |  |  |     def self.clear | 
					
						
							|  |  |  |       @registered_plugins = {} | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2010-10-25 23:32:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-21 01:12:11 +00:00
										 |  |  |     # Removes a plugin from the registered plugins | 
					
						
							|  |  |  |     # It doesn't unload the plugin | 
					
						
							|  |  |  |     def self.unregister(id) | 
					
						
							|  |  |  |       @registered_plugins.delete(id) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-25 23:32:01 +00:00
										 |  |  |     # Checks if a plugin is installed | 
					
						
							|  |  |  |     # | 
					
						
							|  |  |  |     # @param [String] id name of the plugin | 
					
						
							|  |  |  |     def self.installed?(id) | 
					
						
							|  |  |  |       registered_plugins[id.to_sym].present? | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2011-09-20 02:43:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-25 17:17:49 +00:00
										 |  |  |     def self.load | 
					
						
							|  |  |  |       Dir.glob(File.join(self.directory, '*')).sort.each do |directory| | 
					
						
							|  |  |  |         if File.directory?(directory) | 
					
						
							|  |  |  |           lib = File.join(directory, "lib") | 
					
						
							|  |  |  |           if File.directory?(lib) | 
					
						
							|  |  |  |             $:.unshift lib | 
					
						
							|  |  |  |             ActiveSupport::Dependencies.autoload_paths += [lib] | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |           initializer = File.join(directory, "init.rb") | 
					
						
							|  |  |  |           if File.file?(initializer) | 
					
						
							|  |  |  |             require initializer | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2020-10-20 00:02:10 +00:00
										 |  |  |       Redmine::Hook.call_hook :after_plugins_loaded | 
					
						
							| 
									
										
										
										
											2012-04-25 17:17:49 +00:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-16 15:22:48 +00:00
										 |  |  |     def initialize(id) | 
					
						
							|  |  |  |       @id = id.to_sym | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2011-09-20 02:43:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-25 17:17:49 +00:00
										 |  |  |     def public_directory | 
					
						
							|  |  |  |       File.join(self.class.public_directory, id.to_s) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-12-13 13:52:56 +00:00
										 |  |  |     def to_param | 
					
						
							|  |  |  |       id | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-29 17:19:08 +00:00
										 |  |  |     # Returns the absolute path to the plugin assets directory | 
					
						
							| 
									
										
										
										
											2012-04-25 17:17:49 +00:00
										 |  |  |     def assets_directory | 
					
						
							|  |  |  |       File.join(directory, 'assets') | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-16 15:22:48 +00:00
										 |  |  |     def <=>(plugin) | 
					
						
							|  |  |  |       self.id.to_s <=> plugin.id.to_s | 
					
						
							| 
									
										
										
										
											2007-09-23 17:19:27 +00:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2011-09-20 02:43:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-16 20:00:20 +00:00
										 |  |  |     # Sets a requirement on Redmine version | 
					
						
							|  |  |  |     # Raises a PluginRequirementError exception if the requirement is not met | 
					
						
							|  |  |  |     # | 
					
						
							|  |  |  |     # Examples | 
					
						
							|  |  |  |     #   # Requires Redmine 0.7.3 or higher | 
					
						
							|  |  |  |     #   requires_redmine :version_or_higher => '0.7.3' | 
					
						
							|  |  |  |     #   requires_redmine '0.7.3' | 
					
						
							| 
									
										
										
										
											2012-11-17 11:05:27 +00:00
										 |  |  |     # | 
					
						
							|  |  |  |     #   # Requires Redmine 0.7.x or higher | 
					
						
							| 
									
										
										
										
											2012-11-17 10:55:38 +00:00
										 |  |  |     #   requires_redmine '0.7' | 
					
						
							| 
									
										
										
										
											2008-11-16 20:00:20 +00:00
										 |  |  |     # | 
					
						
							|  |  |  |     #   # Requires a specific Redmine version | 
					
						
							|  |  |  |     #   requires_redmine :version => '0.7.3'              # 0.7.3 only | 
					
						
							| 
									
										
										
										
											2012-11-17 10:55:38 +00:00
										 |  |  |     #   requires_redmine :version => '0.7'                # 0.7.x | 
					
						
							| 
									
										
										
										
											2008-11-16 20:00:20 +00:00
										 |  |  |     #   requires_redmine :version => ['0.7.3', '0.8.0']   # 0.7.3 or 0.8.0 | 
					
						
							| 
									
										
										
										
											2012-11-17 10:55:38 +00:00
										 |  |  |     # | 
					
						
							|  |  |  |     #   # Requires a Redmine version within a range | 
					
						
							|  |  |  |     #   requires_redmine :version => '0.7.3'..'0.9.1'     # >= 0.7.3 and <= 0.9.1 | 
					
						
							|  |  |  |     #   requires_redmine :version => '0.7'..'0.9'         # >= 0.7.x and <= 0.9.x | 
					
						
							| 
									
										
										
										
											2008-11-16 20:00:20 +00:00
										 |  |  |     def requires_redmine(arg) | 
					
						
							| 
									
										
										
										
											2020-11-20 11:36:29 +00:00
										 |  |  |       arg = {:version_or_higher => arg} unless arg.is_a?(Hash) | 
					
						
							| 
									
										
										
										
											2008-11-16 20:00:20 +00:00
										 |  |  |       arg.assert_valid_keys(:version, :version_or_higher) | 
					
						
							| 
									
										
										
										
											2011-09-20 02:43:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-16 20:00:20 +00:00
										 |  |  |       current = Redmine::VERSION.to_a | 
					
						
							| 
									
										
										
										
											2012-11-17 10:55:38 +00:00
										 |  |  |       arg.each do |k, req| | 
					
						
							| 
									
										
										
										
											2008-11-16 20:00:20 +00:00
										 |  |  |         case k | 
					
						
							|  |  |  |         when :version_or_higher | 
					
						
							| 
									
										
										
										
											2020-12-09 14:11:45 +00:00
										 |  |  |           unless req.is_a?(String) | 
					
						
							|  |  |  |             raise ArgumentError.new(":version_or_higher accepts a version string only") | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-11-17 10:55:38 +00:00
										 |  |  |           unless compare_versions(req, current) <= 0
 | 
					
						
							| 
									
										
										
										
											2020-12-09 14:11:45 +00:00
										 |  |  |             raise PluginRequirementError.new( | 
					
						
							|  |  |  |               "#{id} plugin requires Redmine #{req} or higher " \ | 
					
						
							|  |  |  |               "but current is #{current.join('.')}" | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2008-11-16 20:00:20 +00:00
										 |  |  |           end | 
					
						
							|  |  |  |         when :version | 
					
						
							| 
									
										
										
										
											2012-11-17 10:55:38 +00:00
										 |  |  |           req = [req] if req.is_a?(String) | 
					
						
							|  |  |  |           if req.is_a?(Array) | 
					
						
							|  |  |  |             unless req.detect {|ver| compare_versions(ver, current) == 0} | 
					
						
							| 
									
										
										
										
											2020-12-09 14:11:45 +00:00
										 |  |  |               raise PluginRequirementError.new( | 
					
						
							|  |  |  |                 "#{id} plugin requires one the following Redmine versions: " \ | 
					
						
							|  |  |  |                 "#{req.join(', ')} but current is #{current.join('.')}" | 
					
						
							|  |  |  |               ) | 
					
						
							| 
									
										
										
										
											2012-11-17 10:55:38 +00:00
										 |  |  |             end | 
					
						
							|  |  |  |           elsif req.is_a?(Range) | 
					
						
							|  |  |  |             unless compare_versions(req.first, current) <= 0 && compare_versions(req.last, current) >= 0
 | 
					
						
							| 
									
										
										
										
											2020-12-09 14:11:45 +00:00
										 |  |  |               raise PluginRequirementError.new( | 
					
						
							|  |  |  |                 "#{id} plugin requires a Redmine version between #{req.first} " \ | 
					
						
							|  |  |  |                 "and #{req.last} but current is #{current.join('.')}" | 
					
						
							|  |  |  |               ) | 
					
						
							| 
									
										
										
										
											2012-11-17 10:55:38 +00:00
										 |  |  |             end | 
					
						
							|  |  |  |           else | 
					
						
							|  |  |  |             raise ArgumentError.new(":version option accepts a version string, an array or a range of versions") | 
					
						
							| 
									
										
										
										
											2008-11-16 20:00:20 +00:00
										 |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |       true | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2007-09-23 17:19:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-11-17 10:55:38 +00:00
										 |  |  |     def compare_versions(requirement, current) | 
					
						
							|  |  |  |       requirement = requirement.split('.').collect(&:to_i) | 
					
						
							|  |  |  |       requirement <=> current.slice(0, requirement.size) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |     private :compare_versions | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-16 02:07:46 +00:00
										 |  |  |     # Sets a requirement on a Redmine plugin version | 
					
						
							|  |  |  |     # Raises a PluginRequirementError exception if the requirement is not met | 
					
						
							|  |  |  |     # | 
					
						
							|  |  |  |     # Examples | 
					
						
							|  |  |  |     #   # Requires a plugin named :foo version 0.7.3 or higher | 
					
						
							|  |  |  |     #   requires_redmine_plugin :foo, :version_or_higher => '0.7.3' | 
					
						
							|  |  |  |     #   requires_redmine_plugin :foo, '0.7.3' | 
					
						
							|  |  |  |     # | 
					
						
							|  |  |  |     #   # Requires a specific version of a Redmine plugin | 
					
						
							|  |  |  |     #   requires_redmine_plugin :foo, :version => '0.7.3'              # 0.7.3 only | 
					
						
							|  |  |  |     #   requires_redmine_plugin :foo, :version => ['0.7.3', '0.8.0']   # 0.7.3 or 0.8.0 | 
					
						
							|  |  |  |     def requires_redmine_plugin(plugin_name, arg) | 
					
						
							| 
									
										
										
										
											2020-11-20 11:36:29 +00:00
										 |  |  |       arg = {:version_or_higher => arg} unless arg.is_a?(Hash) | 
					
						
							| 
									
										
										
										
											2009-12-16 02:07:46 +00:00
										 |  |  |       arg.assert_valid_keys(:version, :version_or_higher) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-15 08:01:10 +00:00
										 |  |  |       begin | 
					
						
							|  |  |  |         plugin = Plugin.find(plugin_name) | 
					
						
							|  |  |  |       rescue PluginNotFound | 
					
						
							|  |  |  |         raise PluginRequirementError.new("#{id} plugin requires the #{plugin_name} plugin") | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2009-12-16 02:07:46 +00:00
										 |  |  |       current = plugin.version.split('.').collect(&:to_i) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       arg.each do |k, v| | 
					
						
							|  |  |  |         v = [] << v unless v.is_a?(Array) | 
					
						
							|  |  |  |         versions = v.collect {|s| s.split('.').collect(&:to_i)} | 
					
						
							|  |  |  |         case k | 
					
						
							|  |  |  |         when :version_or_higher | 
					
						
							| 
									
										
										
										
											2020-12-09 14:11:45 +00:00
										 |  |  |           unless versions.size == 1
 | 
					
						
							|  |  |  |             raise ArgumentError.new("wrong number of versions (#{versions.size} for 1)") | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-16 02:07:46 +00:00
										 |  |  |           unless (current <=> versions.first) >= 0
 | 
					
						
							| 
									
										
										
										
											2020-12-09 14:11:45 +00:00
										 |  |  |             raise PluginRequirementError.new( | 
					
						
							|  |  |  |               "#{id} plugin requires the #{plugin_name} plugin #{v} or higher " \ | 
					
						
							|  |  |  |               "but current is #{current.join('.')}" | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2009-12-16 02:07:46 +00:00
										 |  |  |           end | 
					
						
							|  |  |  |         when :version | 
					
						
							| 
									
										
										
										
											2020-12-09 14:11:45 +00:00
										 |  |  |           unless versions.include?(current.slice(0, 3)) | 
					
						
							|  |  |  |             raise PluginRequirementError.new( | 
					
						
							|  |  |  |               "#{id} plugin requires one the following versions of #{plugin_name}: " \ | 
					
						
							|  |  |  |               "#{v.join(', ')} but current is #{current.join('.')}" | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2009-12-16 02:07:46 +00:00
										 |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |       true | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-23 17:19:27 +00:00
										 |  |  |     # Adds an item to the given +menu+. | 
					
						
							|  |  |  |     # The +id+ parameter (equals to the project id) is automatically added to the url. | 
					
						
							| 
									
										
										
										
											2008-02-22 18:19:00 +00:00
										 |  |  |     #   menu :project_menu, :plugin_example, { :controller => 'example', :action => 'say_hello' }, :caption => 'Sample' | 
					
						
							| 
									
										
										
										
											2011-09-20 02:43:47 +00:00
										 |  |  |     # | 
					
						
							| 
									
										
										
										
											2008-02-22 18:19:00 +00:00
										 |  |  |     # +name+ parameter can be: :top_menu, :account_menu, :application_menu or :project_menu | 
					
						
							| 
									
										
										
										
											2011-09-20 02:43:47 +00:00
										 |  |  |     # | 
					
						
							| 
									
										
										
										
											2008-10-25 09:55:31 +00:00
										 |  |  |     def menu(menu, item, url, options={}) | 
					
						
							|  |  |  |       Redmine::MenuManager.map(menu).push(item, url, options) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |     alias :add_menu_item :menu | 
					
						
							| 
									
										
										
										
											2011-09-20 02:43:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-25 09:55:31 +00:00
										 |  |  |     # Removes +item+ from the given +menu+. | 
					
						
							|  |  |  |     def delete_menu_item(menu, item) | 
					
						
							|  |  |  |       Redmine::MenuManager.map(menu).delete(item) | 
					
						
							| 
									
										
										
										
											2007-09-23 17:19:27 +00:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Defines a permission called +name+ for the given +actions+. | 
					
						
							| 
									
										
										
										
											2011-09-20 02:43:47 +00:00
										 |  |  |     # | 
					
						
							| 
									
										
										
										
											2007-09-23 17:19:27 +00:00
										 |  |  |     # The +actions+ argument is a hash with controllers as keys and actions as values (a single value or an array): | 
					
						
							|  |  |  |     #   permission :destroy_contacts, { :contacts => :destroy } | 
					
						
							|  |  |  |     #   permission :view_contacts, { :contacts => [:index, :show] } | 
					
						
							| 
									
										
										
										
											2011-09-20 02:43:47 +00:00
										 |  |  |     # | 
					
						
							| 
									
										
										
										
											2012-06-25 17:49:35 +00:00
										 |  |  |     # The +options+ argument is a hash that accept the following keys: | 
					
						
							|  |  |  |     # * :public => the permission is public if set to true (implicitly given to any user) | 
					
						
							|  |  |  |     # * :require => can be set to one of the following values to restrict users the permission can be given to: :loggedin, :member | 
					
						
							|  |  |  |     # * :read => set it to true so that the permission is still granted on closed projects | 
					
						
							| 
									
										
										
										
											2011-09-20 02:43:47 +00:00
										 |  |  |     # | 
					
						
							| 
									
										
										
										
											2007-09-23 17:19:27 +00:00
										 |  |  |     # Examples | 
					
						
							|  |  |  |     #   # A permission that is implicitly given to any user | 
					
						
							|  |  |  |     #   # This permission won't appear on the Roles & Permissions setup screen | 
					
						
							| 
									
										
										
										
											2012-06-25 17:49:35 +00:00
										 |  |  |     #   permission :say_hello, { :example => :say_hello }, :public => true, :read => true | 
					
						
							| 
									
										
										
										
											2011-09-20 02:43:47 +00:00
										 |  |  |     # | 
					
						
							| 
									
										
										
										
											2007-09-23 17:19:27 +00:00
										 |  |  |     #   # A permission that can be given to any user | 
					
						
							|  |  |  |     #   permission :say_hello, { :example => :say_hello } | 
					
						
							| 
									
										
										
										
											2011-09-20 02:43:47 +00:00
										 |  |  |     # | 
					
						
							| 
									
										
										
										
											2007-09-23 17:19:27 +00:00
										 |  |  |     #   # A permission that can be given to registered users only | 
					
						
							| 
									
										
										
										
											2010-07-16 03:11:35 +00:00
										 |  |  |     #   permission :say_hello, { :example => :say_hello }, :require => :loggedin | 
					
						
							| 
									
										
										
										
											2011-09-20 02:43:47 +00:00
										 |  |  |     # | 
					
						
							| 
									
										
										
										
											2007-09-23 17:19:27 +00:00
										 |  |  |     #   # A permission that can be given to project members only | 
					
						
							| 
									
										
										
										
											2010-07-16 03:11:35 +00:00
										 |  |  |     #   permission :say_hello, { :example => :say_hello }, :require => :member | 
					
						
							| 
									
										
										
										
											2007-09-23 17:19:27 +00:00
										 |  |  |     def permission(name, actions, options = {}) | 
					
						
							|  |  |  |       if @project_module | 
					
						
							| 
									
										
										
										
											2020-12-09 14:11:45 +00:00
										 |  |  |         Redmine::AccessControl.map do |map| | 
					
						
							|  |  |  |           map.project_module(@project_module) do |map| | 
					
						
							|  |  |  |             map.permission(name, actions, options) | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2007-09-23 17:19:27 +00:00
										 |  |  |       else | 
					
						
							|  |  |  |         Redmine::AccessControl.map {|map| map.permission(name, actions, options)} | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2011-09-20 02:43:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-23 17:19:27 +00:00
										 |  |  |     # Defines a project module, that can be enabled/disabled for each project. | 
					
						
							|  |  |  |     # Permissions defined inside +block+ will be bind to the module. | 
					
						
							| 
									
										
										
										
											2011-09-20 02:43:47 +00:00
										 |  |  |     # | 
					
						
							| 
									
										
										
										
											2007-09-23 17:19:27 +00:00
										 |  |  |     #   project_module :things do | 
					
						
							|  |  |  |     #     permission :view_contacts, { :contacts => [:list, :show] }, :public => true | 
					
						
							|  |  |  |     #     permission :destroy_contacts, { :contacts => :destroy } | 
					
						
							|  |  |  |     #   end | 
					
						
							|  |  |  |     def project_module(name, &block) | 
					
						
							|  |  |  |       @project_module = name | 
					
						
							|  |  |  |       self.instance_eval(&block) | 
					
						
							|  |  |  |       @project_module = nil | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2011-09-20 02:43:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-27 18:38:31 +00:00
										 |  |  |     # Registers an activity provider. | 
					
						
							|  |  |  |     # | 
					
						
							|  |  |  |     # Options: | 
					
						
							|  |  |  |     # * <tt>:class_name</tt> - one or more model(s) that provide these events (inferred from event_type by default) | 
					
						
							|  |  |  |     # * <tt>:default</tt> - setting this option to false will make the events not displayed by default | 
					
						
							| 
									
										
										
										
											2011-09-20 02:43:47 +00:00
										 |  |  |     # | 
					
						
							| 
									
										
										
										
											2008-07-27 18:38:31 +00:00
										 |  |  |     # A model can provide several activity event types. | 
					
						
							| 
									
										
										
										
											2011-09-20 02:43:47 +00:00
										 |  |  |     # | 
					
						
							| 
									
										
										
										
											2008-07-27 18:38:31 +00:00
										 |  |  |     # Examples: | 
					
						
							|  |  |  |     #   register :news | 
					
						
							|  |  |  |     #   register :scrums, :class_name => 'Meeting' | 
					
						
							|  |  |  |     #   register :issues, :class_name => ['Issue', 'Journal'] | 
					
						
							| 
									
										
										
										
											2011-09-20 02:43:47 +00:00
										 |  |  |     # | 
					
						
							| 
									
										
										
										
											2008-07-27 18:38:31 +00:00
										 |  |  |     # Retrieving events: | 
					
						
							|  |  |  |     # Associated model(s) must implement the find_events class method. | 
					
						
							|  |  |  |     # ActiveRecord models can use acts_as_activity_provider as a way to implement this class method. | 
					
						
							| 
									
										
										
										
											2011-09-20 02:43:47 +00:00
										 |  |  |     # | 
					
						
							| 
									
										
										
										
											2014-04-07 08:05:54 +00:00
										 |  |  |     # The following call should return all the scrum events visible by current user that occurred in the 5 last days: | 
					
						
							| 
									
										
										
										
											2008-07-27 18:38:31 +00:00
										 |  |  |     #   Meeting.find_events('scrums', User.current, 5.days.ago, Date.today) | 
					
						
							|  |  |  |     #   Meeting.find_events('scrums', User.current, 5.days.ago, Date.today, :project => foo) # events for project foo only | 
					
						
							| 
									
										
										
										
											2011-09-20 02:43:47 +00:00
										 |  |  |     # | 
					
						
							| 
									
										
										
										
											2008-07-27 18:38:31 +00:00
										 |  |  |     # Note that :view_scrums permission is required to view these events in the activity view. | 
					
						
							|  |  |  |     def activity_provider(*args) | 
					
						
							|  |  |  |       Redmine::Activity.register(*args) | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2011-09-20 02:43:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-27 11:08:29 +00:00
										 |  |  |     # Registers a wiki formatter. | 
					
						
							|  |  |  |     # | 
					
						
							|  |  |  |     # Parameters: | 
					
						
							| 
									
										
										
										
											2015-06-21 16:29:25 +00:00
										 |  |  |     # * +name+ - formatter name | 
					
						
							| 
									
										
										
										
											2008-10-27 11:08:29 +00:00
										 |  |  |     # * +formatter+ - formatter class, which should have an instance method +to_html+ | 
					
						
							| 
									
										
										
										
											2015-06-21 16:29:25 +00:00
										 |  |  |     # * +helper+ - helper module, which will be included by wiki pages (optional) | 
					
						
							|  |  |  |     # * +html_parser+ class reponsible for converting HTML to wiki text (optional) | 
					
						
							|  |  |  |     # * +options+ - a Hash of options (optional) | 
					
						
							|  |  |  |     #   * :label - label for the formatter displayed in application settings | 
					
						
							|  |  |  |     # | 
					
						
							|  |  |  |     # Examples: | 
					
						
							| 
									
										
										
										
											2019-06-06 14:50:14 +00:00
										 |  |  |     #   wiki_format_provider(:custom_formatter, CustomFormatter, :label => "My custom formatter") | 
					
						
							| 
									
										
										
										
											2015-06-21 16:29:25 +00:00
										 |  |  |     # | 
					
						
							|  |  |  |     def wiki_format_provider(name, *args) | 
					
						
							|  |  |  |       Redmine::WikiFormatting.register(name, *args) | 
					
						
							| 
									
										
										
										
											2008-10-27 11:08:29 +00:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2007-09-23 17:19:27 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Returns +true+ if the plugin can be configured. | 
					
						
							|  |  |  |     def configurable? | 
					
						
							|  |  |  |       settings && settings.is_a?(Hash) && !settings[:partial].blank? | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2012-04-25 17:17:49 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def mirror_assets | 
					
						
							|  |  |  |       source = assets_directory | 
					
						
							|  |  |  |       destination = public_directory | 
					
						
							|  |  |  |       return unless File.directory?(source) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       source_files = Dir[source + "/**/*"] | 
					
						
							| 
									
										
										
										
											2020-11-20 11:36:29 +00:00
										 |  |  |       source_dirs = source_files.select {|d| File.directory?(d)} | 
					
						
							| 
									
										
										
										
											2012-04-25 17:17:49 +00:00
										 |  |  |       source_files -= source_dirs | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       unless source_files.empty? | 
					
						
							|  |  |  |         base_target_dir = File.join(destination, File.dirname(source_files.first).gsub(source, '')) | 
					
						
							| 
									
										
										
										
											2012-09-26 04:11:14 +00:00
										 |  |  |         begin | 
					
						
							|  |  |  |           FileUtils.mkdir_p(base_target_dir) | 
					
						
							| 
									
										
										
										
											2019-05-25 06:50:25 +00:00
										 |  |  |         rescue => e | 
					
						
							| 
									
										
										
										
											2012-09-26 04:11:14 +00:00
										 |  |  |           raise "Could not create directory #{base_target_dir}: " + e.message | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2012-04-25 17:17:49 +00:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       source_dirs.each do |dir| | 
					
						
							|  |  |  |         # strip down these paths so we have simple, relative paths we can | 
					
						
							|  |  |  |         # add to the destination | 
					
						
							|  |  |  |         target_dir = File.join(destination, dir.gsub(source, '')) | 
					
						
							|  |  |  |         begin | 
					
						
							|  |  |  |           FileUtils.mkdir_p(target_dir) | 
					
						
							| 
									
										
										
										
											2019-05-25 06:50:25 +00:00
										 |  |  |         rescue => e | 
					
						
							| 
									
										
										
										
											2012-09-26 04:11:00 +00:00
										 |  |  |           raise "Could not create directory #{target_dir}: " + e.message | 
					
						
							| 
									
										
										
										
											2012-04-25 17:17:49 +00:00
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       source_files.each do |file| | 
					
						
							|  |  |  |         begin | 
					
						
							|  |  |  |           target = File.join(destination, file.gsub(source, '')) | 
					
						
							|  |  |  |           unless File.exist?(target) && FileUtils.identical?(file, target) | 
					
						
							|  |  |  |             FileUtils.cp(file, target) | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2019-05-25 06:50:25 +00:00
										 |  |  |         rescue => e | 
					
						
							| 
									
										
										
										
											2012-09-26 04:11:00 +00:00
										 |  |  |           raise "Could not copy #{file} to #{target}: " + e.message | 
					
						
							| 
									
										
										
										
											2012-04-25 17:17:49 +00:00
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2012-04-28 11:07:59 +00:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-01 10:25:22 +00:00
										 |  |  |     # Mirrors assets from one or all plugins to public/plugin_assets | 
					
						
							|  |  |  |     def self.mirror_assets(name=nil) | 
					
						
							|  |  |  |       if name.present? | 
					
						
							|  |  |  |         find(name).mirror_assets | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         all.each do |plugin| | 
					
						
							|  |  |  |           plugin.mirror_assets | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2012-04-28 11:07:59 +00:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2012-04-25 17:17:49 +00:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # The directory containing this plugin's migrations (<tt>plugin/db/migrate</tt>) | 
					
						
							|  |  |  |     def migration_directory | 
					
						
							| 
									
										
										
										
											2015-06-21 16:32:27 +00:00
										 |  |  |       File.join(directory, 'db', 'migrate') | 
					
						
							| 
									
										
										
										
											2012-04-25 17:17:49 +00:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2012-05-01 09:02:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-25 17:17:49 +00:00
										 |  |  |     # Returns the version number of the latest migration for this plugin. Returns | 
					
						
							|  |  |  |     # nil if this plugin has no migrations. | 
					
						
							|  |  |  |     def latest_migration | 
					
						
							|  |  |  |       migrations.last | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2012-05-01 09:02:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-25 17:17:49 +00:00
										 |  |  |     # Returns the version numbers of all migrations for this plugin. | 
					
						
							|  |  |  |     def migrations | 
					
						
							|  |  |  |       migrations = Dir[migration_directory+"/*.rb"] | 
					
						
							| 
									
										
										
										
											2020-11-20 11:36:29 +00:00
										 |  |  |       migrations.map {|p| File.basename(p).match(/0*(\d+)\_/)[1].to_i}.sort | 
					
						
							| 
									
										
										
										
											2012-04-25 17:17:49 +00:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2012-05-01 09:02:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-25 17:17:49 +00:00
										 |  |  |     # Migrate this plugin to the given version | 
					
						
							|  |  |  |     def migrate(version = nil) | 
					
						
							|  |  |  |       Redmine::Plugin::Migrator.migrate_plugin(self, version) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-01 09:02:34 +00:00
										 |  |  |     # Migrates all plugins or a single plugin to a given version | 
					
						
							|  |  |  |     # Exemples: | 
					
						
							|  |  |  |     #   Plugin.migrate | 
					
						
							|  |  |  |     #   Plugin.migrate('sample_plugin') | 
					
						
							|  |  |  |     #   Plugin.migrate('sample_plugin', 1) | 
					
						
							|  |  |  |     # | 
					
						
							|  |  |  |     def self.migrate(name=nil, version=nil) | 
					
						
							|  |  |  |       if name.present? | 
					
						
							|  |  |  |         find(name).migrate(version) | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         all.each do |plugin| | 
					
						
							|  |  |  |           plugin.migrate | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-23 14:22:29 +00:00
										 |  |  |     class MigrationContext < ActiveRecord::MigrationContext | 
					
						
							|  |  |  |       def up(target_version = nil) | 
					
						
							| 
									
										
										
										
											2019-10-17 11:22:06 +00:00
										 |  |  |         selected_migrations = | 
					
						
							|  |  |  |           if block_given? | 
					
						
							| 
									
										
										
										
											2020-11-20 11:36:29 +00:00
										 |  |  |             migrations.select {|m| yield m} | 
					
						
							| 
									
										
										
										
											2019-10-17 11:22:06 +00:00
										 |  |  |           else | 
					
						
							|  |  |  |             migrations | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2021-04-01 15:02:47 +00:00
										 |  |  |         Migrator.new(:up, selected_migrations, schema_migration, target_version).migrate | 
					
						
							| 
									
										
										
										
											2018-09-23 14:22:29 +00:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def down(target_version = nil) | 
					
						
							| 
									
										
										
										
											2019-10-17 11:22:06 +00:00
										 |  |  |         selected_migrations = | 
					
						
							|  |  |  |           if block_given? | 
					
						
							| 
									
										
										
										
											2020-11-20 11:36:29 +00:00
										 |  |  |             migrations.select {|m| yield m} | 
					
						
							| 
									
										
										
										
											2019-10-17 11:22:06 +00:00
										 |  |  |           else | 
					
						
							|  |  |  |             migrations | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2021-04-01 15:02:47 +00:00
										 |  |  |         Migrator.new(:down, selected_migrations, schema_migration, target_version).migrate | 
					
						
							| 
									
										
										
										
											2018-09-23 14:22:29 +00:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def run(direction, target_version) | 
					
						
							| 
									
										
										
										
											2021-04-01 15:02:47 +00:00
										 |  |  |         Migrator.new(direction, migrations, schema_migration, target_version).run | 
					
						
							| 
									
										
										
										
											2018-09-23 14:22:29 +00:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def open | 
					
						
							| 
									
										
										
										
											2021-04-01 15:02:47 +00:00
										 |  |  |         Migrator.new(:up, migrations, schema_migration) | 
					
						
							| 
									
										
										
										
											2018-09-23 14:22:29 +00:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-25 17:17:49 +00:00
										 |  |  |     class Migrator < ActiveRecord::Migrator | 
					
						
							|  |  |  |       # We need to be able to set the 'current' plugin being migrated. | 
					
						
							|  |  |  |       cattr_accessor :current_plugin | 
					
						
							| 
									
										
										
										
											2013-06-06 05:09:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-25 17:17:49 +00:00
										 |  |  |       class << self | 
					
						
							|  |  |  |         # Runs the migrations from a plugin, up (or down) to the version given | 
					
						
							|  |  |  |         def migrate_plugin(plugin, version) | 
					
						
							|  |  |  |           self.current_plugin = plugin | 
					
						
							|  |  |  |           return if current_version(plugin) == version | 
					
						
							| 
									
										
										
										
											2018-09-23 14:22:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-01 15:02:47 +00:00
										 |  |  |           MigrationContext.new(plugin.migration_directory, ::ActiveRecord::Base.connection.schema_migration).migrate(version) | 
					
						
							| 
									
										
										
										
											2012-04-25 17:17:49 +00:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2013-06-06 05:09:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-23 14:22:29 +00:00
										 |  |  |         def get_all_versions(plugin = current_plugin) | 
					
						
							| 
									
										
										
										
											2012-04-25 17:17:49 +00:00
										 |  |  |           # Delete migrations that don't match .. to_i will work because the number comes first | 
					
						
							| 
									
										
										
										
											2018-09-23 14:22:29 +00:00
										 |  |  |           @all_versions ||= {} | 
					
						
							|  |  |  |           @all_versions[plugin.id.to_s] ||= begin | 
					
						
							|  |  |  |             sm_table = ::ActiveRecord::SchemaMigration.table_name | 
					
						
							|  |  |  |             migration_versions  = ActiveRecord::Base.connection.select_values("SELECT version FROM #{sm_table}") | 
					
						
							| 
									
										
										
										
											2020-11-20 11:36:29 +00:00
										 |  |  |             versions_by_plugins = migration_versions.group_by {|version| version.match(/-(.*)$/).try(:[], 1)} | 
					
						
							|  |  |  |             @all_versions       = versions_by_plugins.transform_values! {|versions| versions.map!(&:to_i).sort!} | 
					
						
							| 
									
										
										
										
											2018-09-23 14:22:29 +00:00
										 |  |  |             @all_versions[plugin.id.to_s] || [] | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def current_version(plugin = current_plugin) | 
					
						
							|  |  |  |           get_all_versions(plugin).last || 0
 | 
					
						
							| 
									
										
										
										
											2012-04-25 17:17:49 +00:00
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2013-06-06 05:09:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-23 14:22:29 +00:00
										 |  |  |       def load_migrated | 
					
						
							|  |  |  |         @migrated_versions = Set.new(self.class.get_all_versions(current_plugin)) | 
					
						
							| 
									
										
										
										
											2012-04-25 17:17:49 +00:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2013-06-06 05:09:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-25 17:17:49 +00:00
										 |  |  |       def record_version_state_after_migrating(version) | 
					
						
							|  |  |  |         super(version.to_s + "-" + current_plugin.id.to_s) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2007-09-23 17:19:27 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | end |