From 2929154a293cfc843b5253220c7c8cdaebd2d8fb Mon Sep 17 00:00:00 2001 From: Eric Enns <492127+ericenns@users.noreply.github.com> Date: Thu, 12 Dec 2024 07:14:06 -0600 Subject: [PATCH] chore: refactor Sample::Query to have a single results method with a type option to select between ransack and searchkick --- app/controllers/groups/samples_controller.rb | 4 ++-- .../projects/samples_controller.rb | 4 ++-- app/models/sample/query.rb | 19 +++++++++++++++++-- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/app/controllers/groups/samples_controller.rb b/app/controllers/groups/samples_controller.rb index a57127321b..d005e3c087 100644 --- a/app/controllers/groups/samples_controller.rb +++ b/app/controllers/groups/samples_controller.rb @@ -11,7 +11,7 @@ class SamplesController < Groups::ApplicationController def index @timestamp = DateTime.current - @pagy, @samples = pagy_searchkick(@query.searchkick_pagy_results, limit: params[:limit] || 20) + @pagy, @samples = pagy_searchkick(@query.results(:searchkick_pagy), limit: params[:limit] || 20) @has_samples = authorized_samples.count.positive? end @@ -26,7 +26,7 @@ def select respond_to do |format| format.turbo_stream do if params[:select].present? - @sample_ids = @query.searchkick_results + @sample_ids = @query.results(:searchkick) .where(updated_at: ..params[:timestamp].to_datetime) .select(:id).pluck(:id) end diff --git a/app/controllers/projects/samples_controller.rb b/app/controllers/projects/samples_controller.rb index 1927cd8eca..b7cb6abc06 100644 --- a/app/controllers/projects/samples_controller.rb +++ b/app/controllers/projects/samples_controller.rb @@ -13,7 +13,7 @@ class SamplesController < Projects::ApplicationController # rubocop:disable Metr def index @timestamp = DateTime.current - @pagy, @samples = pagy_searchkick(@query.searchkick_pagy_results, limit: params[:limit] || 20) + @pagy, @samples = pagy_searchkick(@query.results(:searchkick_pagy), limit: params[:limit] || 20) @has_samples = @project.samples.size.positive? end @@ -83,7 +83,7 @@ def select respond_to do |format| format.turbo_stream do if params[:select].present? - @sample_ids = @query.searchkick_results + @sample_ids = @query.results(:searchkick) .where(updated_at: ..params[:timestamp].to_datetime) .select(:id).pluck(:id) end diff --git a/app/models/sample/query.rb b/app/models/sample/query.rb index a613700b84..a95dd19a82 100644 --- a/app/models/sample/query.rb +++ b/app/models/sample/query.rb @@ -5,6 +5,8 @@ class Sample::Query # rubocop:disable Style/ClassAndModuleChildren include ActiveModel::Model include ActiveModel::Attributes + ResultTypeError = Class.new(StandardError) + attribute :column, :string attribute :direction, :string attribute :name_or_puid_cont, :string @@ -27,6 +29,21 @@ def sort=(value) assign_attributes(column:, direction:) end + def results(type = :ransack) + case type + when :ransack + ransack_results + when :searchkick + searchkick_results + when :searchkick_pagy + searchkick_pagy_results + else + raise ResultTypeError, "Unrecognized type: #{type}" + end + end + + private + def ransack_results return Sample.none unless valid? @@ -45,8 +62,6 @@ def searchkick_results Sample.search(name_or_puid_cont.presence || '*', **searchkick_kwargs) end - private - def searchkick_kwargs { fields: [{ name: :text_middle }, { puid: :text_middle }], misspellings: false,