-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #619 from codeforjapan/search-with-pg_bigm2
検索対象をpublic_spacesのみにするよう修正
- Loading branch information
Showing
2 changed files
with
101 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
# frozen_string_literal: true | ||
|
||
module Decidim | ||
# A command that will act as a search service, with all the business logic for performing searches. | ||
class Search < Decidim::Command | ||
ACCEPTED_FILTERS = [:decidim_scope_id_in].freeze | ||
HIGHLIGHTED_RESULTS_COUNT = 4 | ||
|
||
# Public: Initializes the command. | ||
# | ||
# @param term: The term to search for. | ||
# @param organization: The Organization to which the results are constrained. | ||
# @param filters: (optional) A Hash of SearchableResource attributes to filter for. | ||
# @param page_params: (optional) A Hash with `page` and `per_page` options to paginate. | ||
def initialize(term, organization, filters = {}, page_params = {}) | ||
@term = term | ||
@organization = organization | ||
@filters = filters.with_indifferent_access | ||
@page_params = page_params | ||
end | ||
|
||
# Executes the command. Broadcasts these events: | ||
# | ||
# - :ok when everything is valid, together with the search results. | ||
# - :invalid if something failed and couldn't proceed. | ||
# | ||
# Returns nothing. | ||
def call | ||
search_results = Decidim::Searchable.searchable_resources.inject({}) do |results_by_type, (class_name, klass)| | ||
result_ids = filtered_query_for(class_name).pluck(:resource_id) | ||
results_count = result_ids.count | ||
|
||
results = if filters[:with_resource_type].present? && filters[:with_resource_type] == class_name | ||
paginate(klass.order_by_id_list(result_ids)) | ||
elsif filters[:with_resource_type].present? | ||
ApplicationRecord.none | ||
else | ||
klass.order_by_id_list(result_ids.take(HIGHLIGHTED_RESULTS_COUNT)) | ||
end | ||
|
||
results_by_type.update(class_name => { | ||
count: results_count, | ||
results: results | ||
}) | ||
end | ||
broadcast(:ok, search_results) | ||
end | ||
|
||
private | ||
|
||
attr_reader :page_params, :filters, :organization, :term | ||
|
||
def paginate(collection) | ||
return collection if page_params.blank? | ||
|
||
collection.page(page_params[:page]).per(page_params[:per_page]) | ||
end | ||
|
||
def clean_filters | ||
@clean_filters ||= filters.select do |attribute_name, value| | ||
ACCEPTED_FILTERS.include?(attribute_name.to_sym) && value.present? | ||
end.compact | ||
end | ||
|
||
def spaces_to_filter | ||
Decidim.participatory_space_manifests.flat_map do |manifest| | ||
public_spaces = manifest.participatory_spaces.call(organization).public_spaces | ||
spaces = case filters[:with_space_state] | ||
when "active" | ||
public_spaces.active_spaces | ||
when "future" | ||
public_spaces.future_spaces | ||
when "past" | ||
public_spaces.past_spaces | ||
else | ||
public_spaces | ||
end | ||
spaces.select(:id).to_a | ||
end | ||
end | ||
|
||
def filtered_query_for(class_name) | ||
query = SearchableResource.where( | ||
organization: organization, | ||
locale: I18n.locale, | ||
resource_type: class_name | ||
) | ||
|
||
if (spaces = spaces_to_filter) | ||
query = query.where(decidim_participatory_space: spaces) | ||
end | ||
query = query.ransack(clean_filters).result if clean_filters.any? | ||
|
||
query = query.order("datetime DESC") | ||
query = query.global_search(I18n.transliterate(term)) if term.present? | ||
query | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters