2013-03-23 15:13:51 -04:00
|
|
|
class Projects::SnippetsController < Projects::ApplicationController
|
2013-03-23 14:14:37 -04:00
|
|
|
before_filter :module_enabled
|
|
|
|
before_filter :snippet, only: [:show, :edit, :destroy, :update, :raw]
|
|
|
|
|
|
|
|
# Allow read any snippet
|
2013-03-25 06:22:14 -04:00
|
|
|
before_filter :authorize_read_project_snippet!
|
2013-03-23 14:14:37 -04:00
|
|
|
|
|
|
|
# Allow write(create) snippet
|
2013-03-25 06:22:14 -04:00
|
|
|
before_filter :authorize_write_project_snippet!, only: [:new, :create]
|
2013-03-23 14:14:37 -04:00
|
|
|
|
|
|
|
# Allow modify snippet
|
2013-03-25 06:22:14 -04:00
|
|
|
before_filter :authorize_modify_project_snippet!, only: [:edit, :update]
|
2013-03-23 14:14:37 -04:00
|
|
|
|
|
|
|
# Allow destroy snippet
|
2013-03-25 06:22:14 -04:00
|
|
|
before_filter :authorize_admin_project_snippet!, only: [:destroy]
|
2013-03-23 14:14:37 -04:00
|
|
|
|
|
|
|
respond_to :html
|
|
|
|
|
|
|
|
def index
|
|
|
|
@snippets = @project.snippets.fresh.non_expired
|
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
2013-03-25 03:20:14 -04:00
|
|
|
@snippet = @project.snippets.build
|
2013-03-23 14:14:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2013-03-25 03:20:14 -04:00
|
|
|
@snippet = @project.snippets.build(params[:project_snippet])
|
2013-03-23 14:14:37 -04:00
|
|
|
@snippet.author = current_user
|
|
|
|
|
2013-03-25 08:28:39 -04:00
|
|
|
if @snippet.save
|
2013-03-24 14:31:14 -04:00
|
|
|
redirect_to project_snippet_path(@project, @snippet)
|
2013-03-23 14:14:37 -04:00
|
|
|
else
|
|
|
|
respond_with(@snippet)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2013-03-25 08:28:39 -04:00
|
|
|
if @snippet.update_attributes(params[:project_snippet])
|
2013-03-24 14:31:14 -04:00
|
|
|
redirect_to project_snippet_path(@project, @snippet)
|
2013-03-23 14:14:37 -04:00
|
|
|
else
|
|
|
|
respond_with(@snippet)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
|
|
|
@note = @project.notes.new(noteable: @snippet)
|
2013-12-25 06:32:43 -05:00
|
|
|
@notes = @snippet.notes.fresh
|
2013-12-25 15:32:48 -05:00
|
|
|
@noteable = @snippet
|
2013-03-23 14:14:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2013-03-24 14:31:14 -04:00
|
|
|
return access_denied! unless can?(current_user, :admin_project_snippet, @snippet)
|
2013-03-23 14:14:37 -04:00
|
|
|
|
|
|
|
@snippet.destroy
|
|
|
|
|
|
|
|
redirect_to project_snippets_path(@project)
|
|
|
|
end
|
|
|
|
|
|
|
|
def raw
|
|
|
|
send_data(
|
|
|
|
@snippet.content,
|
|
|
|
type: "text/plain",
|
|
|
|
disposition: 'inline',
|
|
|
|
filename: @snippet.file_name
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def snippet
|
|
|
|
@snippet ||= @project.snippets.find(params[:id])
|
|
|
|
end
|
|
|
|
|
2013-03-25 06:22:14 -04:00
|
|
|
def authorize_modify_project_snippet!
|
2013-03-24 14:31:14 -04:00
|
|
|
return render_404 unless can?(current_user, :modify_project_snippet, @snippet)
|
2013-03-23 14:14:37 -04:00
|
|
|
end
|
|
|
|
|
2013-03-25 06:22:14 -04:00
|
|
|
def authorize_admin_project_snippet!
|
2013-03-24 14:31:14 -04:00
|
|
|
return render_404 unless can?(current_user, :admin_project_snippet, @snippet)
|
2013-03-23 14:14:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def module_enabled
|
|
|
|
return render_404 unless @project.snippets_enabled
|
|
|
|
end
|
|
|
|
end
|