From b0d70ede4c4e16e3cacd9b669bcf71180d5ee092 Mon Sep 17 00:00:00 2001 From: r888888888 Date: Fri, 31 May 2013 15:34:25 -0700 Subject: [PATCH] fixes #1653 --- app/controllers/artists_controller.rb | 3 ++- app/models/artist.rb | 12 +++++++++++ app/views/artists/_secondary_links.html.erb | 2 ++ test/unit/artist_test.rb | 24 +++++++++++++++++++++ 4 files changed, 40 insertions(+), 1 deletion(-) diff --git a/app/controllers/artists_controller.rb b/app/controllers/artists_controller.rb index 17e9e44cd..3ae607380 100644 --- a/app/controllers/artists_controller.rb +++ b/app/controllers/artists_controller.rb @@ -2,6 +2,7 @@ class ArtistsController < ApplicationController respond_to :html, :xml, :json before_filter :member_only, :except => [:index, :show, :banned] before_filter :admin_only, :only => [:ban] + before_filter :builder_only, :only => [:edit_name, :update_name] def new @artist = Artist.new_with_defaults(params) @@ -20,7 +21,7 @@ class ArtistsController < ApplicationController def update_name @artist = Artist.find(params[:id]) - @artist.update_attribute(:name, params[:artist][:name]) + @artist.rename!(params[:artist][:name]) respond_with(@artist) end diff --git a/app/models/artist.rb b/app/models/artist.rb index 2eaf88075..ab1925531 100644 --- a/app/models/artist.rb +++ b/app/models/artist.rb @@ -78,6 +78,18 @@ class Artist < ActiveRecord::Base def other_names_comma=(string) self.other_names = string.split(/,/).map {|x| Artist.normalize_name(x)}.join(" ") end + + def rename!(new_name) + new_wiki_page = WikiPage.titled(new_name).first + if new_wiki_page + # Merge the old wiki page into the new one + new_wiki_page.update_attributes(:body => new_wiki_page.body + "\n\n" + notes) + else + wiki_page.update_attribute(:title, new_name) + end + reload + update_attribute(:name, new_name) + end end module GroupMethods diff --git a/app/views/artists/_secondary_links.html.erb b/app/views/artists/_secondary_links.html.erb index 5c27f3640..069f9307d 100644 --- a/app/views/artists/_secondary_links.html.erb +++ b/app/views/artists/_secondary_links.html.erb @@ -11,6 +11,8 @@
  • <%= link_to "Show", artist_path(@artist) %>
  • <% if CurrentUser.is_member? %>
  • <%= link_to "Edit", edit_artist_path(@artist) %>
  • + <% end %> + <% if CurrentUser.is_builder? %>
  • <%= link_to "Edit name", edit_name_artist_path(@artist) %>
  • <% end %>
  • <%= link_to "History", artist_versions_path(:search => {:artist_id => @artist.id}) %>
  • diff --git a/test/unit/artist_test.rb b/test/unit/artist_test.rb index aed64b366..561cff717 100644 --- a/test/unit/artist_test.rb +++ b/test/unit/artist_test.rb @@ -14,6 +14,30 @@ class ArtistTest < ActiveSupport::TestCase CurrentUser.ip_addr = nil end + context "#rename!" do + setup do + @artist = FactoryGirl.create(:artist, :name => "aaa", :notes => "xxx") + end + + should "rename the wiki page" do + wiki_page = @artist.wiki_page + @artist.rename!("bbb") + assert_equal("bbb", @artist.name) + wiki_page.reload + assert_equal("bbb", wiki_page.title) + end + + should "merge the old wiki page into the new one if a wiki page for the new name already exists" do + FactoryGirl.create(:wiki_page, :title => "bbb", :body => "abcabc") + wiki_page = @artist.wiki_page + @artist.rename!("bbb") + wiki_page.reload + @artist.reload + assert_equal("xxx", wiki_page.body) + assert_equal("abcabc\n\nxxx", @artist.wiki_page.body) + end + end + context "with a matching tag alias" do setup do @tag_alias = FactoryGirl.create(:tag_alias, :antecedent_name => "aaa", :consequent_name => "bbb")