[Artists] Allow locking artist pages

This commit is contained in:
Kira 2021-01-17 11:33:21 -08:00
parent e6bbf473e4
commit 814db91046
5 changed files with 45 additions and 3 deletions

View File

@ -100,7 +100,7 @@ private
def artist_params(context = nil)
permitted_params = %i[name other_names other_names_string group_name url_string notes]
permitted_params += [:is_active, :linked_user_id] if CurrentUser.is_janitor?
permitted_params += [:is_active, :linked_user_id, :is_locked] if CurrentUser.is_janitor?
permitted_params << :source if context == :new
params.fetch(:artist, {}).permit(permitted_params)

View File

@ -8,12 +8,15 @@ class Artist < ApplicationRecord
belongs_to_creator
before_validation :normalize_name
before_validation :normalize_other_names
validate :validate_user_can_edit?
validate :user_not_limited
validates :name, tag_name: true, uniqueness: true
validates :group_name, length: { maximum: 100 }
before_save :log_changes
after_save :create_version
after_save :categorize_tag
after_save :update_wiki
after_save :propagate_locked, if: :should_propagate_locked
after_save :clear_url_string_changed
@ -31,6 +34,15 @@ class Artist < ApplicationRecord
scope :banned, -> { where(is_banned: true) }
scope :unbanned, -> { where(is_banned: false) }
def log_changes
if name_changed?
ModAction.log(:artist_page_rename, {new_name: name, old_name: name_was})
end
if is_locked_changed?
ModAction.log(is_locked ? :artist_page_lock : :artist_page_unlock, {artist_page: id})
end
end
module UrlMethods
extend ActiveSupport::Concern
@ -429,6 +441,27 @@ class Artist < ApplicationRecord
end
end
module LockMethods
def propagate_locked
if wiki_page.present?
wiki_page.update_column(:is_locked, is_locked?)
end
end
def should_propagate_locked
saved_change_to_is_locked?
end
def validate_user_can_edit?
return if CurrentUser.is_janitor?
if is_locked?
errors.add(:base, "Artist is locked")
throw :abort
end
end
end
module SearchMethods
def any_other_name_matches(regex)
where(id: Artist.from("unnest(other_names) AS other_name").where("other_name ~ ?", regex))
@ -531,6 +564,7 @@ class Artist < ApplicationRecord
include NoteMethods
include TagMethods
include BanMethods
include LockMethods
extend SearchMethods
def status

View File

@ -8,6 +8,7 @@
<% end %>
<% if CurrentUser.is_janitor? %>
<%= f.input :linked_user_id, label: "Linked User ID" %>
<%= f.input :is_locked, label: "Locked" %>
<% end %>
<%= f.input :other_names_string, label: "Other names", as: :text, hint: '<b style="color: red;">NEW</b> Separate names with spaces, not commas. Use underscores for spaces inside names. Limit 25.'.html_safe %>
<%= f.input :group_name %>

View File

@ -0,0 +1,5 @@
class AddArtistLockStatus < ActiveRecord::Migration[6.0]
def change
add_column :artists, :is_locked, :boolean, nil: false, default: false
end
end

View File

@ -339,7 +339,8 @@ CREATE TABLE public.artists (
created_at timestamp without time zone,
updated_at timestamp without time zone,
other_names text[] DEFAULT '{}'::text[] NOT NULL,
linked_user_id integer
linked_user_id integer,
is_locked boolean DEFAULT false
);
@ -5082,6 +5083,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20200910015420'),
('20201113073842'),
('20201220172926'),
('20201220190335');
('20201220190335'),
('20210117173030');