Module: SnippetsActions

Extended by:
ActiveSupport::Concern
Includes:
Gitlab::NoteableMetadata, PaginatedCollection, RendersBlob, RendersNotes, Snippets::SendBlob, SnippetsSort
Included in:
Projects::SnippetsController, SnippetsController
Defined in:
app/controllers/concerns/snippets_actions.rb

Instance Method Summary collapse

Methods included from SnippetsSort

#sort_param

Methods included from Snippets::SendBlob

#send_snippet_blob

Methods included from SendsBlob

#send_blob

Methods included from Gitlab::NoteableMetadata

#noteable_meta_data

Methods included from RendersBlob

#blob_json, #conditionally_expand_blob, #conditionally_expand_blobs, #render_blob_json

Methods included from RendersNotes

#prepare_notes_for_rendering

Instance Method Details

#destroyObject


93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/controllers/concerns/snippets_actions.rb', line 93

def destroy
  service_response = Snippets::DestroyService.new(current_user, @snippet).execute

  if service_response.success?
    redirect_to gitlab_dashboard_snippets_path(@snippet), status: :found
  elsif service_response.http_status == 403
    access_denied!
  else
    redirect_to gitlab_snippet_path(@snippet),
                status: :found,
                alert: service_response.message
  end
end

#editObject


20
21
22
23
24
25
26
# File 'app/controllers/concerns/snippets_actions.rb', line 20

def edit
  # We need to load some info from the existing blob
  snippet.content = blob.data
  snippet.file_name = blob.path

  render 'edit'
end

#js_request?Boolean

Returns:

  • (Boolean)

50
51
52
# File 'app/controllers/concerns/snippets_actions.rb', line 50

def js_request?
  request.format.js?
end

#rawObject

This endpoint is being replaced by Snippets::BlobController#raw Support for old raw links will be maintainted via this action but it will only return the first blob found, see: gitlab.com/gitlab-org/gitlab/-/issues/217775


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/controllers/concerns/snippets_actions.rb', line 32

def raw
  workhorse_set_content_type!

  # Until we don't migrate all snippets to version
  # snippets we need to support old `SnippetBlob`
  # blobs
  if defined?(blob.snippet)
    send_data(
      convert_line_endings(blob.data),
      type: 'text/plain; charset=utf-8',
      disposition: content_disposition,
      filename: Snippet.sanitized_file_name(blob.name)
    )
  else
    send_snippet_blob(snippet, blob)
  end
end

#showObject

rubocop:disable Gitlab/ModuleWithInstanceVariables


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/controllers/concerns/snippets_actions.rb', line 55

def show
  respond_to do |format|
    format.html do
      conditionally_expand_blob(blob)
      @note = Note.new(noteable: @snippet, project: @snippet.project)
      @noteable = @snippet

      @discussions = @snippet.discussions
      @notes = prepare_notes_for_rendering(@discussions.flat_map(&:notes), @noteable)
      render 'show'
    end

    format.json do
      conditionally_expand_blob(blob)
      render_blob_json(blob)
    end

    format.js do
      if @snippet.embeddable?
        conditionally_expand_blobs(blobs)

        render 'shared/snippets/show'
      else
        head :not_found
      end
    end
  end
end

#updateObject


84
85
86
87
88
89
90
91
# File 'app/controllers/concerns/snippets_actions.rb', line 84

def update
  update_params = snippet_params.merge(spammable_params)

  service_response = Snippets::UpdateService.new(@snippet.project, current_user, update_params).execute(@snippet)
  @snippet = service_response.payload[:snippet]

  handle_repository_error(:edit)
end