forked from e621ng/e621ng
[Artists] Show error when changing notes with locked wiki page (#580)
* [Artists] Show error when changing notes with locked wiki page * [Tests] Add tests for editing artists with locked wiki pages
This commit is contained in:
parent
10156208ac
commit
8f8feac8aa
@ -8,6 +8,7 @@ class Artist < ApplicationRecord
|
||||
before_validation :normalize_name
|
||||
before_validation :normalize_other_names
|
||||
validate :validate_user_can_edit
|
||||
validate :wiki_page_not_locked
|
||||
validate :user_not_limited
|
||||
validates :name, tag_name: true, uniqueness: true, if: :name_changed?
|
||||
validates :name, :group_name, length: { maximum: 100 }
|
||||
@ -405,6 +406,15 @@ class Artist < ApplicationRecord
|
||||
throw :abort
|
||||
end
|
||||
end
|
||||
|
||||
def wiki_page_not_locked
|
||||
return if CurrentUser.is_janitor?
|
||||
|
||||
if @notes.present? && is_note_locked? && wiki_page&.body != @notes
|
||||
errors.add(:base, "Wiki page is locked")
|
||||
throw :abort
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
module SearchMethods
|
||||
@ -526,4 +536,9 @@ class Artist < ApplicationRecord
|
||||
def visible?
|
||||
true
|
||||
end
|
||||
|
||||
def is_note_locked?
|
||||
return false if CurrentUser.is_janitor?
|
||||
wiki_page&.is_locked? || false
|
||||
end
|
||||
end
|
||||
|
@ -15,6 +15,9 @@
|
||||
<%= f.input :group_name %>
|
||||
<%= f.input :url_string, :label => "URLs", as: :text, input_html: { size: "80x10", value: params.dig(:artist, :url_string) || @artist.urls.join("\n")}, hint: "You can prefix a URL with - to mark it as dead." %>
|
||||
|
||||
<%= f.input :notes, as: :dtext, limit: Danbooru.config.wiki_page_max_size %>
|
||||
<% if @artist.is_note_locked? %>
|
||||
<p>Artist is note locked. Notes cannot be edited.</p>
|
||||
<% end %>
|
||||
<%= f.input :notes, as: :dtext, limit: Danbooru.config.wiki_page_max_size, disabled: @artist.is_note_locked? %>
|
||||
<%= f.button :submit, "Submit" %>
|
||||
<% end %>
|
||||
|
@ -266,21 +266,21 @@ class ArtistTest < ActiveSupport::TestCase
|
||||
@artist = create(:artist, url_string: "http://foo.com")
|
||||
end
|
||||
|
||||
should "create a new version when an url is added" do
|
||||
should "create a new version when a url is added" do
|
||||
assert_difference("ArtistVersion.count") do
|
||||
@artist.update(url_string: "http://foo.com http://bar.com")
|
||||
assert_equal(%w[http://bar.com http://foo.com], @artist.versions.last.urls)
|
||||
end
|
||||
end
|
||||
|
||||
should "create a new version when an url is removed" do
|
||||
should "create a new version when a url is removed" do
|
||||
assert_difference("ArtistVersion.count") do
|
||||
@artist.update(url_string: "")
|
||||
assert_equal(%w[], @artist.versions.last.urls)
|
||||
end
|
||||
end
|
||||
|
||||
should "create a new version when an url is marked inactive" do
|
||||
should "create a new version when a 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.urls)
|
||||
@ -374,6 +374,24 @@ class ArtistTest < ActiveSupport::TestCase
|
||||
@artist.reload
|
||||
assert_equal("https://e621.net", @artist.url_string)
|
||||
end
|
||||
|
||||
should "not change notes when locked" do
|
||||
@artist.notes = "abababab"
|
||||
as(create(:user)) { @artist.save }
|
||||
|
||||
assert_equal("abababab", @artist.wiki_page.body)
|
||||
|
||||
@artist.wiki_page.update_column(:is_locked, true)
|
||||
|
||||
@artist.notes = "babababa"
|
||||
assert_no_difference(-> { ArtistVersion.count }) do
|
||||
as(create(:user)) { @artist.save }
|
||||
end
|
||||
|
||||
assert_equal("abababab", @artist.wiki_page.body)
|
||||
|
||||
assert_equal(["Wiki page is locked"], @artist.errors.full_messages)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user