You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

acts_as_activity_provider.rb 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2020 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. module Redmine
  19. module Acts
  20. module ActivityProvider
  21. def self.included(base)
  22. base.extend ClassMethods
  23. end
  24. module ClassMethods
  25. def acts_as_activity_provider(options = {})
  26. unless self.included_modules.include?(Redmine::Acts::ActivityProvider::InstanceMethods)
  27. cattr_accessor :activity_provider_options
  28. send :include, Redmine::Acts::ActivityProvider::InstanceMethods
  29. end
  30. options.assert_valid_keys(:type, :permission, :timestamp, :author_key, :scope)
  31. self.activity_provider_options ||= {}
  32. # One model can provide different event types
  33. # We store these options in activity_provider_options hash
  34. event_type = options.delete(:type) || self.name.underscore.pluralize
  35. options[:timestamp] ||= "#{table_name}.created_on"
  36. options[:author_key] = "#{table_name}.#{options[:author_key]}" if options[:author_key].is_a?(Symbol)
  37. self.activity_provider_options[event_type] = options
  38. end
  39. end
  40. module InstanceMethods
  41. def self.included(base)
  42. base.extend ClassMethods
  43. end
  44. module ClassMethods
  45. # Returns events of type event_type visible by user that occurred between from and to
  46. def find_events(event_type, user, from, to, options)
  47. provider_options = activity_provider_options[event_type]
  48. raise "#{self.name} can not provide #{event_type} events." if provider_options.nil?
  49. scope = provider_options[:scope]
  50. if !scope
  51. scope = self
  52. elsif scope.respond_to?(:call)
  53. scope = scope.call
  54. else
  55. ActiveSupport::Deprecation.warn "acts_as_activity_provider with implicit :scope option is deprecated. Please pass a scope on the #{self.name} as a proc."
  56. end
  57. if from && to
  58. scope = scope.where("#{provider_options[:timestamp]} BETWEEN ? AND ?", from, to)
  59. end
  60. if options[:author]
  61. return [] if provider_options[:author_key].nil?
  62. scope = scope.where("#{provider_options[:author_key]} = ?", options[:author].id)
  63. end
  64. if options[:limit]
  65. # id and creation time should be in same order in most cases
  66. scope = scope.reorder("#{table_name}.id DESC").limit(options[:limit])
  67. end
  68. if provider_options.has_key?(:permission)
  69. scope = scope.where(Project.allowed_to_condition(user, provider_options[:permission] || :view_project, options))
  70. elsif respond_to?(:visible)
  71. scope = scope.visible(user, options)
  72. else
  73. 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."
  74. scope = scope.where(Project.allowed_to_condition(user, "view_#{self.name.underscore.pluralize}".to_sym, options))
  75. end
  76. scope.to_a
  77. end
  78. end
  79. end
  80. end
  81. end
  82. end