artists: simplify artist url saving code.
Refactor the way artist urls are saved so that artist url validations run before the artist is saved, not after.
This commit is contained in:
@@ -5,7 +5,6 @@ class Artist < ApplicationRecord
|
||||
before_create :initialize_creator
|
||||
before_validation :normalize_name
|
||||
after_save :create_version
|
||||
after_save :save_url_string
|
||||
after_save :categorize_tag
|
||||
after_save :update_wiki
|
||||
validates_uniqueness_of :name
|
||||
@@ -64,37 +63,20 @@ class Artist < ApplicationRecord
|
||||
urls.map(&:url)
|
||||
end
|
||||
|
||||
def save_url_string
|
||||
if @url_string
|
||||
prev = url_array
|
||||
curr = @url_string.scan(/\S+/).uniq
|
||||
def url_string=(string)
|
||||
@url_string_was = url_string
|
||||
|
||||
duplicates = prev.select{|url| prev.count(url) > 1}.uniq
|
||||
duplicates.each do |url|
|
||||
count = prev.count(url)
|
||||
urls.where(:url => url).limit(count-1).destroy_all
|
||||
end
|
||||
|
||||
(prev - curr).each do |url|
|
||||
urls.where(:url => url).destroy_all
|
||||
end
|
||||
|
||||
(curr - prev).each do |url|
|
||||
urls.create(:url => url)
|
||||
end
|
||||
self.urls = string.split(/[[:space:]]+/).uniq.map do |url|
|
||||
self.urls.find_or_initialize_by(url: url)
|
||||
end
|
||||
end
|
||||
|
||||
def url_string=(string)
|
||||
@url_string = string
|
||||
end
|
||||
|
||||
def url_string
|
||||
@url_string || url_array.join("\n")
|
||||
url_array.join("\n")
|
||||
end
|
||||
|
||||
def url_string_changed?
|
||||
url_string.scan(/\S+/) != url_array
|
||||
@url_string_was != url_string
|
||||
end
|
||||
|
||||
def map_domain(x)
|
||||
|
||||
@@ -168,11 +168,16 @@ class ArtistTest < ActiveSupport::TestCase
|
||||
assert_equal(["minko"], Artist.find_all_by_url("http://minko.com/x/test.jpg").map(&:name))
|
||||
end
|
||||
|
||||
should "not allow duplicates" do
|
||||
should "not find duplicates" do
|
||||
FactoryGirl.create(:artist, :name => "warhol", :url_string => "http://warhol.com/x/a/image.jpg\nhttp://warhol.com/x/b/image.jpg")
|
||||
assert_equal(["warhol"], Artist.find_all_by_url("http://warhol.com/x/test.jpg").map(&:name))
|
||||
end
|
||||
|
||||
should "not include duplicate urls" do
|
||||
artist = FactoryGirl.create(:artist, :url_string => "http://foo.com http://foo.com")
|
||||
assert_equal(["http://foo.com"], artist.url_array)
|
||||
end
|
||||
|
||||
should "hide deleted artists" do
|
||||
FactoryGirl.create(:artist, :name => "warhol", :url_string => "http://warhol.com/a/image.jpg", :is_active => false)
|
||||
assert_equal([], Artist.find_all_by_url("http://warhol.com/a/image.jpg").map(&:name))
|
||||
@@ -407,5 +412,18 @@ class ArtistTest < ActiveSupport::TestCase
|
||||
tag.reload
|
||||
assert_equal(Tag.categories.artist, tag.category)
|
||||
end
|
||||
|
||||
context "when updated" do
|
||||
setup do
|
||||
@artist = FactoryGirl.create(:artist)
|
||||
@artist.stubs(:merge_version?).returns(false)
|
||||
end
|
||||
|
||||
should "create a new version" do
|
||||
assert_difference("@artist.versions.count") do
|
||||
@artist.update(:url_string => "http://foo.com")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user