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