comparison lib/redmine/plugin.rb @ 909:cbb26bc654de redmine-1.3

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