From ebcd3d43a6763f25730b19b96de44dfd4e783450 Mon Sep 17 00:00:00 2001 From: Marcin Kulik Date: Tue, 3 Apr 2012 19:57:47 +0200 Subject: [PATCH] Ability to delete your asciicast --- app/controllers/asciicasts_controller.rb | 25 +++++++++++++++++++++++- app/views/asciicasts/show.html.erb | 7 ++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/app/controllers/asciicasts_controller.rb b/app/controllers/asciicasts_controller.rb index 6cdb599..8fda216 100644 --- a/app/controllers/asciicasts_controller.rb +++ b/app/controllers/asciicasts_controller.rb @@ -1,6 +1,9 @@ class AsciicastsController < ApplicationController PER_PAGE = 20 + before_filter :load_resource, :only => [:show, :destroy] + before_filter :ensure_authenticated!, :only => [:destroy] + respond_to :html, :json def index @@ -11,8 +14,28 @@ class AsciicastsController < ApplicationController end def show - @asciicast = Asciicast.find(params[:id]) respond_with @asciicast end + def destroy + if @asciicast.user == current_user && @asciicast.destroy + redirect_to profile_path(current_user), + :notice => 'Your asciicast was deleted.' + else + if current_user + target = profile_path(current_user) + else + target = root_path + end + + redirect_to target, + :alert => "Oops, we couldn't remove this asciicast. Sorry." + end + end + + private + + def load_resource + @asciicast = Asciicast.find(params[:id]) + end end diff --git a/app/views/asciicasts/show.html.erb b/app/views/asciicasts/show.html.erb index 4221503..c7911d7 100644 --- a/app/views/asciicasts/show.html.erb +++ b/app/views/asciicasts/show.html.erb @@ -12,6 +12,11 @@ <%= render :partial => 'asciicasts/asciicast', :object => @asciicast %> +<% if @asciicast.user == current_user %> + <%= link_to 'Delete this asciicast', asciicast_path(@asciicast), + :method => :delete, :confirm => 'U SHURE?', :class => 'delete-link' %> +<% end %> +

Comments

@@ -40,5 +45,5 @@ comments.fetch(); new AsciiIo.Views.CommentsIndex({ collection: comments }); - }) + });