Class: Snippets::DestroyService

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

Constant Summary collapse

FAILED_TO_DELETE_ERROR =
:failed_to_delete_error
SNIPPET_NOT_FOUND_ERROR =
:snippet_not_found_error
SNIPPET_ACCESS_ERROR =
:snippet_access_error
DestroyError =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Gitlab::Allowable

#can?, #can_all?, #can_any?

Constructor Details

#initialize(user, snippet) ⇒ DestroyService

Returns a new instance of DestroyService.



15
16
17
18
# File 'app/services/snippets/destroy_service.rb', line 15

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

Instance Attribute Details

#current_userObject (readonly)

Returns the value of attribute current_user.



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

def current_user
  @current_user
end

#snippetObject (readonly)

Returns the value of attribute snippet.



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

def snippet
  @snippet
end

Instance Method Details

#executeObject



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

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

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

  attempt_destroy!

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