From 0724f6ca063a436d110ccd634de64e11b3b08161 Mon Sep 17 00:00:00 2001 From: r888888888 Date: Fri, 7 Mar 2014 16:42:20 -0800 Subject: [PATCH] fixes #1383 --- app/controllers/artists_controller.rb | 8 +++++++- app/models/artist.rb | 18 ++++++++++++++++++ app/views/artists/_secondary_links.html.erb | 7 ++++++- config/routes.rb | 1 + test/unit/artist_test.rb | 10 ++++++++++ 5 files changed, 42 insertions(+), 2 deletions(-) diff --git a/app/controllers/artists_controller.rb b/app/controllers/artists_controller.rb index c532c5fa0..74a50dd57 100644 --- a/app/controllers/artists_controller.rb +++ b/app/controllers/artists_controller.rb @@ -2,7 +2,7 @@ class ArtistsController < ApplicationController respond_to :html, :xml, :json before_filter :member_only, :except => [:index, :show, :banned] before_filter :builder_only, :only => [:destroy] - before_filter :admin_only, :only => [:ban] + before_filter :admin_only, :only => [:ban, :unban] def new @artist = Artist.new_with_defaults(params) @@ -32,6 +32,12 @@ class ArtistsController < ApplicationController redirect_to(artist_path(@artist), :notice => "Artist was banned") end + def unban + @artist = Artist.find(params[:id]) + @artist.unban! + redirect_to(artist_path(@artist), :notice => "Artist was unbanned") + end + def index search_params = params[:search].present? ? params[:search] : params @artists = Artist.search(search_params).order("id desc").paginate(params[:page], :limit => params[:limit]) diff --git a/app/models/artist.rb b/app/models/artist.rb index 722029311..1cb33042b 100644 --- a/app/models/artist.rb +++ b/app/models/artist.rb @@ -202,6 +202,24 @@ class Artist < ActiveRecord::Base end module BanMethods + def unban! + Post.transaction do + CurrentUser.without_safe_mode do + begin + Post.tag_match(name).each do |post| + post.unban! + end + rescue Post::SearchError + # swallow + end + + ti = TagImplication.where(:antecedent_name => name, :consequent_name => "banned_artist").first + ti.destroy if ti + update_column(:is_banned, false) + end + end + end + def ban! Post.transaction do CurrentUser.without_safe_mode do diff --git a/app/views/artists/_secondary_links.html.erb b/app/views/artists/_secondary_links.html.erb index 06a70ad5c..85857241d 100644 --- a/app/views/artists/_secondary_links.html.erb +++ b/app/views/artists/_secondary_links.html.erb @@ -21,7 +21,12 @@ <% end %> <% end %> <% if CurrentUser.is_admin? %> -
  • <%= link_to "Ban", ban_artist_path(@artist), :method => :put, :confirm => "Are you sure you want to ban this artist?" %>
  • + <% if @artist.is_banned? %> +
  • <%= link_to "unban", unban_artist_path(@artist), :method => :put, :confirm => "Are you sure you want to unban this artist?" %>
  • + + <% else %> +
  • <%= link_to "Ban", ban_artist_path(@artist), :method => :put, :confirm => "Are you sure you want to ban this artist?" %>
  • + <% end %> <% end %> <% end %> diff --git a/config/routes.rb b/config/routes.rb index dc85c2382..021e7a393 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -63,6 +63,7 @@ Danbooru::Application.routes.draw do member do put :revert put :ban + put :unban post :undelete end collection do diff --git a/test/unit/artist_test.rb b/test/unit/artist_test.rb index 8e404cc25..cfa3255a3 100644 --- a/test/unit/artist_test.rb +++ b/test/unit/artist_test.rb @@ -37,6 +37,16 @@ class ArtistTest < ActiveSupport::TestCase @post.reload end + should "allow unbanning" do + assert_difference("TagImplication.count", -1) do + @artist.unban! + end + @post.reload + @artist.reload + assert(!@artist.is_banned?, "artist should not be banned") + assert(!@post.is_banned?, "post should not be banned") + end + should "ban the post" do assert(@post.is_banned?) end