summaryrefslogtreecommitdiffstats
path: root/lib/plugins/acts_as_activity_provider
diff options
context:
space:
mode:
authorJean-Philippe Lang <jp_lang@yahoo.fr>2012-04-26 17:23:24 +0000
committerJean-Philippe Lang <jp_lang@yahoo.fr>2012-04-26 17:23:24 +0000
commit8d73ddf73ffc2cefbe16ca06970377e13d4bead0 (patch)
treeecb8fbb4d8330fe936c43127f2b921ad24eec2e8 /lib/plugins/acts_as_activity_provider
parent26868d8b140406cce8c2494efbac7f8f85f452d0 (diff)
downloadredmine-8d73ddf73ffc2cefbe16ca06970377e13d4bead0.tar.gz
redmine-8d73ddf73ffc2cefbe16ca06970377e13d4bead0.zip
Moved Rails plugins required by the core to lib/plugins.
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@9533 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'lib/plugins/acts_as_activity_provider')
-rw-r--r--lib/plugins/acts_as_activity_provider/init.rb2
-rw-r--r--lib/plugins/acts_as_activity_provider/lib/acts_as_activity_provider.rb88
2 files changed, 90 insertions, 0 deletions
diff --git a/lib/plugins/acts_as_activity_provider/init.rb b/lib/plugins/acts_as_activity_provider/init.rb
new file mode 100644
index 000000000..5bf05da2c
--- /dev/null
+++ b/lib/plugins/acts_as_activity_provider/init.rb
@@ -0,0 +1,2 @@
+require File.dirname(__FILE__) + '/lib/acts_as_activity_provider'
+ActiveRecord::Base.send(:include, Redmine::Acts::ActivityProvider)
diff --git a/lib/plugins/acts_as_activity_provider/lib/acts_as_activity_provider.rb b/lib/plugins/acts_as_activity_provider/lib/acts_as_activity_provider.rb
new file mode 100644
index 000000000..2b3eda52c
--- /dev/null
+++ b/lib/plugins/acts_as_activity_provider/lib/acts_as_activity_provider.rb
@@ -0,0 +1,88 @@
+# Redmine - project management software
+# Copyright (C) 2006-2012 Jean-Philippe Lang
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+
+module Redmine
+ module Acts
+ module ActivityProvider
+ def self.included(base)
+ base.extend ClassMethods
+ end
+
+ module ClassMethods
+ def acts_as_activity_provider(options = {})
+ unless self.included_modules.include?(Redmine::Acts::ActivityProvider::InstanceMethods)
+ cattr_accessor :activity_provider_options
+ send :include, Redmine::Acts::ActivityProvider::InstanceMethods
+ end
+
+ options.assert_valid_keys(:type, :permission, :timestamp, :author_key, :find_options)
+ self.activity_provider_options ||= {}
+
+ # One model can provide different event types
+ # We store these options in activity_provider_options hash
+ event_type = options.delete(:type) || self.name.underscore.pluralize
+
+ options[:timestamp] ||= "#{table_name}.created_on"
+ options[:find_options] ||= {}
+ options[:author_key] = "#{table_name}.#{options[:author_key]}" if options[:author_key].is_a?(Symbol)
+ self.activity_provider_options[event_type] = options
+ end
+ end
+
+ module InstanceMethods
+ def self.included(base)
+ base.extend ClassMethods
+ end
+
+ module ClassMethods
+ # Returns events of type event_type visible by user that occured between from and to
+ def find_events(event_type, user, from, to, options)
+ provider_options = activity_provider_options[event_type]
+ raise "#{self.name} can not provide #{event_type} events." if provider_options.nil?
+
+ scope = self
+
+ if from && to
+ scope = scope.scoped(:conditions => ["#{provider_options[:timestamp]} BETWEEN ? AND ?", from, to])
+ end
+
+ if options[:author]
+ return [] if provider_options[:author_key].nil?
+ scope = scope.scoped(:conditions => ["#{provider_options[:author_key]} = ?", options[:author].id])
+ end
+
+ if options[:limit]
+ # id and creation time should be in same order in most cases
+ scope = scope.scoped(:order => "#{table_name}.id DESC", :limit => options[:limit])
+ end
+
+ if provider_options.has_key?(:permission)
+ scope = scope.scoped(:conditions => Project.allowed_to_condition(user, provider_options[:permission] || :view_project, options))
+ elsif respond_to?(:visible)
+ scope = scope.visible(user, options)
+ else
+ 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."
+ scope = scope.scoped(:conditions => Project.allowed_to_condition(user, "view_#{self.name.underscore.pluralize}".to_sym, options))
+ end
+
+ scope.all(provider_options[:find_options].dup)
+ end
+ end
+ end
+ end
+ end
+end