2014-09-02 18:07:02 +05:30
|
|
|
class Projects::SnippetsController < Projects::ApplicationController
|
2017-08-17 22:00:37 +05:30
|
|
|
include RendersNotes
|
2016-09-29 09:46:39 +05:30
|
|
|
include ToggleAwardEmoji
|
2017-08-17 22:00:37 +05:30
|
|
|
include SpammableActions
|
|
|
|
include SnippetsActions
|
|
|
|
include RendersBlob
|
2016-09-29 09:46:39 +05:30
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
skip_before_action :verify_authenticity_token, only: [:show], if: :js_request?
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
before_action :check_snippets_available!
|
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
|
|
|
|
|
|
|
# Allow read any snippet
|
2016-06-02 11:05:42 +05:30
|
|
|
before_action :authorize_read_project_snippet!, except: [:new, :create, :index]
|
2014-09-02 18:07:02 +05:30
|
|
|
|
|
|
|
# Allow write(create) snippet
|
2015-09-11 14:41:01 +05:30
|
|
|
before_action :authorize_create_project_snippet!, only: [:new, :create]
|
2014-09-02 18:07:02 +05:30
|
|
|
|
|
|
|
# Allow modify snippet
|
2015-09-11 14:41:01 +05:30
|
|
|
before_action :authorize_update_project_snippet!, only: [:edit, :update]
|
2014-09-02 18:07:02 +05:30
|
|
|
|
|
|
|
# Allow destroy snippet
|
2015-09-11 14:41:01 +05:30
|
|
|
before_action :authorize_admin_project_snippet!, only: [:destroy]
|
2014-09-02 18:07:02 +05:30
|
|
|
|
|
|
|
respond_to :html
|
|
|
|
|
|
|
|
def index
|
2017-08-17 22:00:37 +05:30
|
|
|
@snippets = SnippetsFinder.new(
|
|
|
|
current_user,
|
|
|
|
project: @project,
|
|
|
|
scope: params[:scope]
|
|
|
|
).execute
|
2016-06-02 11:05:42 +05:30
|
|
|
@snippets = @snippets.page(params[:page])
|
2017-08-17 22:00:37 +05:30
|
|
|
if @snippets.out_of_range? && @snippets.total_pages != 0
|
2017-09-10 17:25:29 +05:30
|
|
|
redirect_to project_snippets_path(@project, page: @snippets.total_pages)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
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)
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
@snippet = CreateSnippetService.new(@project, current_user, create_params).execute
|
|
|
|
|
|
|
|
recaptcha_check_with_fallback { render :new }
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2017-08-17 22:00:37 +05:30
|
|
|
update_params = snippet_params.merge(spammable_params)
|
|
|
|
|
|
|
|
UpdateSnippetService.new(project, current_user, @snippet, update_params).execute
|
|
|
|
|
|
|
|
recaptcha_check_with_fallback { render :edit }
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
2017-08-17 22:00:37 +05:30
|
|
|
blob = @snippet.blob
|
2017-09-10 17:25:29 +05:30
|
|
|
conditionally_expand_blob(blob)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html do
|
|
|
|
@note = @project.notes.new(noteable: @snippet)
|
|
|
|
@noteable = @snippet
|
|
|
|
|
|
|
|
@discussions = @snippet.discussions
|
2018-03-17 18:26:18 +05:30
|
|
|
@notes = prepare_notes_for_rendering(@discussions.flat_map(&:notes), @noteable)
|
2017-08-17 22:00:37 +05:30
|
|
|
render 'show'
|
|
|
|
end
|
|
|
|
|
|
|
|
format.json do
|
|
|
|
render_blob_json(blob)
|
|
|
|
end
|
2018-10-15 14:42:47 +05:30
|
|
|
format.js { render 'shared/snippets/show'}
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
return access_denied! unless can?(current_user, :admin_project_snippet, @snippet)
|
|
|
|
|
|
|
|
@snippet.destroy
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
redirect_to project_snippets_path(@project), status: 302
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def snippet
|
|
|
|
@snippet ||= @project.snippets.find(params[:id])
|
|
|
|
end
|
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
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
def authorize_read_project_snippet!
|
|
|
|
return render_404 unless can?(current_user, :read_project_snippet, @snippet)
|
|
|
|
end
|
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
def authorize_update_project_snippet!
|
|
|
|
return render_404 unless can?(current_user, :update_project_snippet, @snippet)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def authorize_admin_project_snippet!
|
|
|
|
return render_404 unless can?(current_user, :admin_project_snippet, @snippet)
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|