From e289fba9b0cbe4af4699455cf08adca8f597dac1 Mon Sep 17 00:00:00 2001 From: aaronskiba Date: Tue, 29 Oct 2024 13:36:39 -0600 Subject: [PATCH 1/5] Add `scope :search` to ResearchOutput model Search by `research_outputs.title` (case-insensitive) --- app/models/research_output.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/app/models/research_output.rb b/app/models/research_output.rb index 4165f0b7ea..f1bef1f16b 100644 --- a/app/models/research_output.rb +++ b/app/models/research_output.rb @@ -70,6 +70,15 @@ class ResearchOutput < ApplicationRecord # Ensure presence of the :output_type_description if the user selected 'other' validates_presence_of :output_type_description, if: -> { other? }, message: PRESENCE_MESSAGE + # ========== + # = Scopes = + # ========== + + scope :search, lambda { |term| + search_pattern = "%#{term}%" + where('lower(title) LIKE lower(?)', search_pattern) + } + # ==================== # = Instance methods = # ==================== From 3c5a478feacc9bb9a8e69abd93579c70af29a207 Mon Sep 17 00:00:00 2001 From: aaronskiba Date: Tue, 29 Oct 2024 16:29:37 -0600 Subject: [PATCH 2/5] Create `paginable/research_outputs_controller.rb` This commit, along with e289fba9b0cbe4af4699455cf08adca8f597dac1 are being made to resolve https://github.com/portagenetwork/roadmap/issues/935. --- .../paginable/research_outputs_controller.rb | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 app/controllers/paginable/research_outputs_controller.rb diff --git a/app/controllers/paginable/research_outputs_controller.rb b/app/controllers/paginable/research_outputs_controller.rb new file mode 100644 index 0000000000..2155c1321f --- /dev/null +++ b/app/controllers/paginable/research_outputs_controller.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +module Paginable + # Controller for paginating/sorting/searching the research_outputs table + class ResearchOutputsController < ApplicationController + include Paginable + + after_action :verify_authorized + + # GET /paginable/plans/:plan_id/research_outputs + def index + @plan = Plan.find_by(id: params[:plan_id]) + + # Same @research_outputs assignment as app/controllers/research_outputs_controller.rb + @research_outputs = ResearchOutput.includes(:repositories).where(plan_id: @plan.id) + # Same authorize handling as app/controllers/research_outputs_controller.rb + authorize @research_outputs.first || ResearchOutput.new(plan_id: @plan.id) + paginable_renderise( + partial: 'index', + scope: @research_outputs, + query_params: { sort_field: 'research_outputs.title' }, + format: :json + ) + end + end +end From 42f9067839126e9bfec1e26307016d2f54d4f1d7 Mon Sep 17 00:00:00 2001 From: aaronskiba Date: Wed, 30 Oct 2024 16:11:45 -0600 Subject: [PATCH 3/5] Update CHANGELOG.md --- CHANGELOG.md | 2 ++ app/controllers/paginable/research_outputs_controller.rb | 1 - 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0d7dc524f4..442ce5ae3e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,8 @@ - Fixes to CILogon / `openid_connect` Tests [#922](https://github.com/portagenetwork/roadmap/pull/922) + - Fix Paginating, Sorting, and Searching Issues Within "Research Outputs" Tab [#938](https://github.com/portagenetwork/roadmap/pull/938) + ## [4.1.1+portage-4.2.2] - 2024-09-18 ### Changed diff --git a/app/controllers/paginable/research_outputs_controller.rb b/app/controllers/paginable/research_outputs_controller.rb index 2155c1321f..58ab5c8789 100644 --- a/app/controllers/paginable/research_outputs_controller.rb +++ b/app/controllers/paginable/research_outputs_controller.rb @@ -10,7 +10,6 @@ class ResearchOutputsController < ApplicationController # GET /paginable/plans/:plan_id/research_outputs def index @plan = Plan.find_by(id: params[:plan_id]) - # Same @research_outputs assignment as app/controllers/research_outputs_controller.rb @research_outputs = ResearchOutput.includes(:repositories).where(plan_id: @plan.id) # Same authorize handling as app/controllers/research_outputs_controller.rb From cffb456ba0c68efcb571124dba4a4db5f465ffa4 Mon Sep 17 00:00:00 2001 From: aaronskiba Date: Tue, 12 Nov 2024 11:10:41 -0700 Subject: [PATCH 4/5] Change`research_outputs` to local variable The corresponding view, `app/views/paginable/research_outputs/_index.html.erb`, uses `scope` rather than `@research_outputs`. --- app/controllers/paginable/research_outputs_controller.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/controllers/paginable/research_outputs_controller.rb b/app/controllers/paginable/research_outputs_controller.rb index 58ab5c8789..0583ab79b5 100644 --- a/app/controllers/paginable/research_outputs_controller.rb +++ b/app/controllers/paginable/research_outputs_controller.rb @@ -10,13 +10,13 @@ class ResearchOutputsController < ApplicationController # GET /paginable/plans/:plan_id/research_outputs def index @plan = Plan.find_by(id: params[:plan_id]) - # Same @research_outputs assignment as app/controllers/research_outputs_controller.rb - @research_outputs = ResearchOutput.includes(:repositories).where(plan_id: @plan.id) + # Same assignment as app/controllers/research_outputs_controller.rb + research_outputs = ResearchOutput.includes(:repositories).where(plan_id: @plan.id) # Same authorize handling as app/controllers/research_outputs_controller.rb - authorize @research_outputs.first || ResearchOutput.new(plan_id: @plan.id) + authorize research_outputs.first || ResearchOutput.new(plan_id: @plan.id) paginable_renderise( partial: 'index', - scope: @research_outputs, + scope: research_outputs, query_params: { sort_field: 'research_outputs.title' }, format: :json ) From 2aed896b8aace8b033572a518ae391eb6e6e3d84 Mon Sep 17 00:00:00 2001 From: aaronskiba Date: Tue, 12 Nov 2024 12:33:31 -0700 Subject: [PATCH 5/5] Refactor / add comment --- app/controllers/paginable/research_outputs_controller.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/controllers/paginable/research_outputs_controller.rb b/app/controllers/paginable/research_outputs_controller.rb index 0583ab79b5..9eb5d7f338 100644 --- a/app/controllers/paginable/research_outputs_controller.rb +++ b/app/controllers/paginable/research_outputs_controller.rb @@ -13,7 +13,9 @@ def index # Same assignment as app/controllers/research_outputs_controller.rb research_outputs = ResearchOutput.includes(:repositories).where(plan_id: @plan.id) # Same authorize handling as app/controllers/research_outputs_controller.rb - authorize research_outputs.first || ResearchOutput.new(plan_id: @plan.id) + # `|| ResearchOutput.new(plan_id: @plan.id)` prevents Pundit::NotDefinedError when a direct + # GET /paginable/plans/:id/research_outputs request is made on a plan with 0 research_outputs + authorize(research_outputs.first || ResearchOutput.new(plan_id: @plan.id)) paginable_renderise( partial: 'index', scope: research_outputs,