Skip to content

Refactor Requests controller#5563

Open
stefannibrasil wants to merge 10 commits into
rubyforgood:mainfrom
hexdevs:refactor-requests-controller-5557
Open

Refactor Requests controller#5563
stefannibrasil wants to merge 10 commits into
rubyforgood:mainfrom
hexdevs:refactor-requests-controller-5557

Conversation

@stefannibrasil
Copy link
Copy Markdown
Contributor

@stefannibrasil stefannibrasil commented May 11, 2026

Resolves #5557

Description

In #5543, I added one more variable to this controller. I didn't want to refactor the controller since the PR was already too big and it's a good practice to refactor in a separate stream of work.

This PR moves instance variables used in the views to View objects. This keeps the controller slim and it's more maintainable to add methods to the view objects from now on.

Type of change

  • refactor

How Has This Been Tested?

  • Existing request and system tests pass
  • Added coverage for the business logic in the view objects being introduced
  • tested the requests index and view pages

Screenshots

Short walk trough of requests page:

requests.refactor.480.mov

Moving view instances to a View Object.
This was raising a bullet error:

GET /donations
AVOID eager loading detected
  Donation => [:product_drive_participant]
  Remove from your query: .includes([:product_drive_participant])
Moving view instances to a View Object to keep
the controller clean.

Add coverage for business logic
@stefannibrasil stefannibrasil force-pushed the refactor-requests-controller-5557 branch from d3dc2a5 to 1f08698 Compare May 11, 2026 23:39
@@ -31,7 +31,6 @@ def from_params(params:, organization:, helpers:)
.includes(:storage_location,
:donation_site,
:product_drive,
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I am happy to remove this change from this PR. I got here by looking at other view objects and saw the bullet's warning: 8089be6cb25bfff90506e634fc486dbaf6454e9b

Copy link
Copy Markdown
Collaborator

@dorner dorner left a comment

Choose a reason for hiding this comment

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

Love the intention! Had a couple of comments about the details.

end

def location
StorageLocation.find_by(id: default_storage_location)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This isn't memoized, unlike the instance variable approach. Have you validated that this isn't called more than once?

The other methods also aren't memoized, but once loaded into memory Rails should be smart and not make more DB calls.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good call! Working on this.

end
end

def unfulfilled_requests_count
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Same comment as above - please validate that methods are either memoized, only called once, or would not result in additional DB calls.

expect(request.custom_units).to be_truthy

Flipper.disable(:enable_packs)
end
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

There are no unit tests for any of the other methods?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The reason why was because they were mostly accessing associations and it looks like testing rails internals at that point. So I focused on testing the methods that had business logic in them. But I added coverage for the remaining methods in 57a830d

expect(requests.selected_partner).to eq("A Local Partner")
expect(requests.selected_status).to eq("pending")
end
end
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Ditto.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Same reason as above... added full coverage in 2cfa660

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.

Refactor Requests controller: too many instance variables

2 participants