annotate .svn/pristine/4d/4db1f392c52d1c52ef2d37c9a27b017bd4d891ac.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

Merge from branch "live"
author Chris Cannam
date Tue, 09 Sep 2014 09:34:53 +0100
parents cbb26bc654de
children
rev   line source
Chris@909 1 # Redmine - project management software
Chris@909 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
Chris@909 3 #
Chris@909 4 # This program is free software; you can redistribute it and/or
Chris@909 5 # modify it under the terms of the GNU General Public License
Chris@909 6 # as published by the Free Software Foundation; either version 2
Chris@909 7 # of the License, or (at your option) any later version.
Chris@909 8 #
Chris@909 9 # This program is distributed in the hope that it will be useful,
Chris@909 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@909 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@909 12 # GNU General Public License for more details.
Chris@909 13 #
Chris@909 14 # You should have received a copy of the GNU General Public License
Chris@909 15 # along with this program; if not, write to the Free Software
Chris@909 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@909 17
Chris@909 18 module Redmine #:nodoc:
Chris@909 19
Chris@909 20 class PluginNotFound < StandardError; end
Chris@909 21 class PluginRequirementError < StandardError; end
Chris@909 22
Chris@909 23 # Base class for Redmine plugins.
Chris@909 24 # Plugins are registered using the <tt>register</tt> class method that acts as the public constructor.
Chris@909 25 #
Chris@909 26 # Redmine::Plugin.register :example do
Chris@909 27 # name 'Example plugin'
Chris@909 28 # author 'John Smith'
Chris@909 29 # description 'This is an example plugin for Redmine'
Chris@909 30 # version '0.0.1'
Chris@909 31 # settings :default => {'foo'=>'bar'}, :partial => 'settings/settings'
Chris@909 32 # end
Chris@909 33 #
Chris@909 34 # === Plugin attributes
Chris@909 35 #
Chris@909 36 # +settings+ is an optional attribute that let the plugin be configurable.
Chris@909 37 # It must be a hash with the following keys:
Chris@909 38 # * <tt>:default</tt>: default value for the plugin settings
Chris@909 39 # * <tt>:partial</tt>: path of the configuration partial view, relative to the plugin <tt>app/views</tt> directory
Chris@909 40 # Example:
Chris@909 41 # settings :default => {'foo'=>'bar'}, :partial => 'settings/settings'
Chris@909 42 # In this example, the settings partial will be found here in the plugin directory: <tt>app/views/settings/_settings.rhtml</tt>.
Chris@909 43 #
Chris@909 44 # When rendered, the plugin settings value is available as the local variable +settings+
Chris@909 45 class Plugin
Chris@909 46 @registered_plugins = {}
Chris@909 47 class << self
Chris@909 48 attr_reader :registered_plugins
Chris@909 49 private :new
Chris@909 50
Chris@909 51 def def_field(*names)
Chris@909 52 class_eval do
Chris@909 53 names.each do |name|
Chris@909 54 define_method(name) do |*args|
Chris@909 55 args.empty? ? instance_variable_get("@#{name}") : instance_variable_set("@#{name}", *args)
Chris@909 56 end
Chris@909 57 end
Chris@909 58 end
Chris@909 59 end
Chris@909 60 end
Chris@909 61 def_field :name, :description, :url, :author, :author_url, :version, :settings
Chris@909 62 attr_reader :id
Chris@909 63
Chris@909 64 # Plugin constructor
Chris@909 65 def self.register(id, &block)
Chris@909 66 p = new(id)
Chris@909 67 p.instance_eval(&block)
Chris@909 68 # Set a default name if it was not provided during registration
Chris@909 69 p.name(id.to_s.humanize) if p.name.nil?
Chris@909 70 # Adds plugin locales if any
Chris@909 71 # YAML translation files should be found under <plugin>/config/locales/
Chris@909 72 ::I18n.load_path += Dir.glob(File.join(Rails.root, 'vendor', 'plugins', id.to_s, 'config', 'locales', '*.yml'))
Chris@909 73 registered_plugins[id] = p
Chris@909 74 end
Chris@909 75
Chris@909 76 # Returns an array of all registered plugins
Chris@909 77 def self.all
Chris@909 78 registered_plugins.values.sort
Chris@909 79 end
Chris@909 80
Chris@909 81 # Finds a plugin by its id
Chris@909 82 # Returns a PluginNotFound exception if the plugin doesn't exist
Chris@909 83 def self.find(id)
Chris@909 84 registered_plugins[id.to_sym] || raise(PluginNotFound)
Chris@909 85 end
Chris@909 86
Chris@909 87 # Clears the registered plugins hash
Chris@909 88 # It doesn't unload installed plugins
Chris@909 89 def self.clear
Chris@909 90 @registered_plugins = {}
Chris@909 91 end
Chris@909 92
Chris@909 93 # Checks if a plugin is installed
Chris@909 94 #
Chris@909 95 # @param [String] id name of the plugin
Chris@909 96 def self.installed?(id)
Chris@909 97 registered_plugins[id.to_sym].present?
Chris@909 98 end
Chris@909 99
Chris@909 100 def initialize(id)
Chris@909 101 @id = id.to_sym
Chris@909 102 end
Chris@909 103
Chris@909 104 def <=>(plugin)
Chris@909 105 self.id.to_s <=> plugin.id.to_s
Chris@909 106 end
Chris@909 107
Chris@909 108 # Sets a requirement on Redmine version
Chris@909 109 # Raises a PluginRequirementError exception if the requirement is not met
Chris@909 110 #
Chris@909 111 # Examples
Chris@909 112 # # Requires Redmine 0.7.3 or higher
Chris@909 113 # requires_redmine :version_or_higher => '0.7.3'
Chris@909 114 # requires_redmine '0.7.3'
Chris@909 115 #
Chris@909 116 # # Requires a specific Redmine version
Chris@909 117 # requires_redmine :version => '0.7.3' # 0.7.3 only
Chris@909 118 # requires_redmine :version => ['0.7.3', '0.8.0'] # 0.7.3 or 0.8.0
Chris@909 119 def requires_redmine(arg)
Chris@909 120 arg = { :version_or_higher => arg } unless arg.is_a?(Hash)
Chris@909 121 arg.assert_valid_keys(:version, :version_or_higher)
Chris@909 122
Chris@909 123 current = Redmine::VERSION.to_a
Chris@909 124 arg.each do |k, v|
Chris@909 125 v = [] << v unless v.is_a?(Array)
Chris@909 126 versions = v.collect {|s| s.split('.').collect(&:to_i)}
Chris@909 127 case k
Chris@909 128 when :version_or_higher
Chris@909 129 raise ArgumentError.new("wrong number of versions (#{versions.size} for 1)") unless versions.size == 1
Chris@909 130 unless (current <=> versions.first) >= 0
Chris@909 131 raise PluginRequirementError.new("#{id} plugin requires Redmine #{v} or higher but current is #{current.join('.')}")
Chris@909 132 end
Chris@909 133 when :version
Chris@909 134 unless versions.include?(current.slice(0,3))
Chris@909 135 raise PluginRequirementError.new("#{id} plugin requires one the following Redmine versions: #{v.join(', ')} but current is #{current.join('.')}")
Chris@909 136 end
Chris@909 137 end
Chris@909 138 end
Chris@909 139 true
Chris@909 140 end
Chris@909 141
Chris@909 142 # Sets a requirement on a Redmine plugin version
Chris@909 143 # Raises a PluginRequirementError exception if the requirement is not met
Chris@909 144 #
Chris@909 145 # Examples
Chris@909 146 # # Requires a plugin named :foo version 0.7.3 or higher
Chris@909 147 # requires_redmine_plugin :foo, :version_or_higher => '0.7.3'
Chris@909 148 # requires_redmine_plugin :foo, '0.7.3'
Chris@909 149 #
Chris@909 150 # # Requires a specific version of a Redmine plugin
Chris@909 151 # requires_redmine_plugin :foo, :version => '0.7.3' # 0.7.3 only
Chris@909 152 # requires_redmine_plugin :foo, :version => ['0.7.3', '0.8.0'] # 0.7.3 or 0.8.0
Chris@909 153 def requires_redmine_plugin(plugin_name, arg)
Chris@909 154 arg = { :version_or_higher => arg } unless arg.is_a?(Hash)
Chris@909 155 arg.assert_valid_keys(:version, :version_or_higher)
Chris@909 156
Chris@909 157 plugin = Plugin.find(plugin_name)
Chris@909 158 current = plugin.version.split('.').collect(&:to_i)
Chris@909 159
Chris@909 160 arg.each do |k, v|
Chris@909 161 v = [] << v unless v.is_a?(Array)
Chris@909 162 versions = v.collect {|s| s.split('.').collect(&:to_i)}
Chris@909 163 case k
Chris@909 164 when :version_or_higher
Chris@909 165 raise ArgumentError.new("wrong number of versions (#{versions.size} for 1)") unless versions.size == 1
Chris@909 166 unless (current <=> versions.first) >= 0
Chris@909 167 raise PluginRequirementError.new("#{id} plugin requires the #{plugin_name} plugin #{v} or higher but current is #{current.join('.')}")
Chris@909 168 end
Chris@909 169 when :version
Chris@909 170 unless versions.include?(current.slice(0,3))
Chris@909 171 raise PluginRequirementError.new("#{id} plugin requires one the following versions of #{plugin_name}: #{v.join(', ')} but current is #{current.join('.')}")
Chris@909 172 end
Chris@909 173 end
Chris@909 174 end
Chris@909 175 true
Chris@909 176 end
Chris@909 177
Chris@909 178 # Adds an item to the given +menu+.
Chris@909 179 # The +id+ parameter (equals to the project id) is automatically added to the url.
Chris@909 180 # menu :project_menu, :plugin_example, { :controller => 'example', :action => 'say_hello' }, :caption => 'Sample'
Chris@909 181 #
Chris@909 182 # +name+ parameter can be: :top_menu, :account_menu, :application_menu or :project_menu
Chris@909 183 #
Chris@909 184 def menu(menu, item, url, options={})
Chris@909 185 Redmine::MenuManager.map(menu).push(item, url, options)
Chris@909 186 end
Chris@909 187 alias :add_menu_item :menu
Chris@909 188
Chris@909 189 # Removes +item+ from the given +menu+.
Chris@909 190 def delete_menu_item(menu, item)
Chris@909 191 Redmine::MenuManager.map(menu).delete(item)
Chris@909 192 end
Chris@909 193
Chris@909 194 # Defines a permission called +name+ for the given +actions+.
Chris@909 195 #
Chris@909 196 # The +actions+ argument is a hash with controllers as keys and actions as values (a single value or an array):
Chris@909 197 # permission :destroy_contacts, { :contacts => :destroy }
Chris@909 198 # permission :view_contacts, { :contacts => [:index, :show] }
Chris@909 199 #
Chris@909 200 # The +options+ argument can be used to make the permission public (implicitly given to any user)
Chris@909 201 # or to restrict users the permission can be given to.
Chris@909 202 #
Chris@909 203 # Examples
Chris@909 204 # # A permission that is implicitly given to any user
Chris@909 205 # # This permission won't appear on the Roles & Permissions setup screen
Chris@909 206 # permission :say_hello, { :example => :say_hello }, :public => true
Chris@909 207 #
Chris@909 208 # # A permission that can be given to any user
Chris@909 209 # permission :say_hello, { :example => :say_hello }
Chris@909 210 #
Chris@909 211 # # A permission that can be given to registered users only
Chris@909 212 # permission :say_hello, { :example => :say_hello }, :require => :loggedin
Chris@909 213 #
Chris@909 214 # # A permission that can be given to project members only
Chris@909 215 # permission :say_hello, { :example => :say_hello }, :require => :member
Chris@909 216 def permission(name, actions, options = {})
Chris@909 217 if @project_module
Chris@909 218 Redmine::AccessControl.map {|map| map.project_module(@project_module) {|map|map.permission(name, actions, options)}}
Chris@909 219 else
Chris@909 220 Redmine::AccessControl.map {|map| map.permission(name, actions, options)}
Chris@909 221 end
Chris@909 222 end
Chris@909 223
Chris@909 224 # Defines a project module, that can be enabled/disabled for each project.
Chris@909 225 # Permissions defined inside +block+ will be bind to the module.
Chris@909 226 #
Chris@909 227 # project_module :things do
Chris@909 228 # permission :view_contacts, { :contacts => [:list, :show] }, :public => true
Chris@909 229 # permission :destroy_contacts, { :contacts => :destroy }
Chris@909 230 # end
Chris@909 231 def project_module(name, &block)
Chris@909 232 @project_module = name
Chris@909 233 self.instance_eval(&block)
Chris@909 234 @project_module = nil
Chris@909 235 end
Chris@909 236
Chris@909 237 # Registers an activity provider.
Chris@909 238 #
Chris@909 239 # Options:
Chris@909 240 # * <tt>:class_name</tt> - one or more model(s) that provide these events (inferred from event_type by default)
Chris@909 241 # * <tt>:default</tt> - setting this option to false will make the events not displayed by default
Chris@909 242 #
Chris@909 243 # A model can provide several activity event types.
Chris@909 244 #
Chris@909 245 # Examples:
Chris@909 246 # register :news
Chris@909 247 # register :scrums, :class_name => 'Meeting'
Chris@909 248 # register :issues, :class_name => ['Issue', 'Journal']
Chris@909 249 #
Chris@909 250 # Retrieving events:
Chris@909 251 # Associated model(s) must implement the find_events class method.
Chris@909 252 # ActiveRecord models can use acts_as_activity_provider as a way to implement this class method.
Chris@909 253 #
Chris@909 254 # The following call should return all the scrum events visible by current user that occured in the 5 last days:
Chris@909 255 # Meeting.find_events('scrums', User.current, 5.days.ago, Date.today)
Chris@909 256 # Meeting.find_events('scrums', User.current, 5.days.ago, Date.today, :project => foo) # events for project foo only
Chris@909 257 #
Chris@909 258 # Note that :view_scrums permission is required to view these events in the activity view.
Chris@909 259 def activity_provider(*args)
Chris@909 260 Redmine::Activity.register(*args)
Chris@909 261 end
Chris@909 262
Chris@909 263 # Registers a wiki formatter.
Chris@909 264 #
Chris@909 265 # Parameters:
Chris@909 266 # * +name+ - human-readable name
Chris@909 267 # * +formatter+ - formatter class, which should have an instance method +to_html+
Chris@909 268 # * +helper+ - helper module, which will be included by wiki pages
Chris@909 269 def wiki_format_provider(name, formatter, helper)
Chris@909 270 Redmine::WikiFormatting.register(name, formatter, helper)
Chris@909 271 end
Chris@909 272
Chris@909 273 # Returns +true+ if the plugin can be configured.
Chris@909 274 def configurable?
Chris@909 275 settings && settings.is_a?(Hash) && !settings[:partial].blank?
Chris@909 276 end
Chris@909 277 end
Chris@909 278 end