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:
evazion
2018-10-04 19:37:18 -05:00
parent 03cc3dfa50
commit bb5f291112
2 changed files with 41 additions and 9 deletions

View File

@@ -2,12 +2,13 @@ class Artist < ApplicationRecord
extend Memoist
class RevertError < Exception ; end
attr_accessor :url_string_was
attr_accessor :url_string_changed
before_validation :normalize_name
after_save :create_version
after_save :categorize_tag
after_save :update_wiki
after_save :clear_url_string_changed
validates :name, tag_name: true, uniqueness: true
validate :validate_wiki, :on => :create
belongs_to_creator
@@ -181,16 +182,18 @@ class Artist < ApplicationRecord
end
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|
is_active, url = ArtistUrl.parse_prefix(url)
self.urls.find_or_initialize_by(url: url, is_active: is_active)
end.uniq(&:url)
self.url_string_changed = (url_string_was != url_string)
end
def url_string_changed?
url_string_was != url_string
def clear_url_string_changed
self.url_string_changed = false
end
def map_domain(x)
@@ -257,7 +260,7 @@ class Artist < ApplicationRecord
module VersionMethods
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?
merge_version
else

View File

@@ -493,15 +493,44 @@ class ArtistTest < ActiveSupport::TestCase
assert_equal(Tag.categories.artist, tag.category)
end
context "when updated" do
context "when saving" do
setup do
@artist = FactoryBot.create(:artist)
@artist = FactoryBot.create(:artist, url_string: "http://foo.com")
@artist.stubs(:merge_version?).returns(false)
end
should "create a new version" do
should "create a new version when an url is added" 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