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

3992 preserve storage location when editing purchase #4003

Conversation

PhantomSean
Copy link
Contributor

@PhantomSean PhantomSean commented Dec 28, 2023

Resolves #3992

Description

Introduced condition to determine whether a new purchase is being created or if an existing purchase is being edited. If the latter is true the current storage location is displayed rather than defaulting to the organizations storage location/first item on the drop-down.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

Visual Test:

  1. Log in as an Organization Admin.
  2. Go to the Purchases page (http://127.0.0.1:3000/diaper_bank/purchases).
  3. View a purchase that has a Storage Location of 'Pawnee Main Bank (Office)'.
  4. Click the 'Make a Correction' button.
  5. Confirm that the Storage Location is correct and that it is not set to the first item in the drop-down list.

Rspec Test:
Added a test to spec/requests/purchases_requests_spec.rb to confirm that storage location is preserved editing a purchase and that the organization's storage location default is selected when creating a purchase.

Screenshots

Before:
image

After:
image

Copy link
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.

Hi Sean - thanks so much for the PR! The issue here is that the default should be selected for new purchases. It should not be selected when editing existing purchases (instead, the current storage location should be). This PR appears to change it so for new purchases, no storage location is selected by default.

@PhantomSean
Copy link
Contributor Author

Hi @dorner, thanks for the feedback. I've updated my code to allow for new purchases to still default to a storage location. Let me know if this satisfies your request 😄

collection: @storage_locations,
label: "Storage Location",
error: "Where is it being stored?",
selected: current_organization.intake_location,
Copy link
Member

@seanmarcia seanmarcia Dec 29, 2023

Choose a reason for hiding this comment

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

We can probably simplify this quite a bit with a ternary on the selected.
I imagine that something like:
selected: @purchase.new_record? ? current_organization.intake_location : @purchase.storage_location_id,
would work.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @seanmarcia, yes you are correct. Thank you for the suggestion, I've implemented the change that you suggested and is working fine.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks! I think the last part of the form to this would be to just move it into a helper :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No problem, created a helper function for it now.

storage1 = create(:storage_location, name: "storage1")
purchase = create(:purchase, storage_location: storage1)
visit edit_purchase_path(@organization.to_param, purchase)
expect(page).to have_content("storage1")
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm pretty sure this test will pass regardless of which location is selected. To verify, try taking your code in the previous files out and run the test - if it's still green, the test isn't useful. You'd need to find a better way to indicate that the right storage location is selected (perhaps via an option selected matcher.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes you were correct, @dorner. I've updated the test to make sure the expected storage is selected. Thanks for all the feedback, let me know if there's anything else that can be improved on.

dependabot bot and others added 3 commits December 30, 2023 11:02
Bumps [rubocop-rails](https://github.com/rubocop/rubocop-rails) from 2.23.0 to 2.23.1.
- [Release notes](https://github.com/rubocop/rubocop-rails/releases)
- [Changelog](https://github.com/rubocop/rubocop-rails/blob/master/CHANGELOG.md)
- [Commits](rubocop/rubocop-rails@v2.23.0...v2.23.1)

---
updated-dependencies:
- dependency-name: rubocop-rails
  dependency-type: direct:development
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

Preserved storage location on purchase edit page

Improved test

Fixing test affected by my change

Fixed to reintroduce default storage location for new purchases
Bumps [rubocop-performance](https://github.com/rubocop/rubocop-performance) from 1.19.1 to 1.20.1.
- [Release notes](https://github.com/rubocop/rubocop-performance/releases)
- [Changelog](https://github.com/rubocop/rubocop-performance/blob/master/CHANGELOG.md)
- [Commits](rubocop/rubocop-performance@v1.19.1...v1.20.1)

---
updated-dependencies:
- dependency-name: rubocop-performance
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

Simplified changes

Fix issue causing factory_bot_lint failure

Moved logic to a helper function
@PhantomSean PhantomSean force-pushed the 3992-preserve-storage-location-when-editing-purchase branch from fb6e43b to af40477 Compare December 30, 2023 00:27
purchase = create(:purchase, storage_location: storage1)
visit edit_purchase_path(@organization.to_param, purchase)
assert page.has_select? "Storage Location", selected: "storage1"
end
Copy link
Collaborator

Choose a reason for hiding this comment

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

Agree with @seanmarcia (his comment has been abandoned since you force pushed the branch - please don't do that with open PRs that have comments!)

You can inspect the rendered response in a request test. If you're not interacting with it in any way, that should be fine. System tests should be limited to interactivity and JavaScript testing since it's slower and more brittle.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Can another test be added to ensure the new behavior is also as expected?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've moved the test into requests, and added a new test to ensure the new behavior. Let me know if these address the changes you wanted.

Copy link
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.

Getting closer! 😄

it "storage location defaults to organizations storage location" do
purchase = create(:purchase)
get edit_purchase_path(@organization.to_param, purchase)
assert_select('select#purchase_storage_location_id option[selected]').first[storage_location.name]
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not familiar with the assert_select method and not sure I understand what it's doing here specifically (the docs don't seem to indicate this usage). Can you match this selector with the way we do checks elsewhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've updated the tests to use match as is used elsewhere in the tests.

@@ -67,6 +67,12 @@

expect(Purchase.last.amount_spent_in_cents).to eq 100_054
end

it "storage location defaults to organizations storage location" do
purchase = create(:purchase)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Spacing seems off here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, fixed that now

@dorner
Copy link
Collaborator

dorner commented Jan 7, 2024

@PhantomSean So close! There's a conflict that needs to be solved now.

@PhantomSean
Copy link
Contributor Author

@PhantomSean So close! There's a conflict that needs to be solved now.

@dorner Resolved conflict, hopefully all good now. Let me know if there's anything else to be done 😄

@dorner
Copy link
Collaborator

dorner commented Jan 8, 2024

lol now linting is breaking! 😁 I think it would be simpler to just leave the gemfile / gemfile.lock updates out of this PR. It's causing more headaches than it's worth.

@PhantomSean
Copy link
Contributor Author

lol now linting is breaking! 😁 I think it would be simpler to just leave the gemfile / gemfile.lock updates out of this PR. It's causing more headaches than it's worth.

I believe I've resolved the issues if you're free to review

@dorner
Copy link
Collaborator

dorner commented Jan 18, 2024

This looks good! Thanks very much!

@dorner dorner merged commit e07d71e into rubyforgood:main Jan 18, 2024
11 checks passed
@PhantomSean
Copy link
Contributor Author

This looks good! Thanks very much!

No problem!

Copy link
Contributor

@PhantomSean: Your PR 3992 preserve storage location when editing purchase is part of today's Human Essentials production release: 2024.01.21.
Thank you very much for your contribution!

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.

[Bug] On editing purchases, the Storage Location defaults to the first storage location. It shouldn't.
3 participants