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