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