annotate lib/redmine/activity/fetcher.rb @ 1478:5ca1f4a47171 bibplugin_db_migrations

Close obsolete branch bibplugin_db_migrations
author Chris Cannam
date Fri, 30 Nov 2012 14:40:50 +0000
parents cbb26bc654de
children 433d4f72a19b
rev   line source
Chris@0 1 # Redmine - project management software
Chris@909 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
Chris@0 3 #
Chris@0 4 # This program is free software; you can redistribute it and/or
Chris@0 5 # modify it under the terms of the GNU General Public License
Chris@0 6 # as published by the Free Software Foundation; either version 2
Chris@0 7 # of the License, or (at your option) any later version.
Chris@909 8 #
Chris@0 9 # This program is distributed in the hope that it will be useful,
Chris@0 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 12 # GNU General Public License for more details.
Chris@909 13 #
Chris@0 14 # You should have received a copy of the GNU General Public License
Chris@0 15 # along with this program; if not, write to the Free Software
Chris@0 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 17
Chris@0 18 module Redmine
Chris@0 19 module Activity
Chris@0 20 # Class used to retrieve activity events
Chris@0 21 class Fetcher
Chris@0 22 attr_reader :user, :project, :scope
Chris@909 23
Chris@0 24 # Needs to be unloaded in development mode
Chris@0 25 @@constantized_providers = Hash.new {|h,k| h[k] = Redmine::Activity.providers[k].collect {|t| t.constantize } }
Chris@909 26
Chris@0 27 def initialize(user, options={})
Chris@0 28 options.assert_valid_keys(:project, :with_subprojects, :author)
Chris@0 29 @user = user
Chris@0 30 @project = options[:project]
Chris@0 31 @options = options
Chris@909 32
Chris@0 33 @scope = event_types
Chris@0 34 end
Chris@909 35
Chris@0 36 # Returns an array of available event types
Chris@0 37 def event_types
Chris@0 38 return @event_types unless @event_types.nil?
Chris@909 39
Chris@0 40 @event_types = Redmine::Activity.available_event_types
Chris@0 41 @event_types = @event_types.select {|o| @project.self_and_descendants.detect {|p| @user.allowed_to?("view_#{o}".to_sym, p)}} if @project
Chris@0 42 @event_types
Chris@0 43 end
Chris@909 44
Chris@0 45 # Yields to filter the activity scope
Chris@0 46 def scope_select(&block)
Chris@0 47 @scope = @scope.select {|t| yield t }
Chris@0 48 end
Chris@909 49
Chris@0 50 # Sets the scope
Chris@0 51 # Argument can be :all, :default or an array of event types
Chris@0 52 def scope=(s)
Chris@0 53 case s
Chris@0 54 when :all
Chris@0 55 @scope = event_types
Chris@0 56 when :default
Chris@0 57 default_scope!
Chris@0 58 else
Chris@0 59 @scope = s & event_types
Chris@0 60 end
Chris@0 61 end
Chris@909 62
Chris@0 63 # Resets the scope to the default scope
Chris@0 64 def default_scope!
Chris@0 65 @scope = Redmine::Activity.default_event_types
Chris@0 66 end
Chris@909 67
Chris@0 68 # Returns an array of events for the given date range
Chris@0 69 # sorted in reverse chronological order
Chris@0 70 def events(from = nil, to = nil, options={})
Chris@0 71 e = []
Chris@0 72 @options[:limit] = options[:limit]
Chris@909 73
Chris@0 74 @scope.each do |event_type|
Chris@0 75 constantized_providers(event_type).each do |provider|
Chris@0 76 e += provider.find_events(event_type, @user, from, to, @options)
Chris@0 77 end
Chris@0 78 end
Chris@909 79
Chris@0 80 e.sort! {|a,b| b.event_datetime <=> a.event_datetime}
Chris@909 81
Chris@0 82 if options[:limit]
Chris@0 83 e = e.slice(0, options[:limit])
Chris@0 84 end
Chris@0 85 e
Chris@0 86 end
Chris@909 87
Chris@0 88 private
Chris@909 89
Chris@0 90 def constantized_providers(event_type)
Chris@0 91 @@constantized_providers[event_type]
Chris@0 92 end
Chris@0 93 end
Chris@0 94 end
Chris@0 95 end