Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Paginating, Sorting, and Searching Issues Within "Research Outputs" Tab #938

Merged
merged 5 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions app/controllers/paginable/research_outputs_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# 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])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to authorize by plan ? Something like

@plan = Plan.find_by(id: params[:plan_id])
authorize @plan, :show?

We would need to add an application policy on app/policies/paginable/

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

app/policies/research_output_policy.rb might be taking care of this. Here's a bit of that file's contents:

  def index?
    @research_output.plan.readable_by?(@user.id)
  end

  def new?
    @research_output.plan.administerable_by?(@user.id)
  end

  def edit?
    @research_output.plan.administerable_by?(@user.id)
  end

  def create?
    @research_output.plan.administerable_by?(@user.id)
  end

# 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
# `|| 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,
query_params: { sort_field: 'research_outputs.title' },
format: :json
)
end
end
end
9 changes: 9 additions & 0 deletions app/models/research_output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
# ====================
Expand Down
Loading