Class: Snippets::BulkDestroyService
- Inherits:
-
Object
- Object
- Snippets::BulkDestroyService
- Includes:
- Gitlab::Allowable
- Defined in:
- app/services/snippets/bulk_destroy_service.rb
Constant Summary collapse
- NO_ACCESS_ERROR =
{ reason: :no_access_error, message: "You don't have access to delete these snippets." }.freeze
- SNIPPET_REPOSITORIES_DELETE_ERROR =
{ reason: :snippet_repositories_delete_error, message: 'Failed to delete snippet repositories.' }.freeze
- SNIPPETS_DELETE_ERROR =
{ reason: :snippet_delete_error, message: 'Failed to remove snippets.' }.freeze
- DeleteRepositoryError =
Class.new(StandardError)
- SnippetAccessError =
Class.new(StandardError)
Instance Attribute Summary collapse
-
#current_user ⇒ Object
readonly
Returns the value of attribute current_user.
-
#snippets ⇒ Object
readonly
Returns the value of attribute snippets.
Instance Method Summary collapse
- #execute(skip_authorization: false) ⇒ Object
-
#initialize(user, snippets) ⇒ BulkDestroyService
constructor
A new instance of BulkDestroyService.
Methods included from Gitlab::Allowable
Constructor Details
#initialize(user, snippets) ⇒ BulkDestroyService
Returns a new instance of BulkDestroyService.
25 26 27 28 |
# File 'app/services/snippets/bulk_destroy_service.rb', line 25 def initialize(user, snippets) @current_user = user @snippets = snippets end |
Instance Attribute Details
#current_user ⇒ Object (readonly)
Returns the value of attribute current_user.
20 21 22 |
# File 'app/services/snippets/bulk_destroy_service.rb', line 20 def current_user @current_user end |
#snippets ⇒ Object (readonly)
Returns the value of attribute snippets.
20 21 22 |
# File 'app/services/snippets/bulk_destroy_service.rb', line 20 def snippets @snippets end |
Instance Method Details
#execute(skip_authorization: false) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'app/services/snippets/bulk_destroy_service.rb', line 30 def execute(skip_authorization: false) return ServiceResponse.success(message: 'No snippets found.') if snippets.empty? user_can_delete_snippets! unless attempt_delete_repositories! snippets.destroy_all # rubocop: disable Cop/DestroyAll ServiceResponse.success(message: 'Snippets were deleted.') rescue SnippetAccessError ServiceResponse.error( reason: NO_ACCESS_ERROR[:reason], message: NO_ACCESS_ERROR[:message] ) rescue DeleteRepositoryError ServiceResponse.error( reason: SNIPPET_REPOSITORIES_DELETE_ERROR[:reason], message: SNIPPET_REPOSITORIES_DELETE_ERROR[:message] ) rescue StandardError # In case the delete operation fails ServiceResponse.error( reason: SNIPPETS_DELETE_ERROR[:reason], message: SNIPPETS_DELETE_ERROR[:message] ) end |