diff --git a/app/models/artist.rb b/app/models/artist.rb index ddf3771f9..30a82c3dc 100644 --- a/app/models/artist.rb +++ b/app/models/artist.rb @@ -3,6 +3,7 @@ class Artist < ActiveRecord::Base before_save :normalize_name after_save :create_version after_save :save_url_string + after_save :commit_ban validates_uniqueness_of :name belongs_to :creator, :class_name => "User" has_many :members, :class_name => "Artist", :foreign_key => "group_name", :primary_key => "name" @@ -11,7 +12,7 @@ class Artist < ActiveRecord::Base has_one :wiki_page, :foreign_key => "title", :primary_key => "name" has_one :tag_alias, :foreign_key => "antecedent_name", :primary_key => "name" accepts_nested_attributes_for :wiki_page - attr_accessible :name, :url_string, :other_names, :group_name, :wiki_page_attributes, :notes + attr_accessible :name, :url_string, :other_names, :group_name, :wiki_page_attributes, :notes, :is_banned, :is_active scope :url_match, lambda {|string| where(["id in (?)", Artist.find_all_by_url(string).map(&:id)])} scope :other_names_match, lambda {|string| where(["other_names_index @@ to_tsquery('danbooru', ?)", Artist.normalize_name(string)])} scope :name_equals, lambda {|string| where("name = ?", string)} @@ -158,6 +159,34 @@ class Artist < ActiveRecord::Base end end + module BanMethods + def commit_ban + if is_banned? && is_banned_changed? + ban! + end + + true + end + + def ban! + Post.transaction do + Post.tag_match(name).each do |post| + begin + post.flag!("Artist requested removal") + rescue PostFlag::Error + # swallow + end + post.delete! + end + + tag_implication = TagImplication.create(:antecedent_name => name, :consequent_name => "banned_artist") + tag_implication.delay.process! + update_column(:is_active, false) + update_column(:is_banned, true) + end + end + end + include UrlMethods include NameMethods include GroupMethods @@ -165,13 +194,19 @@ class Artist < ActiveRecord::Base extend FactoryMethods include NoteMethods include TagMethods + include BanMethods - def ban! - + def status + if is_banned? + "Banned" + elsif is_active? + "Active" + else + "Deleted" + end end def initialize_creator self.creator_id = CurrentUser.user.id end end - diff --git a/app/views/artists/_form.html.erb b/app/views/artists/_form.html.erb index 908d3f168..709986215 100644 --- a/app/views/artists/_form.html.erb +++ b/app/views/artists/_form.html.erb @@ -3,6 +3,8 @@ <%= f.input :other_names, :hint => "Separate with commas", :as => :text %> <%= f.input :group_name %> <%= f.input :url_string, :label => "URLs", :as => :text, :size => "10x5" %> + <%= f.input :is_active %> + <%= f.input :is_banned %> <%= f.input :notes, :as => :text %> <%= f.button :submit %> <% end %> \ No newline at end of file diff --git a/app/views/artists/index.html.erb b/app/views/artists/index.html.erb index 003ef1a7d..52d730aad 100644 --- a/app/views/artists/index.html.erb +++ b/app/views/artists/index.html.erb @@ -20,17 +20,7 @@ <% end %> <%= artist.other_names %> - - <% if artist.is_active? %> - Active - <% else %> - Deleted - <% end %> - - <% if artist.is_banned? %> - Banned - <% end %> - + <%= artist.status %> <% end %> <% end %> diff --git a/app/views/artists/show.html.erb b/app/views/artists/show.html.erb index 545956818..f9b182c5b 100644 --- a/app/views/artists/show.html.erb +++ b/app/views/artists/show.html.erb @@ -13,15 +13,7 @@ Status - - <% if @artist.is_banned? %> - Banned - <% elsif @artist.is_active? %> - Active - <% else %> - Inactive - <% end %> - + <%= @artist.status %> <% if @artist.has_tag_alias? %> diff --git a/test/unit/artist_test.rb b/test/unit/artist_test.rb index d2d15fe38..2baf3738c 100644 --- a/test/unit/artist_test.rb +++ b/test/unit/artist_test.rb @@ -14,6 +14,24 @@ class ArtistTest < ActiveSupport::TestCase CurrentUser.ip_addr = nil end + context "that has been banned" do + setup do + @post = Factory.create(:post, :tag_string => "aaa") + @artist = Factory.create(:artist, :name => "aaa") + @artist.reload + @artist.update_attributes(:is_banned => true) + @post.reload + end + + should "delete the post" do + assert(@post.is_deleted?) + end + + should "create a new tag implication" do + assert_equal(1, TagImplication.where(:antecedent_name => "aaa", :consequent_name => "banned_artist").count) + end + end + should "create a new wiki page to store any note information" do artist = nil assert_difference("WikiPage.count") do