artists: convert other_names to array (#3987).
This commit is contained in:
@@ -105,7 +105,7 @@ private
|
|||||||
end
|
end
|
||||||
|
|
||||||
def artist_params
|
def artist_params
|
||||||
permitted_params = %i[name other_names other_names_comma group_name url_string notes]
|
permitted_params = %i[name other_names other_names_string group_name url_string notes]
|
||||||
permitted_params << :is_active if CurrentUser.is_builder?
|
permitted_params << :is_active if CurrentUser.is_builder?
|
||||||
|
|
||||||
params.fetch(:artist, {}).permit(permitted_params)
|
params.fetch(:artist, {}).permit(permitted_params)
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ div#c-artists, div#excerpt {
|
|||||||
textarea {
|
textarea {
|
||||||
height: 10em;
|
height: 10em;
|
||||||
|
|
||||||
&#artist_other_names_comma {
|
&#artist_other_names_string {
|
||||||
height: 3em;
|
height: 3em;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,8 +3,10 @@ class Artist < ApplicationRecord
|
|||||||
class RevertError < Exception ; end
|
class RevertError < Exception ; end
|
||||||
|
|
||||||
attr_accessor :url_string_changed
|
attr_accessor :url_string_changed
|
||||||
|
array_attribute :other_names
|
||||||
|
|
||||||
before_validation :normalize_name
|
before_validation :normalize_name
|
||||||
|
before_validation :normalize_other_names
|
||||||
after_save :create_version
|
after_save :create_version
|
||||||
after_save :categorize_tag
|
after_save :categorize_tag
|
||||||
after_save :update_wiki
|
after_save :update_wiki
|
||||||
@@ -239,16 +241,8 @@ class Artist < ApplicationRecord
|
|||||||
name.tr("_", " ")
|
name.tr("_", " ")
|
||||||
end
|
end
|
||||||
|
|
||||||
def other_names_array
|
def normalize_other_names
|
||||||
other_names.try(:split, /[[:space:]]+/)
|
self.other_names = other_names.map { |x| Artist.normalize_name(x) }
|
||||||
end
|
|
||||||
|
|
||||||
def other_names_comma
|
|
||||||
other_names_array.try(:join, ", ")
|
|
||||||
end
|
|
||||||
|
|
||||||
def other_names_comma=(string)
|
|
||||||
self.other_names = string.split(/,/).map {|x| Artist.normalize_name(x)}.join(" ")
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -308,7 +302,7 @@ class Artist < ApplicationRecord
|
|||||||
self.name = version.name
|
self.name = version.name
|
||||||
self.url_string = version.urls.join("\n")
|
self.url_string = version.urls.join("\n")
|
||||||
self.is_active = version.is_active
|
self.is_active = version.is_active
|
||||||
self.other_names = version.other_names.join(" ")
|
self.other_names = version.other_names
|
||||||
self.group_name = version.group_name
|
self.group_name = version.group_name
|
||||||
save
|
save
|
||||||
end
|
end
|
||||||
@@ -466,13 +460,21 @@ class Artist < ApplicationRecord
|
|||||||
where(name: normalize_name(name))
|
where(name: normalize_name(name))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def any_other_name_matches(regex)
|
||||||
|
where(id: Artist.from("unnest(other_names) AS other_name").where("other_name ~ ?", regex))
|
||||||
|
end
|
||||||
|
|
||||||
|
def any_other_name_like(name)
|
||||||
|
where(id: Artist.from("unnest(other_names) AS other_name").where("other_name LIKE ?", name.to_escaped_for_sql_like))
|
||||||
|
end
|
||||||
|
|
||||||
def any_name_matches(query)
|
def any_name_matches(query)
|
||||||
if query =~ %r!\A/(.*)/\z!
|
if query =~ %r!\A/(.*)/\z!
|
||||||
where_regex(:name, $1).or(where_regex(:other_names, $1)).or(where_regex(:group_name, $1))
|
where_regex(:name, $1).or(any_other_name_matches($1)).or(where_regex(:group_name, $1))
|
||||||
else
|
else
|
||||||
normalized_name = normalize_name(query)
|
normalized_name = normalize_name(query)
|
||||||
normalized_name = "*#{normalized_name}*" unless normalized_name.include?("*")
|
normalized_name = "*#{normalized_name}*" unless normalized_name.include?("*")
|
||||||
where_like(:name, normalized_name).or(where_like(:other_names, normalized_name)).or(where_like(:group_name, normalized_name))
|
where_like(:name, normalized_name).or(any_other_name_like(normalized_name)).or(where_like(:group_name, normalized_name))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -492,9 +494,12 @@ class Artist < ApplicationRecord
|
|||||||
q = super
|
q = super
|
||||||
|
|
||||||
q = q.search_text_attribute(:name, params)
|
q = q.search_text_attribute(:name, params)
|
||||||
q = q.search_text_attribute(:other_names, params)
|
|
||||||
q = q.search_text_attribute(:group_name, params)
|
q = q.search_text_attribute(:group_name, params)
|
||||||
|
|
||||||
|
if params[:any_other_name_like]
|
||||||
|
q = q.any_other_name_like(params[:any_other_name_like])
|
||||||
|
end
|
||||||
|
|
||||||
if params[:any_name_matches].present?
|
if params[:any_name_matches].present?
|
||||||
q = q.any_name_matches(params[:any_name_matches])
|
q = q.any_name_matches(params[:any_name_matches])
|
||||||
end
|
end
|
||||||
@@ -536,12 +541,6 @@ class Artist < ApplicationRecord
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
module ApiMethods
|
|
||||||
def hidden_attributes
|
|
||||||
super + [:other_names_index]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
include UrlMethods
|
include UrlMethods
|
||||||
include NameMethods
|
include NameMethods
|
||||||
include GroupMethods
|
include GroupMethods
|
||||||
@@ -551,7 +550,6 @@ class Artist < ApplicationRecord
|
|||||||
include TagMethods
|
include TagMethods
|
||||||
include BanMethods
|
include BanMethods
|
||||||
extend SearchMethods
|
extend SearchMethods
|
||||||
include ApiMethods
|
|
||||||
|
|
||||||
def status
|
def status
|
||||||
if is_banned? && is_active?
|
if is_banned? && is_active?
|
||||||
|
|||||||
@@ -68,7 +68,7 @@ class ArtistVersion < ApplicationRecord
|
|||||||
end
|
end
|
||||||
|
|
||||||
def other_names_diff(version)
|
def other_names_diff(version)
|
||||||
latest_names = artist.other_names_array || []
|
latest_names = artist.other_names || []
|
||||||
new_names = other_names
|
new_names = other_names
|
||||||
old_names = version.present? ? version.other_names : []
|
old_names = version.present? ? version.other_names : []
|
||||||
|
|
||||||
|
|||||||
@@ -14,7 +14,7 @@
|
|||||||
<p><%= @artist.name %></p>
|
<p><%= @artist.name %></p>
|
||||||
<% end %>
|
<% end %>
|
||||||
</div>
|
</div>
|
||||||
<%= f.input :other_names_comma, :hint => "Separate with commas", :as => :text, :label => "Other names" %>
|
<%= f.input :other_names_string, :hint => "Separate with spaces", :as => :text, :label => "Other names" %>
|
||||||
<%= f.input :group_name %>
|
<%= f.input :group_name %>
|
||||||
<%= f.input :url_string, :label => "URLs", :as => :text, :input_html => {:size => "50x5", :value => params.dig(:artist, :url_string) || @artist.urls.join("\n")}, :hint => "You can prefix a URL with - to mark it as dead." %>
|
<%= f.input :url_string, :label => "URLs", :as => :text, :input_html => {:size => "50x5", :value => params.dig(:artist, :url_string) || @artist.urls.join("\n")}, :hint => "You can prefix a URL with - to mark it as dead." %>
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
<li><strong>Tag Alias</strong> <%= artist.tag_alias_name %></li>
|
<li><strong>Tag Alias</strong> <%= artist.tag_alias_name %></li>
|
||||||
<% end %>
|
<% end %>
|
||||||
<% if artist.other_names.present? %>
|
<% if artist.other_names.present? %>
|
||||||
<li><strong>Other Names</strong> <%= link_to_artists(artist.other_names.split(/ /)) %></li>
|
<li><strong>Other Names</strong> <%= link_to_artists(artist.other_names) %></li>
|
||||||
<% end %>
|
<% end %>
|
||||||
<% if artist.group_name.present? %>
|
<% if artist.group_name.present? %>
|
||||||
<li><strong>Group</strong> <%= link_to_artist(artist.group_name) %></li>
|
<li><strong>Group</strong> <%= link_to_artist(artist.group_name) %></li>
|
||||||
|
|||||||
@@ -19,7 +19,7 @@
|
|||||||
(group: <%= link_to(artist.group_name, artist_path(artist)) %>)
|
(group: <%= link_to(artist.group_name, artist_path(artist)) %>)
|
||||||
<% end %>
|
<% end %>
|
||||||
</td>
|
</td>
|
||||||
<td><%= artist.other_names %></td>
|
<td><%= artist.other_names_string %></td>
|
||||||
<td><%= artist.status %></td>
|
<td><%= artist.status %></td>
|
||||||
<% end %>
|
<% end %>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
class ChangeOtherNamesToArrayOnArtists < ActiveRecord::Migration[5.2]
|
||||||
|
def up
|
||||||
|
Artist.without_timeout do
|
||||||
|
remove_index :artists, name: "index_artists_on_other_names_trgm"
|
||||||
|
change_column :artists, :other_names, "text[]", using: "array_remove(regexp_split_to_array(other_names, '\\s+'), '')", default: "{}"
|
||||||
|
add_index :artists, :other_names, using: :gin
|
||||||
|
|
||||||
|
remove_column :artists, :other_names_index
|
||||||
|
execute "DROP TRIGGER trigger_artists_on_update ON artists"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def down
|
||||||
|
Artist.without_timeout do
|
||||||
|
remove_index :artists, :other_names
|
||||||
|
change_column :artists, :other_names, "text", using: "array_to_string(other_names, ' ')", default: nil
|
||||||
|
add_index :artists, :other_names, name: "index_artists_on_other_names_trgm", using: :gin, opclass: :gin_trgm_ops
|
||||||
|
|
||||||
|
add_column :artists, :other_names_index, :tsvector
|
||||||
|
execute "CREATE TRIGGER trigger_artists_on_update BEFORE INSERT OR UPDATE ON artists FOR EACH ROW EXECUTE PROCEDURE tsvector_update_trigger('other_names_index', 'public.danbooru', 'other_names')"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -765,8 +765,7 @@ CREATE TABLE public.artists (
|
|||||||
creator_id integer NOT NULL,
|
creator_id integer NOT NULL,
|
||||||
is_active boolean DEFAULT true NOT NULL,
|
is_active boolean DEFAULT true NOT NULL,
|
||||||
is_banned boolean DEFAULT false NOT NULL,
|
is_banned boolean DEFAULT false NOT NULL,
|
||||||
other_names text,
|
other_names text[] DEFAULT '{}'::text[],
|
||||||
other_names_index tsvector,
|
|
||||||
group_name character varying,
|
group_name character varying,
|
||||||
created_at timestamp without time zone,
|
created_at timestamp without time zone,
|
||||||
updated_at timestamp without time zone
|
updated_at timestamp without time zone
|
||||||
@@ -5078,17 +5077,10 @@ CREATE INDEX index_artists_on_name_trgm ON public.artists USING gin (name public
|
|||||||
|
|
||||||
|
|
||||||
--
|
--
|
||||||
-- Name: index_artists_on_other_names_index; Type: INDEX; Schema: public; Owner: -
|
-- Name: index_artists_on_other_names; Type: INDEX; Schema: public; Owner: -
|
||||||
--
|
--
|
||||||
|
|
||||||
CREATE INDEX index_artists_on_other_names_index ON public.artists USING gin (other_names_index);
|
CREATE INDEX index_artists_on_other_names ON public.artists USING gin (other_names);
|
||||||
|
|
||||||
|
|
||||||
--
|
|
||||||
-- Name: index_artists_on_other_names_trgm; Type: INDEX; Schema: public; Owner: -
|
|
||||||
--
|
|
||||||
|
|
||||||
CREATE INDEX index_artists_on_other_names_trgm ON public.artists USING gin (other_names public.gin_trgm_ops);
|
|
||||||
|
|
||||||
|
|
||||||
--
|
--
|
||||||
@@ -7345,13 +7337,6 @@ CREATE INDEX index_wiki_pages_on_updated_at ON public.wiki_pages USING btree (up
|
|||||||
CREATE TRIGGER insert_favorites_trigger BEFORE INSERT ON public.favorites FOR EACH ROW EXECUTE PROCEDURE public.favorites_insert_trigger();
|
CREATE TRIGGER insert_favorites_trigger BEFORE INSERT ON public.favorites FOR EACH ROW EXECUTE PROCEDURE public.favorites_insert_trigger();
|
||||||
|
|
||||||
|
|
||||||
--
|
|
||||||
-- Name: artists trigger_artists_on_update; Type: TRIGGER; Schema: public; Owner: -
|
|
||||||
--
|
|
||||||
|
|
||||||
CREATE TRIGGER trigger_artists_on_update BEFORE INSERT OR UPDATE ON public.artists FOR EACH ROW EXECUTE PROCEDURE tsvector_update_trigger('other_names_index', 'public.danbooru', 'other_names');
|
|
||||||
|
|
||||||
|
|
||||||
--
|
--
|
||||||
-- Name: comments trigger_comments_on_update; Type: TRIGGER; Schema: public; Owner: -
|
-- Name: comments trigger_comments_on_update; Type: TRIGGER; Schema: public; Owner: -
|
||||||
--
|
--
|
||||||
@@ -7574,6 +7559,7 @@ INSERT INTO "schema_migrations" (version) VALUES
|
|||||||
('20181108205842'),
|
('20181108205842'),
|
||||||
('20181113174914'),
|
('20181113174914'),
|
||||||
('20181114180205'),
|
('20181114180205'),
|
||||||
('20181114185032');
|
('20181114185032'),
|
||||||
|
('20181114202744');
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -407,8 +407,8 @@ class ArtistTest < ActiveSupport::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
should "normalize its other names" do
|
should "normalize its other names" do
|
||||||
artist = FactoryBot.create(:artist, :name => "a1", :other_names_comma => "aaa, bbb, ccc ddd")
|
artist = FactoryBot.create(:artist, :name => "a1", :other_names => "aaa bbb ccc_ddd")
|
||||||
assert_equal("aaa, bbb, ccc_ddd", artist.other_names_comma)
|
assert_equal("aaa bbb ccc_ddd", artist.other_names_string)
|
||||||
end
|
end
|
||||||
|
|
||||||
should "search on its name should return results" do
|
should "search on its name should return results" do
|
||||||
@@ -421,11 +421,11 @@ class ArtistTest < ActiveSupport::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
should "search on other names should return matches" do
|
should "search on other names should return matches" do
|
||||||
artist = FactoryBot.create(:artist, :name => "artist", :other_names_comma => "aaa, ccc ddd")
|
artist = FactoryBot.create(:artist, :name => "artist", :other_names_string => "aaa ccc_ddd")
|
||||||
|
|
||||||
assert_nil(Artist.search(other_names_like: "*artist*").first)
|
assert_nil(Artist.search(any_other_name_like: "*artist*").first)
|
||||||
assert_not_nil(Artist.search(other_names_like: "*aaa*").first)
|
assert_not_nil(Artist.search(any_other_name_like: "*aaa*").first)
|
||||||
assert_not_nil(Artist.search(other_names_like: "*ccc_ddd*").first)
|
assert_not_nil(Artist.search(any_other_name_like: "*ccc_ddd*").first)
|
||||||
assert_not_nil(Artist.search(name: "artist").first)
|
assert_not_nil(Artist.search(name: "artist").first)
|
||||||
assert_not_nil(Artist.search(:any_name_matches => "aaa").first)
|
assert_not_nil(Artist.search(:any_name_matches => "aaa").first)
|
||||||
assert_not_nil(Artist.search(:any_name_matches => "/a/").first)
|
assert_not_nil(Artist.search(:any_name_matches => "/a/").first)
|
||||||
@@ -478,7 +478,7 @@ class ArtistTest < ActiveSupport::TestCase
|
|||||||
assert_equal(%w[yyy], first_version.other_names)
|
assert_equal(%w[yyy], first_version.other_names)
|
||||||
artist.revert_to!(first_version)
|
artist.revert_to!(first_version)
|
||||||
artist.reload
|
artist.reload
|
||||||
assert_equal("yyy", artist.other_names)
|
assert_equal(%w[yyy], artist.other_names)
|
||||||
end
|
end
|
||||||
|
|
||||||
should "update the category of the tag when created" do
|
should "update the category of the tag when created" do
|
||||||
|
|||||||
Reference in New Issue
Block a user