Skip to content

Commit

Permalink
Merge pull request #619 from codeforjapan/search-with-pg_bigm2
Browse files Browse the repository at this point in the history
検索対象をpublic_spacesのみにするよう修正
  • Loading branch information
ayuki-joto authored Jul 28, 2024
2 parents a69942d + ee7b215 commit fb7e58d
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 2 deletions.
99 changes: 99 additions & 0 deletions app/commands/decidim/search.rb
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
4 changes: 2 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2024_01_28_143444) do
ActiveRecord::Schema.define(version: 2024_07_13_180919) do

# These are extensions that must be enabled in order to support this database
enable_extension "ltree"
enable_extension "pg_trgm"
enable_extension "pg_bigm"
enable_extension "plpgsql"

create_table "active_storage_attachments", force: :cascade do |t|
Expand Down

0 comments on commit fb7e58d

Please sign in to comment.