-
-
Notifications
You must be signed in to change notification settings - Fork 504
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
4033 product drives filter by category #4094
4033 product drives filter by category #4094
Conversation
@@ -6,10 +6,13 @@ def index | |||
setup_date_range_picker | |||
@product_drives = current_organization | |||
.product_drives | |||
.includes(donations: [line_items: [:item]]) |
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.
This is a weird syntax... I'd assume it should look like donations: {line_items: :item}
. Not even sure how this parses 😮
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.
Functionally they create the same SQL query but describe the associations using arrays vs using a hash as you have.
I've changed it as you suggested as it probably is better to have it described as a hash and it a bit more readable.
app/models/product_drive.rb
Outdated
@@ -18,6 +18,7 @@ class ProductDrive < ApplicationRecord | |||
include Filterable | |||
|
|||
scope :by_name, ->(name_filter) { where(name: name_filter) } | |||
scope :by_item_category_id, ->(item_category_id) { joins(donations: [line_items: [:item]]).where(item: { item_category_id: item_category_id }) } |
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.
Can this line be split? Kinda long.
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.
Sure
app/models/product_drive.rb
Outdated
.during(date_range) | ||
.sum('line_items.quantity') | ||
def donation_quantity_by_date(date_range, item_category_id = nil) | ||
if [nil, ''].include? item_category_id |
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.
More Rails-y: if item_category_id.blank?
Better yet, use present?
and switch the conditional so the positive condition comes first.
.where(item: {item_category_id: item_category_id}) | ||
.during(date_range) | ||
.sum('line_items.quantity') | ||
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 usually try to build the query rather than copy the lines. So I'd write this thusly:
query = donations.during(date_range)
if item_category_id.present?
query = query.joins(line_items: :item).where(item: {item_category_id: item_category_id})
else
query = query.joins(:line_items)
end
query.sum('line_items.quantity')
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.
Thank you, that's a lot cleaner
app/models/product_drive.rb
Outdated
donations.joins(line_items: [:item]) | ||
.where(item: {item_category_id: item_category_id}) | ||
.during(date_range) | ||
.select('line_items.item_id').distinct.count |
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.
Same as above.
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.
See above comment, can this be built reusing the parts that are common to both conditions?
spec/models/product_drive_spec.rb
Outdated
it "calculates and returns all donated organization item quantities by category and date" do | ||
create(:donation, :with_items, item_quantity: 2, product_drive: product_drive, issued_at: '26-01-2023') | ||
result = product_drive.donation_quantity_by_date(Time.zone.parse('23/01/2023')..Time.zone.parse('26/01/2023'), 1) | ||
expect(result).to eq product_drive.organization.items.where(item_category_id: 1).count |
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.
This is a bit too finicky IMO. I'd actually create an item with the desired item category and verify that it comes back in the query. Otherwise we could just not have any data in there and it comes back empty... but we wouldn't know if it was empty because there was no data, or because there's an error in our query. In general for things like this you'll want at least one result that should be included and one that shouldn't, and verify that only the one you want comes back.
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.
Good feedback, I've created two items with unique category ids and have the test verifying the one we want comes back
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.
One last nitpick and then I think this is good to go!
app/models/product_drive.rb
Outdated
donations.joins(line_items: [:item]) | ||
.where(item: {item_category_id: item_category_id}) | ||
.during(date_range) | ||
.select('line_items.item_id').distinct.count |
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.
See above comment, can this be built reusing the parts that are common to both conditions?
Sorry missed that one! Should be sorted now 😄 |
Seems as though there appears to be a flaky test case in
Not sure if this a known issue. And another flaky test in |
Yep, does look flaky. Re-ran it and it passed. The other one should be fixed on the main branch. |
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.
Looks good - thank you!
No problem, thanks for all the good feedback! |
@PhantomSean: Your PR |
Resolves #4033
Description
Added new filter to the product drive page. This allows for filtration by item category. Made adjustments to the functions that display donation quantities and distinct item counts to return correct figures for when item category is chosen.
Type of change
How Has This Been Tested?
Visual Test:
Rspec Tests:
Added testing to spec/models/product_drive_spec.rb to confirm that
by_item_category_id
works as expected.Screenshots
Before:
After: