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

Conversation

aaronskiba
Copy link
Collaborator

Fixes #935

Changes proposed in this PR:

  • Add scope :search to ResearchOutput model: e289fba
    • Search query is performed by research_outputs.title (case-insensitive)
  • Create app/controllers/paginable/research_outputs_controller.rb 3c5a478
    • This new controller fixes the following features on the /plans/:plan_id/research_outputs page:
      • The clickable pagination options (e.g. View all,1,2,Next,Last)
      • The search box (for research_outputs.title matches)
      • The clickable sorting arrows

Search by `research_outputs.title` (case-insensitive)
This commit, along with e289fba are being made to resolve #935.
@aaronskiba aaronskiba marked this pull request as ready for review October 30, 2024 22:06
Copy link
Collaborator

@lagoan lagoan left a comment

Choose a reason for hiding this comment

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

I just have some questions here to start a conversation. Sorry if I missed something.

app/models/research_output.rb Show resolved Hide resolved
# 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)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I see this authorize handling on app/controllers/research_outputs_controller.rb. Why are we creating a new ResearchOutput if we cannot find one ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In the case of app/controllers/research_outputs_controller.rb:
If I only use authorize @research_outputs.first, I encounter the following if the plan has no research_outputs:

Pundit::NotDefinedError - unable to find policy `NilClassPolicy` for `nil`:
  app/controllers/research_outputs_controller.rb:17:in `index'

If I use authorize @research_outputs.first if @research_outputs.first.present?, I encounter the following if the plan has no research_outputs:

Pundit::AuthorizationNotPerformedError at /plans/14224/research_outputs
ResearchOutputsController

However, it may be the case that there is always at least one research output when app/controllers/paginable/research_outputs_controller.rb is called. But it might still be safest to keep authorize @research_outputs.first || ResearchOutput.new(plan_id: @plan.id)? (I could at least add a comment explaining this a bit).

Copy link
Collaborator

Choose a reason for hiding this comment

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

A comment would be good. Also add the method call parenthesis to clarify the expected order.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

However, it may be the case that there is always at least one research output when app/controllers/paginable/research_outputs_controller.rb is called. But it might still be safest to keep authorize @research_outputs.first || ResearchOutput.new(plan_id: @plan.id)? (I could at least add a comment explaining this a bit).

I guess || 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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

A comment would be good. Also add the method call parenthesis to clarify the expected order.

Cool, I made one more commit addressing this.

Comment on lines 12 to 14
@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)
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 @plan and @research_outputs to be instance variables?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

There is a corresponding app/views/paginable/research_outputs/_index.html.erb. That file has usage of @plan, but I think @research_outputs can be changed to research_outputs.


# 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

The corresponding view, `app/views/paginable/research_outputs/_index.html.erb`, uses `scope` rather than `@research_outputs`.
Copy link
Collaborator

@lagoan lagoan left a comment

Choose a reason for hiding this comment

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

Good work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants