2018-09-25 23:45:43 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-06-16 11:08:32 -04:00
|
|
|
class Projects::SnippetsController < Projects::Snippets::ApplicationController
|
|
|
|
include SnippetsActions
|
2016-09-04 04:51:12 -04:00
|
|
|
include ToggleAwardEmoji
|
2017-02-01 13:15:59 -05:00
|
|
|
include SpammableActions
|
2018-02-06 08:33:18 -05:00
|
|
|
|
2017-06-20 07:13:04 -04:00
|
|
|
before_action :check_snippets_available!
|
2020-06-16 11:08:32 -04:00
|
|
|
|
2017-02-01 13:15:59 -05:00
|
|
|
before_action :snippet, only: [:show, :edit, :destroy, :update, :raw, :toggle_award_emoji, :mark_as_spam]
|
2013-03-23 14:14:37 -04:00
|
|
|
|
2020-01-23 07:08:38 -05:00
|
|
|
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]
|
2013-03-23 14:14:37 -04:00
|
|
|
|
|
|
|
def index
|
2020-06-22 08:08:47 -04:00
|
|
|
@snippet_counts = ::Snippets::CountService
|
2020-02-05 04:08:43 -05:00
|
|
|
.new(current_user, project: @project)
|
|
|
|
.execute
|
|
|
|
|
2020-07-10 14:09:45 -04:00
|
|
|
@snippets = SnippetsFinder.new(current_user, project: @project, scope: params[:scope], sort: sort_param)
|
2019-09-02 07:12:20 -04:00
|
|
|
.execute
|
|
|
|
.page(params[:page])
|
|
|
|
.inc_author
|
|
|
|
|
2019-09-03 13:45:00 -04:00
|
|
|
return if redirect_out_of_range(@snippets)
|
2019-09-02 07:12:20 -04:00
|
|
|
|
|
|
|
@noteable_meta_data = noteable_meta_data(@snippets, 'Snippet')
|
2013-03-23 14:14:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
2016-01-15 05:29:53 -05:00
|
|
|
@snippet = @noteable = @project.snippets.build
|
2013-03-23 14:14:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2017-02-14 14:07:11 -05:00
|
|
|
create_params = snippet_params.merge(spammable_params)
|
2020-06-22 08:08:47 -04:00
|
|
|
service_response = ::Snippets::CreateService.new(project, current_user, create_params).execute
|
2020-01-16 19:09:00 -05:00
|
|
|
@snippet = service_response.payload[:snippet]
|
2015-08-26 17:59:52 -04:00
|
|
|
|
2020-05-13 17:08:55 -04:00
|
|
|
handle_repository_error(:new)
|
2013-03-23 14:14:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2016-09-04 04:51:12 -04:00
|
|
|
alias_method :awardable, :snippet
|
2017-02-01 13:15:59 -05:00
|
|
|
alias_method :spammable, :snippet
|
2013-03-23 14:14:37 -04:00
|
|
|
|
2017-06-29 13:06:35 -04:00
|
|
|
def spammable_path
|
|
|
|
project_snippet_path(@project, @snippet)
|
|
|
|
end
|
|
|
|
|
2014-06-26 11:51:11 -04:00
|
|
|
def snippet_params
|
2017-05-03 11:26:49 -04:00
|
|
|
params.require(:project_snippet).permit(:title, :content, :file_name, :private, :visibility_level, :description)
|
2014-06-26 11:51:11 -04:00
|
|
|
end
|
2013-03-23 14:14:37 -04:00
|
|
|
end
|