2012-09-27 20:59:42 +02:00
|
|
|
class SnippetsController < ProjectResourceController
|
2013-03-18 22:33:41 +01:00
|
|
|
before_filter :module_enabled
|
2012-08-11 00:07:50 +02:00
|
|
|
before_filter :snippet, only: [:show, :edit, :destroy, :update, :raw]
|
2011-12-15 22:57:46 +01:00
|
|
|
|
|
|
|
# Allow read any snippet
|
2011-10-16 23:07:10 +02:00
|
|
|
before_filter :authorize_read_snippet!
|
2011-12-15 22:57:46 +01:00
|
|
|
|
|
|
|
# Allow write(create) snippet
|
2012-08-11 00:07:50 +02:00
|
|
|
before_filter :authorize_write_snippet!, only: [:new, :create]
|
2011-12-15 22:57:46 +01:00
|
|
|
|
|
|
|
# Allow modify snippet
|
2012-08-11 00:07:50 +02:00
|
|
|
before_filter :authorize_modify_snippet!, only: [:edit, :update]
|
2011-12-15 22:57:46 +01:00
|
|
|
|
|
|
|
# Allow destroy snippet
|
2012-08-11 00:07:50 +02:00
|
|
|
before_filter :authorize_admin_snippet!, only: [:destroy]
|
2011-10-16 23:07:10 +02:00
|
|
|
|
|
|
|
respond_to :html
|
|
|
|
|
|
|
|
def index
|
2013-01-05 22:49:01 +01:00
|
|
|
@snippets = @project.snippets.fresh.non_expired
|
2011-10-16 23:07:10 +02:00
|
|
|
end
|
|
|
|
|
2011-10-26 15:46:25 +02:00
|
|
|
def new
|
2011-10-16 23:07:10 +02:00
|
|
|
@snippet = @project.snippets.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
@snippet = @project.snippets.new(params[:snippet])
|
|
|
|
@snippet.author = current_user
|
|
|
|
@snippet.save
|
|
|
|
|
|
|
|
if @snippet.valid?
|
|
|
|
redirect_to [@project, @snippet]
|
|
|
|
else
|
|
|
|
respond_with(@snippet)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
@snippet.update_attributes(params[:snippet])
|
|
|
|
|
|
|
|
if @snippet.valid?
|
|
|
|
redirect_to [@project, @snippet]
|
|
|
|
else
|
|
|
|
respond_with(@snippet)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
2012-08-11 00:07:50 +02:00
|
|
|
@note = @project.notes.new(noteable: @snippet)
|
2012-12-02 20:43:39 +01:00
|
|
|
@target_type = :snippet
|
|
|
|
@target_id = @snippet.id
|
2011-10-16 23:07:10 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2011-10-17 12:39:03 +02:00
|
|
|
return access_denied! unless can?(current_user, :admin_snippet, @snippet)
|
2011-10-16 23:07:10 +02:00
|
|
|
|
|
|
|
@snippet.destroy
|
|
|
|
|
2011-10-17 12:39:03 +02:00
|
|
|
redirect_to project_snippets_path(@project)
|
2011-10-16 23:07:10 +02:00
|
|
|
end
|
2011-12-15 22:57:46 +01:00
|
|
|
|
2012-12-14 06:14:05 +01:00
|
|
|
def raw
|
2012-06-12 20:41:46 +02:00
|
|
|
send_data(
|
|
|
|
@snippet.content,
|
2012-08-11 00:07:50 +02:00
|
|
|
type: "text/plain",
|
|
|
|
disposition: 'inline',
|
|
|
|
filename: @snippet.file_name
|
2012-06-12 20:41:46 +02:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2011-12-15 22:57:46 +01:00
|
|
|
protected
|
2012-06-12 20:41:46 +02:00
|
|
|
|
2012-02-21 23:31:18 +01:00
|
|
|
def snippet
|
|
|
|
@snippet ||= @project.snippets.find(params[:id])
|
|
|
|
end
|
2011-12-15 22:57:46 +01:00
|
|
|
|
|
|
|
def authorize_modify_snippet!
|
2012-02-21 23:31:18 +01:00
|
|
|
return render_404 unless can?(current_user, :modify_snippet, @snippet)
|
2011-12-15 22:57:46 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def authorize_admin_snippet!
|
2012-02-21 23:31:18 +01:00
|
|
|
return render_404 unless can?(current_user, :admin_snippet, @snippet)
|
2011-12-15 22:57:46 +01:00
|
|
|
end
|
2013-03-18 22:33:41 +01:00
|
|
|
|
|
|
|
def module_enabled
|
|
|
|
return render_404 unless @project.snippet_enabled
|
|
|
|
end
|
2011-10-16 23:07:10 +02:00
|
|
|
end
|