From fc747e9ae24ec90bc446c0a8b1889b5b4b41f809 Mon Sep 17 00:00:00 2001 From: Chris Huynh Date: Wed, 18 Dec 2024 15:07:44 -0600 Subject: [PATCH] refactor logic so pagy results are returned from query model --- app/controllers/concerns/samples_query.rb | 21 -------------- app/controllers/groups/samples_controller.rb | 8 +++-- .../projects/samples_controller.rb | 8 +++-- app/models/sample/query.rb | 29 ++++++++++++------- 4 files changed, 29 insertions(+), 37 deletions(-) delete mode 100644 app/controllers/concerns/samples_query.rb diff --git a/app/controllers/concerns/samples_query.rb b/app/controllers/concerns/samples_query.rb deleted file mode 100644 index a38ac731c8..0000000000 --- a/app/controllers/concerns/samples_query.rb +++ /dev/null @@ -1,21 +0,0 @@ -# frozen_string_literal: true - -# Queries for samples table -module SamplesQuery - extend ActiveSupport::Concern - - # Determines which pagy method to use when loading the samples table based on @query - def pagy_for_samples_query - limit = params[:limit] || 20 - if @query.advanced_query - pagy_searchkick(@query.results(:searchkick_pagy), limit:) - else - pagy(@query.results(:ransack), limit:) - end - end - - def select_query - @query.results(@query.advanced_query ? :searchkick : :ransack) - .where(updated_at: ..params[:timestamp].to_datetime).select(:id).pluck(:id) - end -end diff --git a/app/controllers/groups/samples_controller.rb b/app/controllers/groups/samples_controller.rb index 80fd8c9e5f..268627b023 100644 --- a/app/controllers/groups/samples_controller.rb +++ b/app/controllers/groups/samples_controller.rb @@ -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 @@ -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 diff --git a/app/controllers/projects/samples_controller.rb b/app/controllers/projects/samples_controller.rb index e3054484b9..d5fb01f94a 100644 --- a/app/controllers/projects/samples_controller.rb +++ b/app/controllers/projects/samples_controller.rb @@ -6,7 +6,6 @@ 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 @@ -14,7 +13,7 @@ class SamplesController < Projects::ApplicationController # rubocop:disable Metr 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 @@ -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 diff --git a/app/models/sample/query.rb b/app/models/sample/query.rb index a67d7ed705..37912ac86b 100644 --- a/app/models/sample/query.rb +++ b/app/models/sample/query.rb @@ -4,6 +4,7 @@ class Sample::Query # rubocop:disable Style/ClassAndModuleChildren include ActiveModel::Model include ActiveModel::Attributes + include Pagy::Backend ResultTypeError = Class.new(StandardError) @@ -33,17 +34,15 @@ 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 @@ -51,10 +50,20 @@ def results(type = :ransack) 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