@@ -1,83 +1,93 | |||||
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 Acts |
|
19 | module Acts | |
20 | module ActivityProvider |
|
20 | module ActivityProvider | |
21 | def self.included(base) |
|
21 | def self.included(base) | |
22 | base.extend ClassMethods |
|
22 | base.extend ClassMethods | |
23 | end |
|
23 | end | |
24 |
|
24 | |||
25 | module ClassMethods |
|
25 | module ClassMethods | |
26 | def acts_as_activity_provider(options = {}) |
|
26 | def acts_as_activity_provider(options = {}) | |
27 | unless self.included_modules.include?(Redmine::Acts::ActivityProvider::InstanceMethods) |
|
27 | unless self.included_modules.include?(Redmine::Acts::ActivityProvider::InstanceMethods) | |
28 | cattr_accessor :activity_provider_options |
|
28 | cattr_accessor :activity_provider_options | |
29 | send :include, Redmine::Acts::ActivityProvider::InstanceMethods |
|
29 | send :include, Redmine::Acts::ActivityProvider::InstanceMethods | |
30 | end |
|
30 | end | |
31 |
|
31 | |||
32 | options.assert_valid_keys(:type, :permission, :timestamp, :author_key, :find_options) |
|
32 | options.assert_valid_keys(:type, :permission, :timestamp, :author_key, :find_options) | |
33 | self.activity_provider_options ||= {} |
|
33 | self.activity_provider_options ||= {} | |
34 |
|
34 | |||
35 | # One model can provide different event types |
|
35 | # One model can provide different event types | |
36 | # We store these options in activity_provider_options hash |
|
36 | # We store these options in activity_provider_options hash | |
37 | event_type = options.delete(:type) || self.name.underscore.pluralize |
|
37 | event_type = options.delete(:type) || self.name.underscore.pluralize | |
38 |
|
38 | |||
39 | options[:permission] = "view_#{self.name.underscore.pluralize}".to_sym unless options.has_key?(:permission) |
|
|||
40 | options[:timestamp] ||= "#{table_name}.created_on" |
|
39 | options[:timestamp] ||= "#{table_name}.created_on" | |
41 | options[:find_options] ||= {} |
|
40 | options[:find_options] ||= {} | |
42 | options[:author_key] = "#{table_name}.#{options[:author_key]}" if options[:author_key].is_a?(Symbol) |
|
41 | options[:author_key] = "#{table_name}.#{options[:author_key]}" if options[:author_key].is_a?(Symbol) | |
43 | self.activity_provider_options[event_type] = options |
|
42 | self.activity_provider_options[event_type] = options | |
44 | end |
|
43 | end | |
45 | end |
|
44 | end | |
46 |
|
45 | |||
47 | module InstanceMethods |
|
46 | module InstanceMethods | |
48 | def self.included(base) |
|
47 | def self.included(base) | |
49 | base.extend ClassMethods |
|
48 | base.extend ClassMethods | |
50 | end |
|
49 | end | |
51 |
|
50 | |||
52 | module ClassMethods |
|
51 | module ClassMethods | |
53 | # Returns events of type event_type visible by user that occured between from and to |
|
52 | # Returns events of type event_type visible by user that occured between from and to | |
54 | def find_events(event_type, user, from, to, options) |
|
53 | def find_events(event_type, user, from, to, options) | |
55 | provider_options = activity_provider_options[event_type] |
|
54 | provider_options = activity_provider_options[event_type] | |
56 | raise "#{self.name} can not provide #{event_type} events." if provider_options.nil? |
|
55 | raise "#{self.name} can not provide #{event_type} events." if provider_options.nil? | |
57 |
|
56 | |||
58 | scope_options = {} |
|
57 | scope_options = {} | |
59 | cond = ARCondition.new |
|
58 | cond = ARCondition.new | |
60 | if from && to |
|
59 | if from && to | |
61 | cond.add(["#{provider_options[:timestamp]} BETWEEN ? AND ?", from, to]) |
|
60 | cond.add(["#{provider_options[:timestamp]} BETWEEN ? AND ?", from, to]) | |
62 | end |
|
61 | end | |
|
62 | ||||
63 | if options[:author] |
|
63 | if options[:author] | |
64 | return [] if provider_options[:author_key].nil? |
|
64 | return [] if provider_options[:author_key].nil? | |
65 | cond.add(["#{provider_options[:author_key]} = ?", options[:author].id]) |
|
65 | cond.add(["#{provider_options[:author_key]} = ?", options[:author].id]) | |
66 | end |
|
66 | end | |
67 | cond.add(Project.allowed_to_condition(user, provider_options[:permission], options)) if provider_options[:permission] |
|
67 | ||
68 | scope_options[:conditions] = cond.conditions |
|
|||
69 | if options[:limit] |
|
68 | if options[:limit] | |
70 | # id and creation time should be in same order in most cases |
|
69 | # id and creation time should be in same order in most cases | |
71 | scope_options[:order] = "#{table_name}.id DESC" |
|
70 | scope_options[:order] = "#{table_name}.id DESC" | |
72 | scope_options[:limit] = options[:limit] |
|
71 | scope_options[:limit] = options[:limit] | |
73 | end |
|
72 | end | |
74 |
|
73 | |||
|
74 | scope = self | |||
|
75 | if provider_options.has_key?(:permission) | |||
|
76 | cond.add(Project.allowed_to_condition(user, provider_options[:permission] || :view_project, options)) | |||
|
77 | elsif respond_to?(:visible) | |||
|
78 | scope = scope.visible(user, options) | |||
|
79 | else | |||
|
80 | ActiveSupport::Deprecation.warn "acts_as_activity_provider with implicit :permission option is deprecated. Add a visible scope to the #{self.name} model or use explicit :permission option." | |||
|
81 | cond.add(Project.allowed_to_condition(user, "view_#{self.name.underscore.pluralize}".to_sym, options)) | |||
|
82 | end | |||
|
83 | scope_options[:conditions] = cond.conditions | |||
|
84 | ||||
75 | with_scope(:find => scope_options) do |
|
85 | with_scope(:find => scope_options) do | |
76 | find(:all, provider_options[:find_options].dup) |
|
86 | scope.find(:all, provider_options[:find_options].dup) | |
77 | end |
|
87 | end | |
78 | end |
|
88 | end | |
79 | end |
|
89 | end | |
80 | end |
|
90 | end | |
81 | end |
|
91 | end | |
82 | end |
|
92 | end | |
83 | end |
|
93 | end |
General Comments 0
You need to be logged in to leave comments.
Login now