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