1
0
Fork 0

Implement undoing

This commit is contained in:
Andrew Tomaka 2015-10-14 09:21:16 -04:00
parent dc55fc10f1
commit dfa4ffeaea
5 changed files with 31 additions and 7 deletions

View file

@ -1,3 +1,11 @@
class Admin::BaseController < ApplicationController
layout 'admin'
private
def undo_link(item)
view_context.link_to('Undo this change',
admin_revert_version_path(id: item.version.last),
method: :post)
end
end

View file

@ -24,7 +24,8 @@ class Admin::CardsController < Admin::BaseController
authorize @card
if @card.save
redirect_to admin_cards_path, notice: 'Card was created'
redirect_to admin_cards_path,
notice: "Card was created. #{undo_link(@card)}"
else
render :new
end
@ -40,7 +41,8 @@ class Admin::CardsController < Admin::BaseController
authorize @card
if @card.update(card_params)
redirect_to admin_cards_path, notice: 'Card was updated'
redirect_to admin_cards_path,
notice: "Card was updated. #{undo_link(@card)}"
else
render :edit
end
@ -51,7 +53,8 @@ class Admin::CardsController < Admin::BaseController
@card.destroy
redirect_to admin_cards_path, notice: 'Card was deleted'
redirect_to admin_cards_path,
notice: "Card was deleted. #{undo_link(@card)}"
end
private

View file

@ -1,16 +1,27 @@
class Admin::VersionsController < Admin::BaseController
before_action :set_version, only: [:show]
def show
@version = PaperTrail::Version.find(params[:id]).reify(has_one: true)
type = @version.class.to_s.underscore
instance_variable_set('@' + type, @version)
render "admin/#{type.pluralize}/show"
end
def revert
@version = PaperTrail::Version.find(params[:id])
if @version.reify(has_one: true)
@version.reify(has_one: true).save!
else
@version.item.destroy
end
redirect_to :back, notice: "Undid #{@version.event}!"
end
private
def set_version
@version = PaperTrail::Version.find(params[:id]).reify(has_one: true)
end
end

View file

@ -2,4 +2,4 @@
button.close type='button' data-dismiss='alert'
span aria-hidden='true' &times;
span.sr-only Close
= message
= raw(message)

View file

@ -12,6 +12,8 @@ Rails.application.routes.draw do
get '/dashboard', to: 'dashboard#index'
post '/versions/revert', to: 'versions#revert', as: 'revert_version'
resources :awaken_types
resources :cards
resources :characters