Skip to content

Commit

Permalink
chore: refactor Sample::Query to have a single results method with a …
Browse files Browse the repository at this point in the history
…type option to select between ransack and searchkick
  • Loading branch information
ericenns committed Dec 12, 2024
1 parent 55dfc38 commit 2929154
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
4 changes: 2 additions & 2 deletions app/controllers/groups/samples_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/projects/samples_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
19 changes: 17 additions & 2 deletions app/models/sample/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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?

Expand All @@ -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,
Expand Down

0 comments on commit 2929154

Please sign in to comment.