Skip to content

Commit

Permalink
Samples Table: Determine query type (#873)
Browse files Browse the repository at this point in the history
* add advanced_query?

* move logic of advanced_query

* add pagy query to a concern, change select to use ransack

* fix comment

* move select query to concern

* add missed include to groups samples controller

* cleanup comments in samples query

* change select to use ransack or searchkick dependent on advanced_query

* refactor logic so pagy results are returned from query model

* undo testing change

* fix fallback in query.results

* move limit and page default values to query model

* rename action to pagy_query

* rename action to query

* utilize kwargs for query.results
  • Loading branch information
ChrisHuynh333 authored Dec 20, 2024
1 parent 13ecb2f commit 5ff9e2d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
6 changes: 2 additions & 4 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.results(:searchkick_pagy), limit: params[:limit] || 20)
@pagy, @samples = @query.results(limit: params[:limit] || 20, page: params[:page] || 1)
@has_samples = authorized_samples.count.positive?
end

Expand All @@ -26,9 +26,7 @@ def select
respond_to do |format|
format.turbo_stream do
if params[:select].present?
@sample_ids = @query.results(:searchkick)
.where(updated_at: ..params[:timestamp].to_datetime)
.select(:id).pluck(:id)
@sample_ids = @query.results.where(updated_at: ..params[:timestamp].to_datetime).select(:id).pluck(:id)
end
end
end
Expand Down
6 changes: 2 additions & 4 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.results(:searchkick_pagy), limit: params[:limit] || 20)
@pagy, @samples = @query.results(limit: params[:limit] || 20, page: params[:page] || 1)
@has_samples = @project.samples.size.positive?
end

Expand Down Expand Up @@ -83,9 +83,7 @@ def select
respond_to do |format|
format.turbo_stream do
if params[:select].present?
@sample_ids = @query.results(:searchkick)
.where(updated_at: ..params[:timestamp].to_datetime)
.select(:id).pluck(:id)
@sample_ids = @query.results.where(updated_at: ..params[:timestamp].to_datetime).select(:id).pluck(:id)
end
end
end
Expand Down
29 changes: 20 additions & 9 deletions app/models/sample/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
class Sample::Query # rubocop:disable Style/ClassAndModuleChildren
include ActiveModel::Model
include ActiveModel::Attributes
include Pagy::Backend

ResultTypeError = Class.new(StandardError)

Expand All @@ -13,13 +14,15 @@ class Sample::Query # rubocop:disable Style/ClassAndModuleChildren
attribute :name_or_puid_in, default: -> { [] }
attribute :project_ids, default: -> { [] }
attribute :sort, :string, default: 'updated_at desc'
attribute :advanced_query, :boolean, default: false

validates :direction, inclusion: { in: %w[asc desc] }
validates :project_ids, length: { minimum: 1 }

def initialize(...)
super
self.sort = sort
self.advanced_query = advanced_query?
end

def sort=(value)
Expand All @@ -29,21 +32,29 @@ 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
def advanced_query?
# simplified version, will be further implemented when we have the definition of an advanced query
false
end

def results(**results_arguments)
if results_arguments[:limit] || results_arguments[:page]
pagy_results(results_arguments[:limit], results_arguments[:page])
else
raise ResultTypeError, "Unrecognized type: #{type}"
advanced_query ? searchkick_results : ransack_results
end
end

private

def pagy_results(limit, page)
if advanced_query
pagy_searchkick(searchkick_pagy_results, limit:, page:)
else
pagy(ransack_results, limit:, page:)
end
end

def ransack_results
return Sample.none unless valid?

Expand Down

0 comments on commit 5ff9e2d

Please sign in to comment.