diff --git a/app/models/artist.rb b/app/models/artist.rb index 154bb075d..f033d4c3c 100644 --- a/app/models/artist.rb +++ b/app/models/artist.rb @@ -44,9 +44,14 @@ class Artist < ActiveRecord::Base def save_url_string if @url_string - urls.clear + prev = urls.map(&:url) + curr = @url_string.scan(/\S+/) - @url_string.scan(/\S+/).each do |url| + (prev - curr).each do |url| + urls.where(:url => url).destroy_all + end + + (curr - prev).each do |url| urls.create(:url => url) end end diff --git a/test/unit/artist_test.rb b/test/unit/artist_test.rb index 085e88f3c..a3c222f60 100644 --- a/test/unit/artist_test.rb +++ b/test/unit/artist_test.rb @@ -124,6 +124,14 @@ class ArtistTest < ActiveSupport::TestCase assert_equal(["http://not.rembrandt.com/test.jpg"], artist.urls.map(&:to_s).sort) end + should "not delete urls that have not changed" do + artist = FactoryGirl.create(:artist, :name => "rembrandt", :url_string => "http://rembrandt.com/test.jpg") + old_url_ids = ArtistUrl.order("id").pluck(&:id) + artist.url_string = "http://rembrandt.com/test.jpg" + artist.save + assert_equal(old_url_ids, ArtistUrl.order("id").pluck(&:id)) + end + should "ignore pixiv.net/ and pixiv.net/img/ url matches" do a1 = FactoryGirl.create(:artist, :name => "yomosaka", :url_string => "http://i2.pixiv.net/img100/img/yomosaka/27618292.jpg") a2 = FactoryGirl.create(:artist, :name => "niwatazumi_bf", :url_string => "http://i2.pixiv.net/img16/img/niwatazumi_bf/35488864_big_p6.jpg")