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

Resolves #4739 #4763

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

Conversation

jorgecoutinhobr
Copy link
Contributor

Resolves #4739

Description

  • Remove product drive delete capability if there are any donations for the product drive.

Type of change

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

How Has This Been Tested?

  • Add new tests for product_drive_system_spec.rb

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 OK, but has a request (hah) about the tests. @cielf this accomplishes the goal by hiding the button rather than having a backend validation - is that good enough?

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

def can_delete?(user)
user.has_role?(Role::ORG_ADMIN, organization) && donations.empty?
Copy link
Collaborator

Choose a reason for hiding this comment

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

🎉

@@ -130,4 +130,34 @@
expect(page).to have_content 'Endless drive'
end
end

context "when deleting a Product drive" do
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 these tests to request tests? There's no reason to load a whole browser to test the HTML output by the controller.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it! Thanks for the heads-up

@cielf
Copy link
Collaborator

cielf commented Nov 1, 2024

Looks OK, but has a request (hah) about the tests. @cielf this accomplishes the goal by hiding the button rather than having a backend validation - is that good enough?

Hmm. All other things being equal, I would prefer the somewhat safer solution. Do I think it's going to be an issue before such time as we would want to add deactivation? Nope. So I guess I'm ok with going forward with it if @dorner is. Should we add some warning comments?

@jorgecoutinhobr
Copy link
Contributor Author

Hey, @cielf and @dorner!! :bowtie:

I made a few changes. I thought about keeping the "ProductDrive#can_delete?" method as it was and doing all the validation in the controller, but that way it would still be possible to delete a product drive with donations from the Rails console. So I took another approach.
I don't know why a test broke, locally it's working.

@@ -4,4 +4,8 @@ def is_virtual(product_drive:)

product_drive.virtual? ? 'Yes' : 'No'
end

def can_delete_product_drive?(user, product_drive)
user.has_role?(Role::ORG_ADMIN, product_drive.organization) && product_drive.donations.empty?
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 a bit confused here. Can we instead move this to a policy object? Maybe under app/policies? We can pass in the product drive and the user. Then we can reuse this method across both the view and the backend. Something like:

module ProductDrivePolicy
  class << self
    def can_destroy?(product_drive, user)
      user.has_role?(Role::ORG_ADMIN, product_drive.organization) && product_drive.donations.empty?
    end
  end
end

and then you can call ProductDrivePolicy.can_destroy?(product_drive, user) in both places.

I know we don't have any of these policy objects yet, but I think I like this pattern for future similar use cases as well.

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

def validate_destroy
Copy link
Collaborator

Choose a reason for hiding this comment

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

This seems a bit heavy for a validation. I'd rather extract this to a ProductDriveDestroyService which calls the policy. That matches the pattern we have a bit better. It can be pretty lightweight.

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