-
-
Notifications
You must be signed in to change notification settings - Fork 527
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
MichaScant
wants to merge
19
commits into
rubyforgood:main
Choose a base branch
from
MichaScant:fix-product-drive-delete
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
39c7550
add new logic for product_drive deletion
jorgecoutinhobr 3aec94e
add new tests for Product Drive system spec
jorgecoutinhobr cb17ab1
Implement new role verification and validation for product drive dele…
jorgecoutinhobr f7b416a
Remove product drive deletion tests from system spec
jorgecoutinhobr 65d9b7e
Merge branch 'main' of https://github.com/MichaScant/human-essentials…
MichaScant 83976b7
fixed proposed changes
MichaScant ea47123
Merge branch 'main' of https://github.com/MichaScant/human-essentials…
MichaScant 3204abe
proposed changes
MichaScant 677de12
fixed ruby lint errors
MichaScant b0ac10f
fixed error in rspec-system(6,3)
MichaScant 045d8be
replaced ProductDrivePolicy with the can destroy in product destroy s…
MichaScant 018c9f4
changed can_destroy to class method so it is able to be called
MichaScant 1b8c05f
fixed rubocop errors and arguments in show.html.erb
MichaScant 4d822f3
attempted to fixes, made can_destroy class method ot make it a class …
MichaScant 542a1e7
removed redundant operation as product_drive is already accessible
MichaScant cbb18ec
no longer destroying in controller
MichaScant 4bb1407
Merge branch 'main' of https://github.com/MichaScant/human-essentials…
MichaScant e8edee3
updated code based on requested changes
MichaScant 21cf464
throwing errors in destroy service at times now as test expects it
MichaScant File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,29 @@ | ||
class ProductDriveDestroyService | ||
attr_reader :product_drive, :user, :organization | ||
|
||
def initialize(product_drive, user, organization) | ||
@product_drive = product_drive | ||
@user = user | ||
@organization = organization | ||
end | ||
|
||
def self.call(product_drive, user, organization) | ||
new(product_drive, user, organization).call | ||
end | ||
|
||
def self.can_destroy?(product_drive, user) | ||
return false unless user.has_role?(Role::ORG_ADMIN, product_drive.organization) | ||
product_drive.donations.empty? | ||
end | ||
|
||
def call | ||
return unauthorized_error unless verify_role | ||
return donation_error unless self.class.can_destroy?(product_drive, user) | ||
|
||
product_drive.destroy | ||
|
||
if product_drive.errors.any? | ||
{ | ||
success: false, | ||
message: product_drive.errors.full_messages.join("\n") | ||
} | ||
else | ||
{ | ||
success: true, | ||
message: "Product drive was successfully destroyed." | ||
} | ||
class << self | ||
def call(product_drive, user, organization) | ||
return Result.new(error: "You are not allowed to perform this action.") unless verify_role(user, organization) | ||
|
||
unless can_destroy?(product_drive, user) | ||
product_drive.errors.add(:base, "Cannot delete product drive with donations.") | ||
raise ActiveRecord::RecordInvalid.new(product_drive) | ||
end | ||
|
||
if product_drive.destroy | ||
Result.new(value: "Product drive was successfully destroyed.") | ||
else | ||
raise ActiveRecord::RecordNotDestroyed.new("Failed to destroy product drive", product_drive) | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def verify_role | ||
return true if user.has_role?(Role::ORG_ADMIN, @organization) | ||
false | ||
end | ||
|
||
# def can_destroy?(product_drive, user) | ||
# return false unless user.has_role?(Role::ORG_ADMIN, product_drive.organization) | ||
|
||
# if product_drive.donations.empty? | ||
# true | ||
# else | ||
# product_drive.errors.add(:base, "Cannot delete product drive with donations.") | ||
# false | ||
# end | ||
# end | ||
def can_destroy?(product_drive, user) | ||
return false unless user.has_role?(Role::ORG_ADMIN, product_drive.organization) | ||
product_drive.donations.empty? | ||
end | ||
|
||
def unauthorized_error | ||
{success: false, message: "You are not allowed to perform this action."} | ||
end | ||
private | ||
|
||
def donation_error | ||
product_drive.errors.add(:base, "Cannot delete product drive with donations.") | ||
{success: false, message: "Cannot delete product drive with donations."} | ||
def verify_role(user, organization) | ||
user.has_role?(Role::ORG_ADMIN, organization) | ||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are we sometimes raising an error and sometimes setting the error to the Result object?