Class: Snippets::DestroyService

Inherits:
Object
  • Object
show all
Includes:
Gitlab::Allowable
Defined in:
app/services/snippets/destroy_service.rb

Constant Summary collapse

DestroyError =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Gitlab::Allowable

#can?

Constructor Details

#initialize(user, snippet) ⇒ DestroyService

Returns a new instance of DestroyService.


11
12
13
14
# File 'app/services/snippets/destroy_service.rb', line 11

def initialize(user, snippet)
  @current_user = user
  @snippet = snippet
end

Instance Attribute Details

#current_userObject (readonly)

Returns the value of attribute current_user


7
8
9
# File 'app/services/snippets/destroy_service.rb', line 7

def current_user
  @current_user
end

#snippetObject (readonly)

Returns the value of attribute snippet


7
8
9
# File 'app/services/snippets/destroy_service.rb', line 7

def snippet
  @snippet
end

Instance Method Details

#executeObject


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/services/snippets/destroy_service.rb', line 16

def execute
  if snippet.nil?
    return service_response_error('No snippet found.', 404)
  end

  unless user_can_delete_snippet?
    return service_response_error(
      "You don't have access to delete this snippet.",
      403
    )
  end

  attempt_destroy!

  ServiceResponse.success(message: 'Snippet was deleted.')
rescue DestroyError
  service_response_error('Failed to remove snippet repository.', 400)
rescue
  attempt_rollback_repository
  service_response_error('Failed to remove snippet.', 400)
end