@@ -1,54 +1,46 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2008 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2008 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 |
|
18 | module Redmine | |
19 | module Activity |
|
19 | module Activity | |
20 |
|
20 | |||
21 | mattr_accessor :available_event_types, :default_event_types, :providers |
|
21 | mattr_accessor :available_event_types, :default_event_types, :providers | |
22 |
|
22 | |||
23 | @@available_event_types = [] |
|
23 | @@available_event_types = [] | |
24 | @@default_event_types = [] |
|
24 | @@default_event_types = [] | |
25 | @@providers = Hash.new {|h,k| h[k]=[] } |
|
25 | @@providers = Hash.new {|h,k| h[k]=[] } | |
26 |
|
26 | |||
27 | class << self |
|
27 | class << self | |
28 | def map(&block) |
|
28 | def map(&block) | |
29 | yield self |
|
29 | yield self | |
30 | end |
|
30 | end | |
31 |
|
31 | |||
32 | # Registers an activity provider |
|
32 | # Registers an activity provider | |
33 | # |
|
|||
34 | # Options: |
|
|||
35 | # * :class_name - one or more model(s) that provide these events (inferred from event_type by default) |
|
|||
36 | # * :default - setting this option to false will make the events not displayed by default |
|
|||
37 | # |
|
|||
38 | # Examples: |
|
|||
39 | # register :issues |
|
|||
40 | # register :myevents, :class_name => 'Meeting' |
|
|||
41 | def register(event_type, options={}) |
|
33 | def register(event_type, options={}) | |
42 | options.assert_valid_keys(:class_name, :default) |
|
34 | options.assert_valid_keys(:class_name, :default) | |
43 |
|
35 | |||
44 | event_type = event_type.to_s |
|
36 | event_type = event_type.to_s | |
45 | providers = options[:class_name] || event_type.classify |
|
37 | providers = options[:class_name] || event_type.classify | |
46 | providers = ([] << providers) unless providers.is_a?(Array) |
|
38 | providers = ([] << providers) unless providers.is_a?(Array) | |
47 |
|
39 | |||
48 | @@available_event_types << event_type unless @@available_event_types.include?(event_type) |
|
40 | @@available_event_types << event_type unless @@available_event_types.include?(event_type) | |
49 | @@default_event_types << event_type unless options[:default] == false |
|
41 | @@default_event_types << event_type unless options[:default] == false | |
50 | @@providers[event_type] += providers |
|
42 | @@providers[event_type] += providers | |
51 | end |
|
43 | end | |
52 | end |
|
44 | end | |
53 | end |
|
45 | end | |
54 | end |
|
46 | end |
@@ -1,125 +1,151 | |||||
1 | # redMine - project management software |
|
1 | # redMine - project management software | |
2 | # Copyright (C) 2006-2007 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2007 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 | # Base class for Redmine plugins. |
|
20 | # Base class for Redmine plugins. | |
21 | # Plugins are registered using the <tt>register</tt> class method that acts as the public constructor. |
|
21 | # Plugins are registered using the <tt>register</tt> class method that acts as the public constructor. | |
22 | # |
|
22 | # | |
23 | # Redmine::Plugin.register :example do |
|
23 | # Redmine::Plugin.register :example do | |
24 | # name 'Example plugin' |
|
24 | # name 'Example plugin' | |
25 | # author 'John Smith' |
|
25 | # author 'John Smith' | |
26 | # description 'This is an example plugin for Redmine' |
|
26 | # description 'This is an example plugin for Redmine' | |
27 | # version '0.0.1' |
|
27 | # version '0.0.1' | |
28 | # settings :default => {'foo'=>'bar'}, :partial => 'settings/settings' |
|
28 | # settings :default => {'foo'=>'bar'}, :partial => 'settings/settings' | |
29 | # end |
|
29 | # end | |
30 | # |
|
30 | # | |
31 | # === Plugin attributes |
|
31 | # === Plugin attributes | |
32 | # |
|
32 | # | |
33 | # +settings+ is an optional attribute that let the plugin be configurable. |
|
33 | # +settings+ is an optional attribute that let the plugin be configurable. | |
34 | # It must be a hash with the following keys: |
|
34 | # It must be a hash with the following keys: | |
35 | # * <tt>:default</tt>: default value for the plugin settings |
|
35 | # * <tt>:default</tt>: default value for the plugin settings | |
36 | # * <tt>:partial</tt>: path of the configuration partial view, relative to the plugin <tt>app/views</tt> directory |
|
36 | # * <tt>:partial</tt>: path of the configuration partial view, relative to the plugin <tt>app/views</tt> directory | |
37 | # Example: |
|
37 | # Example: | |
38 | # settings :default => {'foo'=>'bar'}, :partial => 'settings/settings' |
|
38 | # settings :default => {'foo'=>'bar'}, :partial => 'settings/settings' | |
39 | # In this example, the settings partial will be found here in the plugin directory: <tt>app/views/settings/_settings.rhtml</tt>. |
|
39 | # In this example, the settings partial will be found here in the plugin directory: <tt>app/views/settings/_settings.rhtml</tt>. | |
40 | # |
|
40 | # | |
41 | # When rendered, the plugin settings value is available as the local variable +settings+ |
|
41 | # When rendered, the plugin settings value is available as the local variable +settings+ | |
42 | class Plugin |
|
42 | class Plugin | |
43 | @registered_plugins = {} |
|
43 | @registered_plugins = {} | |
44 | class << self |
|
44 | class << self | |
45 | attr_reader :registered_plugins |
|
45 | attr_reader :registered_plugins | |
46 | private :new |
|
46 | private :new | |
47 |
|
47 | |||
48 | def def_field(*names) |
|
48 | def def_field(*names) | |
49 | class_eval do |
|
49 | class_eval do | |
50 | names.each do |name| |
|
50 | names.each do |name| | |
51 | define_method(name) do |*args| |
|
51 | define_method(name) do |*args| | |
52 | args.empty? ? instance_variable_get("@#{name}") : instance_variable_set("@#{name}", *args) |
|
52 | args.empty? ? instance_variable_get("@#{name}") : instance_variable_set("@#{name}", *args) | |
53 | end |
|
53 | end | |
54 | end |
|
54 | end | |
55 | end |
|
55 | end | |
56 | end |
|
56 | end | |
57 | end |
|
57 | end | |
58 | def_field :name, :description, :author, :version, :settings |
|
58 | def_field :name, :description, :author, :version, :settings | |
59 |
|
59 | |||
60 | # Plugin constructor |
|
60 | # Plugin constructor | |
61 | def self.register(name, &block) |
|
61 | def self.register(name, &block) | |
62 | p = new |
|
62 | p = new | |
63 | p.instance_eval(&block) |
|
63 | p.instance_eval(&block) | |
64 | Plugin.registered_plugins[name] = p |
|
64 | Plugin.registered_plugins[name] = p | |
65 | end |
|
65 | end | |
66 |
|
66 | |||
67 | # Adds an item to the given +menu+. |
|
67 | # Adds an item to the given +menu+. | |
68 | # The +id+ parameter (equals to the project id) is automatically added to the url. |
|
68 | # The +id+ parameter (equals to the project id) is automatically added to the url. | |
69 | # menu :project_menu, :plugin_example, { :controller => 'example', :action => 'say_hello' }, :caption => 'Sample' |
|
69 | # menu :project_menu, :plugin_example, { :controller => 'example', :action => 'say_hello' }, :caption => 'Sample' | |
70 | # |
|
70 | # | |
71 | # +name+ parameter can be: :top_menu, :account_menu, :application_menu or :project_menu |
|
71 | # +name+ parameter can be: :top_menu, :account_menu, :application_menu or :project_menu | |
72 | # |
|
72 | # | |
73 | def menu(name, item, url, options={}) |
|
73 | def menu(name, item, url, options={}) | |
74 | Redmine::MenuManager.map(name) {|menu| menu.push item, url, options} |
|
74 | Redmine::MenuManager.map(name) {|menu| menu.push item, url, options} | |
75 | end |
|
75 | end | |
76 |
|
76 | |||
77 | # Defines a permission called +name+ for the given +actions+. |
|
77 | # Defines a permission called +name+ for the given +actions+. | |
78 | # |
|
78 | # | |
79 | # The +actions+ argument is a hash with controllers as keys and actions as values (a single value or an array): |
|
79 | # The +actions+ argument is a hash with controllers as keys and actions as values (a single value or an array): | |
80 | # permission :destroy_contacts, { :contacts => :destroy } |
|
80 | # permission :destroy_contacts, { :contacts => :destroy } | |
81 | # permission :view_contacts, { :contacts => [:index, :show] } |
|
81 | # permission :view_contacts, { :contacts => [:index, :show] } | |
82 | # |
|
82 | # | |
83 | # The +options+ argument can be used to make the permission public (implicitly given to any user) |
|
83 | # The +options+ argument can be used to make the permission public (implicitly given to any user) | |
84 | # or to restrict users the permission can be given to. |
|
84 | # or to restrict users the permission can be given to. | |
85 | # |
|
85 | # | |
86 | # Examples |
|
86 | # Examples | |
87 | # # A permission that is implicitly given to any user |
|
87 | # # A permission that is implicitly given to any user | |
88 | # # This permission won't appear on the Roles & Permissions setup screen |
|
88 | # # This permission won't appear on the Roles & Permissions setup screen | |
89 | # permission :say_hello, { :example => :say_hello }, :public => true |
|
89 | # permission :say_hello, { :example => :say_hello }, :public => true | |
90 | # |
|
90 | # | |
91 | # # A permission that can be given to any user |
|
91 | # # A permission that can be given to any user | |
92 | # permission :say_hello, { :example => :say_hello } |
|
92 | # permission :say_hello, { :example => :say_hello } | |
93 | # |
|
93 | # | |
94 | # # A permission that can be given to registered users only |
|
94 | # # A permission that can be given to registered users only | |
95 | # permission :say_hello, { :example => :say_hello }, :require => loggedin |
|
95 | # permission :say_hello, { :example => :say_hello }, :require => loggedin | |
96 | # |
|
96 | # | |
97 | # # A permission that can be given to project members only |
|
97 | # # A permission that can be given to project members only | |
98 | # permission :say_hello, { :example => :say_hello }, :require => member |
|
98 | # permission :say_hello, { :example => :say_hello }, :require => member | |
99 | def permission(name, actions, options = {}) |
|
99 | def permission(name, actions, options = {}) | |
100 | if @project_module |
|
100 | if @project_module | |
101 | Redmine::AccessControl.map {|map| map.project_module(@project_module) {|map|map.permission(name, actions, options)}} |
|
101 | Redmine::AccessControl.map {|map| map.project_module(@project_module) {|map|map.permission(name, actions, options)}} | |
102 | else |
|
102 | else | |
103 | Redmine::AccessControl.map {|map| map.permission(name, actions, options)} |
|
103 | Redmine::AccessControl.map {|map| map.permission(name, actions, options)} | |
104 | end |
|
104 | end | |
105 | end |
|
105 | end | |
106 |
|
106 | |||
107 | # Defines a project module, that can be enabled/disabled for each project. |
|
107 | # Defines a project module, that can be enabled/disabled for each project. | |
108 | # Permissions defined inside +block+ will be bind to the module. |
|
108 | # Permissions defined inside +block+ will be bind to the module. | |
109 | # |
|
109 | # | |
110 | # project_module :things do |
|
110 | # project_module :things do | |
111 | # permission :view_contacts, { :contacts => [:list, :show] }, :public => true |
|
111 | # permission :view_contacts, { :contacts => [:list, :show] }, :public => true | |
112 | # permission :destroy_contacts, { :contacts => :destroy } |
|
112 | # permission :destroy_contacts, { :contacts => :destroy } | |
113 | # end |
|
113 | # end | |
114 | def project_module(name, &block) |
|
114 | def project_module(name, &block) | |
115 | @project_module = name |
|
115 | @project_module = name | |
116 | self.instance_eval(&block) |
|
116 | self.instance_eval(&block) | |
117 | @project_module = nil |
|
117 | @project_module = nil | |
118 | end |
|
118 | end | |
|
119 | ||||
|
120 | # Registers an activity provider. | |||
|
121 | # | |||
|
122 | # Options: | |||
|
123 | # * <tt>:class_name</tt> - one or more model(s) that provide these events (inferred from event_type by default) | |||
|
124 | # * <tt>:default</tt> - setting this option to false will make the events not displayed by default | |||
|
125 | # | |||
|
126 | # A model can provide several activity event types. | |||
|
127 | # | |||
|
128 | # Examples: | |||
|
129 | # register :news | |||
|
130 | # register :scrums, :class_name => 'Meeting' | |||
|
131 | # register :issues, :class_name => ['Issue', 'Journal'] | |||
|
132 | # | |||
|
133 | # Retrieving events: | |||
|
134 | # Associated model(s) must implement the find_events class method. | |||
|
135 | # ActiveRecord models can use acts_as_activity_provider as a way to implement this class method. | |||
|
136 | # | |||
|
137 | # The following call should return all the scrum events visible by current user that occured in the 5 last days: | |||
|
138 | # Meeting.find_events('scrums', User.current, 5.days.ago, Date.today) | |||
|
139 | # Meeting.find_events('scrums', User.current, 5.days.ago, Date.today, :project => foo) # events for project foo only | |||
|
140 | # | |||
|
141 | # Note that :view_scrums permission is required to view these events in the activity view. | |||
|
142 | def activity_provider(*args) | |||
|
143 | Redmine::Activity.register(*args) | |||
|
144 | end | |||
119 |
|
145 | |||
120 | # Returns +true+ if the plugin can be configured. |
|
146 | # Returns +true+ if the plugin can be configured. | |
121 | def configurable? |
|
147 | def configurable? | |
122 | settings && settings.is_a?(Hash) && !settings[:partial].blank? |
|
148 | settings && settings.is_a?(Hash) && !settings[:partial].blank? | |
123 | end |
|
149 | end | |
124 | end |
|
150 | end | |
125 | end |
|
151 | end |
General Comments 0
You need to be logged in to leave comments.
Login now