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

#4739: Fix product drive delete (updated) #4918

Open
wants to merge 19 commits into
base: main
Choose a base branch
from

Conversation

MichaScant
Copy link
Contributor

@MichaScant MichaScant commented Jan 6, 2025

Resolves #4739

Builds of the suggested changes from PR: #4763

@@ -69,6 +71,10 @@ def self.search_date_range(dates)
@search_date_range = { start_date: dates[0], end_date: dates[1] }
end

def destroy_product_drive
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 this method is really worth defining when it's a one-liner.

@@ -0,0 +1,23 @@
class ProductDriveDestroyService
def initialize(product_drive)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we change call to a class method? It's easier to work with in terms of stubbing tests.

@@ -90,7 +90,10 @@
</p>
<div class="card-footer clearfix">
<%= edit_button_to edit_product_drive_path(@product_drive), { text: "Make a correction", size: "md" } %>
<%= delete_button_to product_drive_path(@product_drive), { confirm: "Are you sure you want to permanently remove this product drive?", size: "md" } if current_user.has_role?(Role::ORG_ADMIN, current_organization) %>
confirm: "Are you sure you want to permanently remove this product drive?",
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this was a mistake?

expect(response.body).to include('Product drive was successfully destroyed.')
end

it "does not delete the product drive if it has donations" 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 test probably should be moved to the destroy service.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, should I replace it with the code that was previously in (describe "DELETE #destroy") or would you rather I delete it?

Copy link
Collaborator

Choose a reason for hiding this comment

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

You can leave the code that was there before.

@@ -85,6 +94,13 @@ def destroy

private

def verify_role
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we move this verification to the destroy service? You can pass the user into it.

@MichaScant MichaScant requested a review from dorner January 13, 2025 21:26
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.

Still some stuff left in that I think might be mistakes, plus a couple of other comments.

@@ -1,6 +1,7 @@
class ProductDrivesController < ApplicationController
include Importable
before_action :set_product_drive, only: [:show, :edit, :update, :destroy]
before_action only: :destroy
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this missing something, or am I reading it wrong?

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 don't believe so, although it can probably be removed seeing as the above line already is doing it along with other before_actions

format.json { head :no_content }
result = ProductDriveDestroyService.call(@product_drive, current_user, current_organization)

if result[:success]
Copy link
Collaborator

Choose a reason for hiding this comment

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

We have a Result class that can be used for this now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok will modify such that it uses the result class, and will change the destroy service to return result as well

@@ -40,6 +40,8 @@ class ProductDrive < ApplicationRecord

validate :end_date_is_bigger_of_end_date

before_destroy :validate_destroy, prepend: true
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this method defined somewhere?

end

def self.call(product_drive, user, organization)
new(product_drive, user, organization).call
Copy link
Collaborator

Choose a reason for hiding this comment

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

um... my point was that we do not need to call new at all. All methods can be class-level methods (you can wrap the whole shebang in a class << self block to make it simpler).

# product_drive.errors.add(:base, "Cannot delete product drive with donations.")
# false
# end
# end
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we need this commented-out code?

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.

Remove product drive delete if any donations for product drive
3 participants