-
-
Notifications
You must be signed in to change notification settings - Fork 572
Refactor Requests controller #5563
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
base: main
Are you sure you want to change the base?
Changes from all commits
85da11f
8089be6
b690801
72f9a3e
9a694c4
1f08698
7664d93
09927e6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| module View | ||
| RequestInfo = Data.define( | ||
| :request | ||
| ) do | ||
| class << self | ||
| def from_params(params:, organization:) | ||
| request = organization.requests.find(params[:id]) | ||
|
|
||
| new(request:) | ||
| end | ||
| end | ||
|
|
||
| def item_requests | ||
| request.item_requests.includes(:item) | ||
| end | ||
|
|
||
| def inventory | ||
| View::Inventory.new(request.organization_id) | ||
| end | ||
|
|
||
| def default_storage_location | ||
| request.partner.default_storage_location_id || request.organization.default_storage_location | ||
| end | ||
|
|
||
| def location | ||
| StorageLocation.find_by(id: default_storage_location) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| end | ||
|
|
||
| def custom_units | ||
| Flipper.enabled?(:enable_packs) && request.item_requests.any? { |item| item.request_unit } | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| module View | ||
| Requests = Data.define( | ||
| :requests, | ||
| :filters, | ||
| :paginated_requests, | ||
| :organization, | ||
| :helpers | ||
| ) do | ||
| include DateRangeHelper | ||
|
|
||
| class << self | ||
| def filter_params(params = {}) | ||
| if params.key?(:filters) | ||
| params.require(:filters).permit(:by_request_item_id, :by_partner, :by_status, :by_request_type) | ||
| else | ||
| {} | ||
| end | ||
| end | ||
|
|
||
| def from_params(params:, organization:, helpers:) | ||
| filters = filter_params(params) | ||
|
|
||
| requests = organization | ||
| .ordered_requests | ||
| .undiscarded | ||
| .during(helpers.selected_range) | ||
| .class_filter(filters) | ||
|
|
||
| paginated_requests = requests.includes(:partner).page(params[:page]) | ||
|
|
||
| new(requests:, filters:, paginated_requests:, organization:, helpers:) | ||
| end | ||
| end | ||
|
|
||
| def unfulfilled_requests_count | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| organization | ||
| .requests | ||
| .where(status: [:pending, :started]) | ||
| .during(helpers.selected_range) | ||
| .class_filter(filters) | ||
| .count | ||
| end | ||
|
|
||
| def calculate_product_totals | ||
| RequestsTotalItemsService.new(requests: requests).calculate | ||
| end | ||
|
|
||
| def items | ||
| organization.items.alphabetized.select(:id, :name) | ||
| end | ||
|
|
||
| def partners | ||
| organization.partners.alphabetized.select(:id, :name, :status) | ||
| end | ||
|
|
||
| def statuses | ||
| Request.statuses.transform_keys(&:humanize) | ||
| end | ||
|
|
||
| def partner_users | ||
| User.where(id: paginated_requests.map(&:partner_user_id)).select(:id, :name, :email) | ||
| end | ||
|
|
||
| def request_types | ||
| Request.request_types.transform_keys(&:humanize) | ||
| end | ||
|
|
||
| def selected_request_type | ||
| filters[:by_request_type] | ||
| end | ||
|
|
||
| def selected_request_item | ||
| filters[:by_request_item_id] | ||
| end | ||
|
|
||
| def selected_partner | ||
| filters[:by_partner] | ||
| end | ||
|
|
||
| def selected_status | ||
| filters[:by_status] | ||
| end | ||
| end | ||
| end | ||
There was a problem hiding this comment.
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