annotate .svn/pristine/8e/8e4c0c24e2e10f35f263c39915b03fd65f43917a.svn-base @ 1327:287f201c2802 redmine-2.2-integration

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