diff --git a/app/models/artist.rb b/app/models/artist.rb index 5f690c726..dba52d00e 100644 --- a/app/models/artist.rb +++ b/app/models/artist.rb @@ -3,7 +3,7 @@ class Artist < ActiveRecord::Base before_save :normalize_name after_save :create_version after_save :save_url_string - after_create :categorize_tag + after_save :categorize_tag validates_uniqueness_of :name belongs_to :creator, :class_name => "User" has_many :members, :class_name => "Artist", :foreign_key => "group_name", :primary_key => "name" @@ -178,7 +178,9 @@ class Artist < ActiveRecord::Base end def categorize_tag - Tag.find_or_create_by_name("artist:#{name}") + if new_record? || name_changed? + Tag.find_or_create_by_name("artist:#{name}") + end end end diff --git a/test/unit/artist_test.rb b/test/unit/artist_test.rb index 7db3ba829..3d7a52756 100644 --- a/test/unit/artist_test.rb +++ b/test/unit/artist_test.rb @@ -182,11 +182,20 @@ class ArtistTest < ActiveSupport::TestCase assert_equal("yyy", artist.other_names) end - should "update the category of the tag" do + should "update the category of the tag when created" do tag = FactoryGirl.create(:tag, :name => "abc") artist = FactoryGirl.create(:artist, :name => "abc") tag.reload assert_equal(Tag.categories.artist, tag.category) end + + should "update the category of the tag when renamed" do + tag = FactoryGirl.create(:tag, :name => "def") + artist = FactoryGirl.create(:artist, :name => "abc") + artist.name = "def" + artist.save + tag.reload + assert_equal(Tag.categories.artist, tag.category) + end end end