2018-12-05 23:21:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
class Projects::SnippetsController < Projects::Snippets::ApplicationController
|
|
|
|
include SnippetsActions
|
2016-09-29 09:46:39 +05:30
|
|
|
include ToggleAwardEmoji
|
2017-08-17 22:00:37 +05:30
|
|
|
include SpammableActions
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
before_action :check_snippets_available!
|
2020-06-23 00:09:42 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
before_action :snippet, only: [:show, :edit, :destroy, :update, :raw, :toggle_award_emoji, :mark_as_spam]
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
before_action :authorize_create_snippet!, only: [:new, :create]
|
|
|
|
before_action :authorize_read_snippet!, except: [:new, :create, :index]
|
|
|
|
before_action :authorize_update_snippet!, only: [:edit, :update]
|
|
|
|
before_action :authorize_admin_snippet!, only: [:destroy]
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
before_action do
|
|
|
|
push_frontend_feature_flag(:snippet_multiple_files, current_user)
|
|
|
|
end
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
def index
|
2020-07-28 23:09:34 +05:30
|
|
|
@snippet_counts = ::Snippets::CountService
|
2020-03-13 15:44:24 +05:30
|
|
|
.new(current_user, project: @project)
|
|
|
|
.execute
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
@snippets = SnippetsFinder.new(current_user, project: @project, scope: params[:scope], sort: sort_param)
|
2019-12-04 20:38:33 +05:30
|
|
|
.execute
|
|
|
|
.page(params[:page])
|
|
|
|
.inc_author
|
|
|
|
|
|
|
|
return if redirect_out_of_range(@snippets)
|
|
|
|
|
|
|
|
@noteable_meta_data = noteable_meta_data(@snippets, 'Snippet')
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
2016-01-19 16:12:03 +05:30
|
|
|
@snippet = @noteable = @project.snippets.build
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2017-08-17 22:00:37 +05:30
|
|
|
create_params = snippet_params.merge(spammable_params)
|
2020-07-28 23:09:34 +05:30
|
|
|
service_response = ::Snippets::CreateService.new(project, current_user, create_params).execute
|
2020-03-13 15:44:24 +05:30
|
|
|
@snippet = service_response.payload[:snippet]
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
handle_repository_error(:new)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
alias_method :awardable, :snippet
|
2017-08-17 22:00:37 +05:30
|
|
|
alias_method :spammable, :snippet
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def spammable_path
|
|
|
|
project_snippet_path(@project, @snippet)
|
|
|
|
end
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
def snippet_params
|
2017-09-10 17:25:29 +05:30
|
|
|
params.require(:project_snippet).permit(:title, :content, :file_name, :private, :visibility_level, :description)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|