Module: Redmine::Acts::ActivityProvider::InstanceMethods::ClassMethods

Defined in:
lib/plugins/acts_as_activity_provider/lib/acts_as_activity_provider.rb

Instance Method Summary collapse

Instance Method Details

#find_events(event_type, user, from, to, options) ⇒ Object

Returns events of type event_type visible by user that occurred between from and to



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/plugins/acts_as_activity_provider/lib/acts_as_activity_provider.rb', line 54

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 = provider_options[:scope]
  if !scope
    scope = self
  elsif scope.respond_to?(:call)
    scope = scope.call
  else
    Rails.application.deprecators[:redmine].warn "acts_as_activity_provider with implicit :scope option is deprecated. Please pass a scope on the #{self.name} as a proc."
  end

  scope = scope.where("#{provider_options[:timestamp]} >= ?", from) if from
  scope = scope.where("#{provider_options[:timestamp]} <= ?", to) if to

  if options[:author]
    return [] if provider_options[:author_key].nil?
    scope = scope.where("#{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.reorder("#{table_name}.id DESC").limit(options[:limit])
  end

  if provider_options.has_key?(:permission)
    scope = scope.where(Project.allowed_to_condition(user, provider_options[:permission] || :view_project, options))
  elsif respond_to?(:visible)
    scope = scope.visible(user, options)
  else
    Rails.application.deprecators[:redmine].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.where(Project.allowed_to_condition(user, "view_#{self.name.underscore.pluralize}".to_sym, options))
  end

  if options[:last_by_project]
    scope = scope.group("#{Project.table_name}.id").maximum(provider_options[:timestamp])
  end

  scope.to_a
end