Chris@909: # redMine - project management software Chris@909: # Copyright (C) 2006-2008 Jean-Philippe Lang Chris@909: # Chris@909: # This program is free software; you can redistribute it and/or Chris@909: # modify it under the terms of the GNU General Public License Chris@909: # as published by the Free Software Foundation; either version 2 Chris@909: # of the License, or (at your option) any later version. Chris@909: # Chris@909: # This program is distributed in the hope that it will be useful, Chris@909: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@909: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@909: # GNU General Public License for more details. Chris@909: # Chris@909: # You should have received a copy of the GNU General Public License Chris@909: # along with this program; if not, write to the Free Software Chris@909: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@909: Chris@909: module Redmine Chris@909: module Acts Chris@909: module ActivityProvider Chris@909: def self.included(base) Chris@909: base.extend ClassMethods Chris@909: end Chris@909: Chris@909: module ClassMethods Chris@909: def acts_as_activity_provider(options = {}) Chris@909: unless self.included_modules.include?(Redmine::Acts::ActivityProvider::InstanceMethods) Chris@909: cattr_accessor :activity_provider_options Chris@909: send :include, Redmine::Acts::ActivityProvider::InstanceMethods Chris@909: end Chris@909: Chris@909: options.assert_valid_keys(:type, :permission, :timestamp, :author_key, :find_options) Chris@909: self.activity_provider_options ||= {} Chris@909: Chris@909: # One model can provide different event types Chris@909: # We store these options in activity_provider_options hash Chris@909: event_type = options.delete(:type) || self.name.underscore.pluralize Chris@909: Chris@909: options[:timestamp] ||= "#{table_name}.created_on" Chris@909: options[:find_options] ||= {} Chris@909: options[:author_key] = "#{table_name}.#{options[:author_key]}" if options[:author_key].is_a?(Symbol) Chris@909: self.activity_provider_options[event_type] = options Chris@909: end Chris@909: end Chris@909: Chris@909: module InstanceMethods Chris@909: def self.included(base) Chris@909: base.extend ClassMethods Chris@909: end Chris@909: Chris@909: module ClassMethods Chris@909: # Returns events of type event_type visible by user that occured between from and to Chris@909: def find_events(event_type, user, from, to, options) Chris@909: provider_options = activity_provider_options[event_type] Chris@909: raise "#{self.name} can not provide #{event_type} events." if provider_options.nil? Chris@909: Chris@909: scope_options = {} Chris@909: cond = ARCondition.new Chris@909: if from && to Chris@909: cond.add(["#{provider_options[:timestamp]} BETWEEN ? AND ?", from, to]) Chris@909: end Chris@909: Chris@909: if options[:author] Chris@909: return [] if provider_options[:author_key].nil? Chris@909: cond.add(["#{provider_options[:author_key]} = ?", options[:author].id]) Chris@909: end Chris@909: Chris@909: if options[:limit] Chris@909: # id and creation time should be in same order in most cases Chris@909: scope_options[:order] = "#{table_name}.id DESC" Chris@909: scope_options[:limit] = options[:limit] Chris@909: end Chris@909: Chris@909: scope = self Chris@909: if provider_options.has_key?(:permission) Chris@909: cond.add(Project.allowed_to_condition(user, provider_options[:permission] || :view_project, options)) Chris@909: elsif respond_to?(:visible) Chris@909: scope = scope.visible(user, options) Chris@909: else Chris@909: 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@909: cond.add(Project.allowed_to_condition(user, "view_#{self.name.underscore.pluralize}".to_sym, options)) Chris@909: end Chris@909: scope_options[:conditions] = cond.conditions Chris@909: Chris@909: with_scope(:find => scope_options) do Chris@909: scope.find(:all, provider_options[:find_options].dup) Chris@909: end Chris@909: end Chris@909: end Chris@909: end Chris@909: end Chris@909: end Chris@909: end