Class: Decidim::ActivitySearch

Inherits:
Searchlight::Search
  • Object
show all
Defined in:
app/services/decidim/activity_search.rb

Overview

A class to search for recent activity performed in a Decidim Organization. It is intended to be used in the user profile, to retrieve activity and timeline for that user.

It will return any action for a given resource, except ‘create` on resources that implement the `Decidim::Publicable` concern to avoid leaking private data.

Possible options:

:organization - the ‘Decidim::Organization` to scope to search. Required. :user - an optional `Decidim::USer` that performed the activites :follows - a collection of `Decidim::Follow` resources. It will return any

activity affecting any of these resources, performed by any of them or
contained in any of them as spaces.

:scopes - a collection of ‘Decidim::Scope`. It will return any activity that

took place in any of those scopes.

Instance Method Summary collapse

Instance Method Details

#base_queryObject

Needed by Searchlight, this is the base query that will be used to append other criteria to the search.



24
25
26
27
28
29
30
31
32
33
# File 'app/services/decidim/activity_search.rb', line 24

def base_query
  query = ActionLog
          .where(visibility: %w(public-only all))
          .where(organization: options.fetch(:organization))

  query = query.where(user: options[:user]) if options[:user]
  query = query.where(resource_type: options[:resource_name]) if options[:resource_name]

  filter_follows(query)
end

#resource_typesObject

All the resource types that are eligible to be included as an activity.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/services/decidim/activity_search.rb', line 51

def resource_types
  @resource_types ||= %w(
    Decidim::Accountability::Result
    Decidim::Blogs::Post
    Decidim::Comments::Comment
    Decidim::Consultations::Question
    Decidim::Debates::Debate
    Decidim::Meetings::Meeting
    Decidim::Proposals::Proposal
    Decidim::Surveys::Survey
    Decidim::Assembly
    Decidim::Consultation
    Decidim::Initiative
    Decidim::ParticipatoryProcess
  ).select do |klass|
    klass.safe_constantize.present?
  end
end

#runObject

Overwrites the default Searchlight run method since we want to return activities in an specific order but we need to set it at the end of the chain.



37
38
39
# File 'app/services/decidim/activity_search.rb', line 37

def run
  super.order(created_at: :desc)
end

#search_resource_typeObject

Adds a constrain to filter by resource type(s).



42
43
44
45
46
47
48
# File 'app/services/decidim/activity_search.rb', line 42

def search_resource_type
  if resource_types.include?(resource_type)
    scope_for(resource_type)
  else
    all_resources_scope
  end
end