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,