Skip to content

Commit

Permalink
refactor logic so pagy results are returned from query model
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisHuynh333 committed Dec 18, 2024
1 parent 072e3fb commit fc747e9
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 37 deletions.
21 changes: 0 additions & 21 deletions app/controllers/concerns/samples_query.rb

This file was deleted.

8 changes: 5 additions & 3 deletions app/controllers/groups/samples_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ module Groups
class SamplesController < Groups::ApplicationController
include Metadata
include Storable
include SamplesQuery

before_action :group, :current_page
before_action :query, only: %i[index search select]

def index
@timestamp = DateTime.current
@pagy, @samples = pagy_for_samples_query
@pagy, @samples = @query.results(action: 'index', limit: params[:limit] || 20, page: params[:page] || 1)
@has_samples = authorized_samples.count.positive?
end

Expand All @@ -26,7 +25,10 @@ def select

respond_to do |format|
format.turbo_stream do
@sample_ids = select_query if params[:select].present?
if params[:select].present?
@sample_ids = @query.results(action: 'select')
.where(updated_at: ..params[:timestamp].to_datetime).select(:id).pluck(:id)
end
end
end
end
Expand Down
8 changes: 5 additions & 3 deletions app/controllers/projects/samples_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ class SamplesController < Projects::ApplicationController # rubocop:disable Metr
include Metadata
include ListActions
include Storable
include SamplesQuery

before_action :sample, only: %i[show edit update view_history_version]
before_action :current_page
before_action :query, only: %i[index search select]

def index
@timestamp = DateTime.current
@pagy, @samples = pagy_for_samples_query
@pagy, @samples = @query.results(action: 'index', limit: params[:limit] || 20, page: params[:page] || 1)
@has_samples = @project.samples.size.positive?
end

Expand Down Expand Up @@ -83,7 +82,10 @@ def select

respond_to do |format|
format.turbo_stream do
@sample_ids = select_query if params[:select].present?
if params[:select].present?
@sample_ids = @query.results(action: 'select')
.where(updated_at: ..params[:timestamp].to_datetime).select(:id).pluck(:id)
end
end
end
end
Expand Down
29 changes: 19 additions & 10 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 Down Expand Up @@ -33,28 +34,36 @@ def sort=(value)

def advanced_query?
# simplified version, will be further implemented when we have the definition of an advanced query
false
true
end

def results(type = :ransack)
case type
when :ransack
ransack_results
when :searchkick
searchkick_results
when :searchkick_pagy
searchkick_pagy_results
def results(params)
case params[:action]
when 'index'
pagy_results(params[:limit], params[:page])
when 'select'
advanced_query ? searchkick_results : ransack_results
else
raise ResultTypeError, "Unrecognized type: #{type}"
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?

sort_samples.ransack(ransack_params).result
samples = sort_samples
samples.ransack(ransack_params).result
# sort_samples.ransack(ransack_params).result
end

def searchkick_pagy_results
Expand Down

0 comments on commit fc747e9

Please sign in to comment.