From 779d83e253b0903d9e077955398922954affe5bf Mon Sep 17 00:00:00 2001 From: albert Date: Thu, 15 Mar 2012 18:26:39 -0400 Subject: [PATCH] fixed pool deletion logic --- app/controllers/pools_controller.rb | 11 ++++++++++- app/models/pool.rb | 7 ++++--- app/views/pools/_secondary_links.html.erb | 6 +++++- app/views/posts/show.html.erb | 10 ++++++++++ config/routes.rb | 1 + db/migrate/20100211025616_create_pools.rb | 1 + test/functional/pools_controller_test.rb | 20 +++++++++++++++++--- 7 files changed, 48 insertions(+), 8 deletions(-) diff --git a/app/controllers/pools_controller.rb b/app/controllers/pools_controller.rb index b1dc92fbe..b0944a77b 100644 --- a/app/controllers/pools_controller.rb +++ b/app/controllers/pools_controller.rb @@ -49,10 +49,19 @@ class PoolsController < ApplicationController if !@pool.deletable_by?(CurrentUser.user) raise User::PrivilegeError end - @pool.destroy + @pool.update_attribute(:is_deleted, true) respond_with(@pool, :notice => "Pool deleted") end + def undelete + @pool = Pool.find(params[:id]) + if !@pool.deletable_by?(CurrentUser.user) + raise User::PrivilegeError + end + @pool.update_attribute(:is_deleted, false) + respond_with(@pool, :notice => "Pool undeleted") + end + def revert @pool = Pool.find(params[:id]) @version = PoolVersion.find(params[:version_id]) diff --git a/app/models/pool.rb b/app/models/pool.rb index 34ec378af..22a5266cb 100644 --- a/app/models/pool.rb +++ b/app/models/pool.rb @@ -12,9 +12,9 @@ class Pool < ActiveRecord::Base before_validation :initialize_creator, :on => :create after_save :create_version before_destroy :create_mod_action_for_destroy - attr_accessible :name, :description, :post_ids, :post_id_array, :post_count, :as => [:member, :privileged, :contributor, :janitor, :moderator, :admin, :default] - attr_accessible :is_active, :as => [:janitor, :moderator, :admin] - scope :active, where("is_active = true") + attr_accessible :name, :description, :post_ids, :post_id_array, :post_count, :is_active, :as => [:member, :privileged, :contributor, :janitor, :moderator, :admin, :default] + attr_accessible :is_deleted, :as => [:janitor, :moderator, :admin] + scope :active, where("is_active = true and is_deleted = false") def self.name_to_id(name) if name =~ /^\d+$/ @@ -56,6 +56,7 @@ class Pool < ActiveRecord::Base end def initialize_is_active + self.is_deleted = false if is_deleted.nil? self.is_active = true if is_active.nil? end diff --git a/app/views/pools/_secondary_links.html.erb b/app/views/pools/_secondary_links.html.erb index 7f1946f1a..00d066f8c 100644 --- a/app/views/pools/_secondary_links.html.erb +++ b/app/views/pools/_secondary_links.html.erb @@ -9,7 +9,11 @@
  • <%= link_to "Posts", posts_path(:tags => "pool:#{@pool.id}") %>
  • <%= link_to "Edit", edit_pool_path(@pool) %>
  • <% if @pool.deletable_by?(CurrentUser.user) %> -
  • <%= link_to "Delete", pool_path(@pool), :method => :delete, :confirm => "Are you sure you want to delete this pool?", :remote => true %>
  • + <% if @pool.is_deleted? %> +
  • <%= link_to "Undelete", undelete_pool_path(@pool), :method => :post, :remote => true %>
  • + <% else %> +
  • <%= link_to "Delete", pool_path(@pool), :method => :delete, :confirm => "Are you sure you want to delete this pool?", :remote => true %>
  • + <% end %> <% end %>
  • <%= link_to "Order", edit_pool_order_path(@pool) %>
  • <% end %> diff --git a/app/views/posts/show.html.erb b/app/views/posts/show.html.erb index 85e8f3a26..0e18d0584 100644 --- a/app/views/posts/show.html.erb +++ b/app/views/posts/show.html.erb @@ -46,6 +46,8 @@
  • Comments
  • Edit
  • +
  • Favorite
  • +
  • Share
  • @@ -59,6 +61,14 @@ + + + +
    diff --git a/config/routes.rb b/config/routes.rb index a2b367905..dccb736df 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -116,6 +116,7 @@ Danbooru::Application.routes.draw do end member do put :revert + post :undelete end resource :order, :only => [:edit, :update], :controller => "PoolOrders" end diff --git a/db/migrate/20100211025616_create_pools.rb b/db/migrate/20100211025616_create_pools.rb index e53ef9e0c..b75dec4d0 100644 --- a/db/migrate/20100211025616_create_pools.rb +++ b/db/migrate/20100211025616_create_pools.rb @@ -7,6 +7,7 @@ class CreatePools < ActiveRecord::Migration t.column :is_active, :boolean, :null => false, :default => true t.column :post_ids, :text, :null => false, :default => "" t.column :post_count, :integer, :null => false, :default => 0 + t.column :is_deleted, :boolean, :null => false, :default => false t.timestamps end diff --git a/test/functional/pools_controller_test.rb b/test/functional/pools_controller_test.rb index c89a92893..9222f95c6 100644 --- a/test/functional/pools_controller_test.rb +++ b/test/functional/pools_controller_test.rb @@ -67,9 +67,23 @@ class PoolsControllerTest < ActionController::TestCase end should "destroy a pool" do - assert_difference("Pool.count", -1) do - post :destroy, {:id => @pool.id}, {:user_id => @mod.id} - end + post :destroy, {:id => @pool.id}, {:user_id => @mod.id} + @pool.reload + assert_equal(true, @pool.is_deleted?) + end + end + + context "undelete action" do + setup do + @pool = Factory.create(:pool) + @pool.is_deleted = true + @pool.save + end + + should "restore a pool" do + post :undelete, {:id => @pool.id}, {:user_id => @mod.id} + @pool.reload + assert_equal(false, @pool.is_deleted?) end end