annotate .svn/pristine/e7/e7261db0635c323b685102f3edd749f32ecf6146.svn-base @ 1516:b450a9d58aed redmine-2.4

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