##// END OF EJS Templates
Code cleanup....
Jean-Philippe Lang -
r11644:2e67ffe769a3
parent child
Show More
@@ -1,88 +1,88
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2013 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
19 19 module Acts
20 20 module ActivityProvider
21 21 def self.included(base)
22 22 base.extend ClassMethods
23 23 end
24 24
25 25 module ClassMethods
26 26 def acts_as_activity_provider(options = {})
27 27 unless self.included_modules.include?(Redmine::Acts::ActivityProvider::InstanceMethods)
28 28 cattr_accessor :activity_provider_options
29 29 send :include, Redmine::Acts::ActivityProvider::InstanceMethods
30 30 end
31 31
32 32 options.assert_valid_keys(:type, :permission, :timestamp, :author_key, :find_options)
33 33 self.activity_provider_options ||= {}
34 34
35 35 # One model can provide different event types
36 36 # We store these options in activity_provider_options hash
37 37 event_type = options.delete(:type) || self.name.underscore.pluralize
38 38
39 39 options[:timestamp] ||= "#{table_name}.created_on"
40 40 options[:find_options] ||= {}
41 41 options[:author_key] = "#{table_name}.#{options[:author_key]}" if options[:author_key].is_a?(Symbol)
42 42 self.activity_provider_options[event_type] = options
43 43 end
44 44 end
45 45
46 46 module InstanceMethods
47 47 def self.included(base)
48 48 base.extend ClassMethods
49 49 end
50 50
51 51 module ClassMethods
52 52 # Returns events of type event_type visible by user that occured between from and to
53 53 def find_events(event_type, user, from, to, options)
54 54 provider_options = activity_provider_options[event_type]
55 55 raise "#{self.name} can not provide #{event_type} events." if provider_options.nil?
56 56
57 57 scope = self
58 58
59 59 if from && to
60 scope = scope.scoped(:conditions => ["#{provider_options[:timestamp]} BETWEEN ? AND ?", from, to])
60 scope = scope.where("#{provider_options[:timestamp]} BETWEEN ? AND ?", from, to)
61 61 end
62 62
63 63 if options[:author]
64 64 return [] if provider_options[:author_key].nil?
65 scope = scope.scoped(:conditions => ["#{provider_options[:author_key]} = ?", options[:author].id])
65 scope = scope.where("#{provider_options[:author_key]} = ?", options[:author].id)
66 66 end
67 67
68 68 if options[:limit]
69 69 # id and creation time should be in same order in most cases
70 scope = scope.scoped(:order => "#{table_name}.id DESC", :limit => options[:limit])
70 scope = scope.reorder("#{table_name}.id DESC").limit(options[:limit])
71 71 end
72 72
73 73 if provider_options.has_key?(:permission)
74 scope = scope.scoped(:conditions => Project.allowed_to_condition(user, provider_options[:permission] || :view_project, options))
74 scope = scope.where(Project.allowed_to_condition(user, provider_options[:permission] || :view_project, options))
75 75 elsif respond_to?(:visible)
76 76 scope = scope.visible(user, options)
77 77 else
78 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."
79 scope = scope.scoped(:conditions => Project.allowed_to_condition(user, "view_#{self.name.underscore.pluralize}".to_sym, options))
79 scope = scope.where(Project.allowed_to_condition(user, "view_#{self.name.underscore.pluralize}".to_sym, options))
80 80 end
81 81
82 82 scope.all(provider_options[:find_options].dup)
83 83 end
84 84 end
85 85 end
86 86 end
87 87 end
88 88 end
General Comments 0
You need to be logged in to leave comments. Login now