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

issue addressed: 4274 #4366

Conversation

Gabe-Torres
Copy link
Contributor

Resolves #4274

Description

This PR implements changes to the ItemsController and adds an instance method to the Item model to query an organization for existing item names. These changes allow banks to receive a clear message, “An item with that name already exists (could be an inactive item),” when attempting to enter a new item name that already exists.
Please also include relevant motivation and context.
Main changes:

  1. ItemsController
  • Slightly restructured the create method to use abstraction for error handling and added a custom method while using rescue_from with ActiveRecord::RecordInvalid
  1. Item Model
  • Added a method to query for existing item names within an organization.

Guide questions:

  • What motivated this change (if not already described in an issue)?
    -- Banks should have a clear message explaining why an item couldn’t be created in the case of creating an item with the same name as another.
  • What alternative solutions did you consider?
    -- I considered using ActiveRecord to handle the case without the new model method but wanted clean code with the desired message.
  • What are the tradeoffs for your solution?
    -- Using the model method allowed for easier readability and control over error messages. This allowed to keep the other error message in the case of the error not being matching item names.
    -- Using ActiveRecord could be a more useful route since it is built in and would reduce the amount of logic in the controller and model.

List any dependencies that are required for this change. (gems, js libraries, etc.)

Include anything else we should know about. -->

Type of change

  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

Screenshots

I added WIP in the title since I got a couple of failing tests.
Ferrum:: JavaScriptError: TypeError: Cannot read properties of null (reading 'dispatchEvent') at Cuprite,trigger (<anonymous>: 412:10) at Cuprite.select (<anonymous>: 359:12) at HTMLOptionElement. <anonymous> (<anonymous>: 1:31)
I believe others have been having similar issues when running the test suite and this is being figured out currently. The two failing test do pass if I run the test separately.
Screenshot 2024-05-16 at 11 46 18 PM

Include screenshots (before / after) for style changes, highlight edited element.-->

Before

Screenshot 2024-05-17 at 1 29 09 AM

After

Screenshot 2024-05-17 at 1 24 42 AM

Screenshot 2024-05-17 at 1 24 34 AM

Screenshot 2024-05-17 at 1 24 18 AM

Screenshot 2024-05-17 at 1 25 37 AM

Screenshot 2024-05-17 at 1 23 22 AM

Screenshot 2024-05-17 at 1 25 11 AM

… existing item. A helpful message pops up stating the error. This is relevant to items within the same organization only.
…n for an already existing item name. This method will return true for items with the same name within the same organization. Will return false if the name does't exist within an organization. Will return false if the name exist but within a different organization.
… scenario of a name already being used. Create private method to abstract the error handling logic from the create method and call the model method that checks if an item name exist.
@cielf
Copy link
Collaborator

cielf commented May 17, 2024

Hey @Gabe-Torres. Thank you for this!
For future pull requests, please note that what we want for "Screenshots" is screenshots of the application in action, rather than screenshots of the code.

@cielf cielf requested a review from dorner May 17, 2024 11:33
app/models/item.rb Outdated Show resolved Hide resolved
@dorner
Copy link
Collaborator

dorner commented May 17, 2024

Also be sure to remove the WIP from the pull request title when it's ready to be reviewed.

…wncase on name parameter for case insensitive scenario
…ore action, check_for_existing_item_name, that triggers the query method to check for matching names before the create action. There is a conditional, item_params_valid?, that will only trigger the before action if the item is valid. This was needed because invalid params were going through the query, causing the "with invalid params" test(ln: 170) to fail. Lastly I made a helper method, prepare_new_action_render, to keep the code DRY.
@Gabe-Torres Gabe-Torres changed the title issue addressed: 4274 WIP issue addressed: 4274 May 19, 2024
@Gabe-Torres
Copy link
Contributor Author

I removed the WIP and it is ready to be reviewed again. It should be noted that I am still getting two recurring Ferrum::JavaScriptError: test fails.
One question I have is whether the query method should only check for matching item names within an organization or all organizations?
I also think I completely over thought this issue. 🤔 Please let me know if there's a more simple approach!

@@ -1,6 +1,8 @@
# Provides full CRUD to Items. Every item is rooted in a BaseItem, but Diaperbanks have full control to do whatever
# they like with their own Items.
class ItemsController < ApplicationController
before_action :check_for_existing_item_name, only: [:create], if: :item_params_valid?
Copy link
Collaborator

Choose a reason for hiding this comment

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

I feel like really what we want to do here is have a validation on uniqueness, no? You can only have one item per organization with the same name.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ahhhh that makes total sense. There is already a validation on uniqueness, so I will go off that. I initially went that route but ran into some issues. I'm going to try again and I'll be sure to reach out if I have issues!

flash[:error] = "Something didn't work quite right -- try again?"
render action: :new
prepare_new_action_render
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not sure I understand why this was moved? All you need to do here is change flash[:error] to be the actual error message instead of a generic "Something didn't work quite right".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes perfect sense, I initially did that but overthought it and convinced myself it was not that simple... 🥲 thank you! I will get this fixed up right now.

@@ -128,6 +127,46 @@
end
end

context "with an already existing item name" do
Copy link
Collaborator

Choose a reason for hiding this comment

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

We generally use request specs, not controller specs (which are discouraged in new Rails).

expect(response).to render_template(:new)
end

it "shouldn't create an item with the same name, case-insensitively" do
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is a superset of the previous test - if it fails case-insensitively, it will by definition also fail for case-sensitive.

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.

Improve error handling on New Item
3 participants