artists: don't create new version when nothing changed.
Fix an issue where saving an artist entry without changing anything would create a new artist version.
This commit is contained in:
@@ -2,12 +2,13 @@ class Artist < ApplicationRecord
|
|||||||
extend Memoist
|
extend Memoist
|
||||||
class RevertError < Exception ; end
|
class RevertError < Exception ; end
|
||||||
|
|
||||||
attr_accessor :url_string_was
|
attr_accessor :url_string_changed
|
||||||
|
|
||||||
before_validation :normalize_name
|
before_validation :normalize_name
|
||||||
after_save :create_version
|
after_save :create_version
|
||||||
after_save :categorize_tag
|
after_save :categorize_tag
|
||||||
after_save :update_wiki
|
after_save :update_wiki
|
||||||
|
after_save :clear_url_string_changed
|
||||||
validates :name, tag_name: true, uniqueness: true
|
validates :name, tag_name: true, uniqueness: true
|
||||||
validate :validate_wiki, :on => :create
|
validate :validate_wiki, :on => :create
|
||||||
belongs_to_creator
|
belongs_to_creator
|
||||||
@@ -181,16 +182,18 @@ class Artist < ApplicationRecord
|
|||||||
end
|
end
|
||||||
|
|
||||||
def url_string=(string)
|
def url_string=(string)
|
||||||
self.url_string_was = url_string
|
url_string_was = url_string
|
||||||
|
|
||||||
self.urls = string.to_s.scan(/[^[:space:]]+/).map do |url|
|
self.urls = string.to_s.scan(/[^[:space:]]+/).map do |url|
|
||||||
is_active, url = ArtistUrl.parse_prefix(url)
|
is_active, url = ArtistUrl.parse_prefix(url)
|
||||||
self.urls.find_or_initialize_by(url: url, is_active: is_active)
|
self.urls.find_or_initialize_by(url: url, is_active: is_active)
|
||||||
end.uniq(&:url)
|
end.uniq(&:url)
|
||||||
|
|
||||||
|
self.url_string_changed = (url_string_was != url_string)
|
||||||
end
|
end
|
||||||
|
|
||||||
def url_string_changed?
|
def clear_url_string_changed
|
||||||
url_string_was != url_string
|
self.url_string_changed = false
|
||||||
end
|
end
|
||||||
|
|
||||||
def map_domain(x)
|
def map_domain(x)
|
||||||
@@ -257,7 +260,7 @@ class Artist < ApplicationRecord
|
|||||||
|
|
||||||
module VersionMethods
|
module VersionMethods
|
||||||
def create_version(force=false)
|
def create_version(force=false)
|
||||||
if saved_change_to_name? || url_string_changed? || saved_change_to_is_active? || saved_change_to_is_banned? || saved_change_to_other_names? || saved_change_to_group_name? || saved_change_to_notes? || force
|
if saved_change_to_name? || url_string_changed || saved_change_to_is_active? || saved_change_to_is_banned? || saved_change_to_other_names? || saved_change_to_group_name? || saved_change_to_notes? || force
|
||||||
if merge_version?
|
if merge_version?
|
||||||
merge_version
|
merge_version
|
||||||
else
|
else
|
||||||
|
|||||||
@@ -493,15 +493,44 @@ class ArtistTest < ActiveSupport::TestCase
|
|||||||
assert_equal(Tag.categories.artist, tag.category)
|
assert_equal(Tag.categories.artist, tag.category)
|
||||||
end
|
end
|
||||||
|
|
||||||
context "when updated" do
|
context "when saving" do
|
||||||
setup do
|
setup do
|
||||||
@artist = FactoryBot.create(:artist)
|
@artist = FactoryBot.create(:artist, url_string: "http://foo.com")
|
||||||
@artist.stubs(:merge_version?).returns(false)
|
@artist.stubs(:merge_version?).returns(false)
|
||||||
end
|
end
|
||||||
|
|
||||||
should "create a new version" do
|
should "create a new version when an url is added" do
|
||||||
assert_difference("ArtistVersion.count") do
|
assert_difference("ArtistVersion.count") do
|
||||||
@artist.update(:url_string => "http://foo.com")
|
@artist.update(:url_string => "http://foo.com http://bar.com")
|
||||||
|
assert_equal(%w[http://bar.com http://foo.com], @artist.versions.last.url_array)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
should "create a new version when an url is removed" do
|
||||||
|
assert_difference("ArtistVersion.count") do
|
||||||
|
@artist.update(:url_string => "")
|
||||||
|
assert_equal(%w[], @artist.versions.last.url_array)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
should "create a new version when an url is marked inactive" do
|
||||||
|
assert_difference("ArtistVersion.count") do
|
||||||
|
@artist.update(:url_string => "-http://foo.com")
|
||||||
|
assert_equal(%w[-http://foo.com], @artist.versions.last.url_array)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
should "not create a new version when nothing has changed" do
|
||||||
|
assert_no_difference("ArtistVersion.count") do
|
||||||
|
@artist.save
|
||||||
|
assert_equal(%w[http://foo.com], @artist.versions.last.url_array)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
should "not save invalid urls" do
|
||||||
|
assert_no_difference("ArtistVersion.count") do
|
||||||
|
@artist.update(:url_string => "http://foo.com www.example.com")
|
||||||
|
assert_equal(%w[http://foo.com], @artist.versions.last.url_array)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user