Chris@1295
|
1 # Redmine - project management software
|
Chris@1295
|
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
|
Chris@1295
|
3 #
|
Chris@1295
|
4 # This program is free software; you can redistribute it and/or
|
Chris@1295
|
5 # modify it under the terms of the GNU General Public License
|
Chris@1295
|
6 # as published by the Free Software Foundation; either version 2
|
Chris@1295
|
7 # of the License, or (at your option) any later version.
|
Chris@1295
|
8 #
|
Chris@1295
|
9 # This program is distributed in the hope that it will be useful,
|
Chris@1295
|
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@1295
|
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@1295
|
12 # GNU General Public License for more details.
|
Chris@1295
|
13 #
|
Chris@1295
|
14 # You should have received a copy of the GNU General Public License
|
Chris@1295
|
15 # along with this program; if not, write to the Free Software
|
Chris@1295
|
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
Chris@1295
|
17
|
Chris@1295
|
18 module Redmine
|
Chris@1295
|
19 module Acts
|
Chris@1295
|
20 module ActivityProvider
|
Chris@1295
|
21 def self.included(base)
|
Chris@1295
|
22 base.extend ClassMethods
|
Chris@1295
|
23 end
|
Chris@1295
|
24
|
Chris@1295
|
25 module ClassMethods
|
Chris@1295
|
26 def acts_as_activity_provider(options = {})
|
Chris@1295
|
27 unless self.included_modules.include?(Redmine::Acts::ActivityProvider::InstanceMethods)
|
Chris@1295
|
28 cattr_accessor :activity_provider_options
|
Chris@1295
|
29 send :include, Redmine::Acts::ActivityProvider::InstanceMethods
|
Chris@1295
|
30 end
|
Chris@1295
|
31
|
Chris@1295
|
32 options.assert_valid_keys(:type, :permission, :timestamp, :author_key, :find_options)
|
Chris@1295
|
33 self.activity_provider_options ||= {}
|
Chris@1295
|
34
|
Chris@1295
|
35 # One model can provide different event types
|
Chris@1295
|
36 # We store these options in activity_provider_options hash
|
Chris@1295
|
37 event_type = options.delete(:type) || self.name.underscore.pluralize
|
Chris@1295
|
38
|
Chris@1295
|
39 options[:timestamp] ||= "#{table_name}.created_on"
|
Chris@1295
|
40 options[:find_options] ||= {}
|
Chris@1295
|
41 options[:author_key] = "#{table_name}.#{options[:author_key]}" if options[:author_key].is_a?(Symbol)
|
Chris@1295
|
42 self.activity_provider_options[event_type] = options
|
Chris@1295
|
43 end
|
Chris@1295
|
44 end
|
Chris@1295
|
45
|
Chris@1295
|
46 module InstanceMethods
|
Chris@1295
|
47 def self.included(base)
|
Chris@1295
|
48 base.extend ClassMethods
|
Chris@1295
|
49 end
|
Chris@1295
|
50
|
Chris@1295
|
51 module ClassMethods
|
Chris@1295
|
52 # Returns events of type event_type visible by user that occured between from and to
|
Chris@1295
|
53 def find_events(event_type, user, from, to, options)
|
Chris@1295
|
54 provider_options = activity_provider_options[event_type]
|
Chris@1295
|
55 raise "#{self.name} can not provide #{event_type} events." if provider_options.nil?
|
Chris@1295
|
56
|
Chris@1295
|
57 scope = self
|
Chris@1295
|
58
|
Chris@1295
|
59 if from && to
|
Chris@1295
|
60 scope = scope.scoped(:conditions => ["#{provider_options[:timestamp]} BETWEEN ? AND ?", from, to])
|
Chris@1295
|
61 end
|
Chris@1295
|
62
|
Chris@1295
|
63 if options[:author]
|
Chris@1295
|
64 return [] if provider_options[:author_key].nil?
|
Chris@1295
|
65 scope = scope.scoped(:conditions => ["#{provider_options[:author_key]} = ?", options[:author].id])
|
Chris@1295
|
66 end
|
Chris@1295
|
67
|
Chris@1295
|
68 if options[:limit]
|
Chris@1295
|
69 # id and creation time should be in same order in most cases
|
Chris@1295
|
70 scope = scope.scoped(:order => "#{table_name}.id DESC", :limit => options[:limit])
|
Chris@1295
|
71 end
|
Chris@1295
|
72
|
Chris@1295
|
73 if provider_options.has_key?(:permission)
|
Chris@1295
|
74 scope = scope.scoped(:conditions => Project.allowed_to_condition(user, provider_options[:permission] || :view_project, options))
|
Chris@1295
|
75 elsif respond_to?(:visible)
|
Chris@1295
|
76 scope = scope.visible(user, options)
|
Chris@1295
|
77 else
|
Chris@1295
|
78 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@1295
|
79 scope = scope.scoped(:conditions => Project.allowed_to_condition(user, "view_#{self.name.underscore.pluralize}".to_sym, options))
|
Chris@1295
|
80 end
|
Chris@1295
|
81
|
Chris@1295
|
82 scope.all(provider_options[:find_options].dup)
|
Chris@1295
|
83 end
|
Chris@1295
|
84 end
|
Chris@1295
|
85 end
|
Chris@1295
|
86 end
|
Chris@1295
|
87 end
|
Chris@1295
|
88 end
|