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: renames Itemizable.to_a to line_item_hashes (#3909) #4031

Conversation

elasticspoon
Copy link
Collaborator

Description

Itemizable has a method to_a which returns an array of hashes representing the line items in that Itemizable object. to_a is meant to convert an object to an array - instead, this returns an array of child members of the record.

This renames the method to Itemizable.line_item_hashes so that when we work with the line items inside an Itemizable, we explicitly use a array of hashes rather than the parent Itemizable itself.

Tradeoffs and Considerations

The main methods affected are by this change are StorageLocation.increase_inventory / StorageLocation.decrease_inventory. These methods were overloaded accepting both Itemizable objects and arrays of hashes.

Now, they will exclusively accept arrays of hashes. But this causes a bunch of ugly looking code because it now passes in the array directly.

@audit.storage_location.increase_inventory(increasing_adjustment.line_item_hashes)
@audit.storage_location.decrease_inventory(decreasing_adjustment.line_item_hashes)

There are several other potential ways to approach this:

  • increase and decrease could remain overloaded, check if they were passed an array or Itemizable and behave differently. I think this results in complection, the method knows too much about what it is being given. It would cause issues if the method was passed an array of something other than hashes.
  • increase and decrease could expect an Itemizable and call line_item_hashes themselves. This cleans up a lot of the code. However, there are several instances where the methods are passed arrays of hashes (here and here). I am not sure how to approach making in those places.
  • Pass the arrays of hashes directly. I opted for this option. While it does add some verbosity I think it clarifies behavior and maybe increase_inventory could be increase_inventory(itemizable_array:) to better reflect its behavior. That would make it even more verbose.

I am totally open to modifying how this is approached.

Note:

to_a method is not actually removed. I removed all calls to it that I could find but if called it currently logs an error and calls line_item_hashes. This is a quick change to fully remove it.

I was worried that some calls might not get caught by tests and I did not want the application to blow up with a no method error. I can also change that as needed.

Resolves #3909

Type of change

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

How Has This Been Tested?

I ran the full suite. But I did not write any tests specifically for my changes, I am not sure how to directly test the renaming of a method. I also clicked around the site a bit as different users to see if to_a would get called and it seems fine.

However, I was having a lot of flakiness issues with the systems specs (dashboard, admin/users_system). This was happening even on the main branch for me, so I don't think its related to my code. I think the tests are just not playing nice with me using docker.

I was also unable to run the CSV parsing tests, the setting for downloads to automatically download was not working.

@elasticspoon elasticspoon force-pushed the 3909-rename-Itemizable#to_a-to-line_item_hashes(itemizable-array) branch 2 times, most recently from 963c445 to 9ba1614 Compare January 15, 2024 23:54
@elasticspoon
Copy link
Collaborator Author

As I was reading the code to better understand what was going on I ended up making a few small refactors within StorageLocation.

@cielf cielf requested a review from dorner January 17, 2024 21:51
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.

@elasticspoon this is an amazing first pass! Thanks so much for your effort in this. I've left a couple of comments and a question for CL :)

app/models/concerns/itemizable.rb Show resolved Hide resolved
app/models/storage_location.rb Outdated Show resolved Hide resolved
app/models/concerns/itemizable.rb Outdated Show resolved Hide resolved
@elasticspoon elasticspoon force-pushed the 3909-rename-Itemizable#to_a-to-line_item_hashes(itemizable-array) branch 2 times, most recently from 3bee0c9 to 56a033c Compare January 18, 2024 19:46
@elasticspoon elasticspoon requested a review from dorner January 18, 2024 20:51
app/models/concerns/itemizable.rb Outdated Show resolved Hide resolved
spec/models/storage_location_spec.rb Outdated Show resolved Hide resolved
spec/models/storage_location_spec.rb Outdated Show resolved Hide resolved
@elasticspoon elasticspoon force-pushed the 3909-rename-Itemizable#to_a-to-line_item_hashes(itemizable-array) branch 2 times, most recently from 0a2afcb to 97b6156 Compare January 22, 2024 02:29
@elasticspoon elasticspoon requested a review from dorner January 23, 2024 02:43
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.

One tiny nitpick and I think this is good to go!
@cielf with the flag, merging this should be relatively benign. We can turn the flag on after the release.

spec/support/itemizable_shared_example.rb Outdated Show resolved Hide resolved
Itemizable has a method to_a which returns an array of hashes
representing the line items in that itemizable object. to_a
is meant to convert an object to an array - instead, this
returns an array of child members of the record.

This renames the method so that when we work with the line items
inside an Itemizable, we explicitly use a array of hashes
rather than the parent Itemizable itself.

The main methods affected are by this change are
StorageLocation.increase/decrease. These methods were overloaded
accepting both Itemizable objects and arrays of hashes. Now, they
will exclusively accept arrays of hashes.

The Itemizable.to_a method is not fully removed,
it currently logs an error and calls the correct method.

Fixes rubyforgood#3909
This adds a test for the situation where an item is
in the line_item_hash in the decrease inventory
method but does not exist in the database.

Previously this code path was untested.
This commit simplifies the behavior of the increase
and decrease methods by extracting the behavior of
updating the quantity of a single item out.

The new methods generalizes the behavior of all updates
and accepts a boolean to determine if the item
quantity should increase or decrease.

The method amount_insufficient was similarly extracted
out of decrease_inventory.
This commit does a small performance optimization
on the validation for empty inventory. It performs
faster in all situation except a very large
inventory that is completely empty.

It also renames the fuctions to better reflect
the validation.
line_item_hashes reflects the return type of
the value rather than its meaning.

line_item_values better reflects the meaning
of the function.
Adds the flag :deprecate_to_a.

With flag on it will raise an error and log a warning.
With flag off it will silently call the correct method.
Adds tests to itemizable concern to ensure flipper flag works.
This reverts commit cb06232.
This reverts commit 25fe88b.
@elasticspoon elasticspoon force-pushed the 3909-rename-Itemizable#to_a-to-line_item_hashes(itemizable-array) branch from 97b6156 to 9caee96 Compare January 24, 2024 05:17
@elasticspoon
Copy link
Collaborator Author

Closed the final nit. Hopefully we are all set now!

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.

Looks good to me! @cielf any issues with merging?

@elasticspoon thanks for your hard work on this. FYI, in the future please don't force push branches that have pull requests, as it breaks GitHub's "view commits since my last review" feature. 🙂

@elasticspoon
Copy link
Collaborator Author

@elasticspoon thanks for your hard work on this. FYI, in the future please don't force push branches that have pull requests, as it breaks GitHub's "view commits since my last review" feature. 🙂

Ok. What the suggested flow then? I thought I was rebasing my commits on top of main. Does that break it? Or did I accidentally force push?

Or does rebasing the branch on Github force push?

@dorner
Copy link
Collaborator

dorner commented Jan 25, 2024

Yes, whenever you rebase you're actually creating new commits. The only way to keep commits is to merge main back into your branch if you want it there.

@elasticspoon
Copy link
Collaborator Author

Makes sense. Github just stores the SHA of the last commit you reviewed and when I rebase it creates a bunch of new commits since then, so the diff will show all that info. That's kind of unfortunate.

Apparently you can use something like the script mentioned here but that is local Github CLI only.

I will stick to merging in the future then :)

@cielf
Copy link
Collaborator

cielf commented Jan 26, 2024

@dorner I think we can go ahead -- it's behind a flag, so we can shut it off quickly once we turn it on. Naive question -- does the flag automatically get turned on when it deploys to staging?

@dorner
Copy link
Collaborator

dorner commented Jan 26, 2024

No, it does not.

@cielf cielf merged commit 2725d38 into rubyforgood:main Jan 31, 2024
11 checks passed
@elasticspoon elasticspoon deleted the 3909-rename-Itemizable#to_a-to-line_item_hashes(itemizable-array) branch January 31, 2024 17:20
Copy link
Contributor

github-actions bot commented Feb 4, 2024

@elasticspoon: Your PR fix: renames Itemizable.to_a to line_item_hashes (#3909) is part of today's Human Essentials production release: 2024.02.04.
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.

Rename to_a method in Itemizable
3 participants