artists: convert other_names to array (#3987).

This commit is contained in:
evazion
2018-11-14 15:54:51 -06:00
parent 741462ae68
commit 41ff05c121
10 changed files with 60 additions and 53 deletions

View File

@@ -105,7 +105,7 @@ private
end
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?
params.fetch(:artist, {}).permit(permitted_params)

View File

@@ -14,7 +14,7 @@ div#c-artists, div#excerpt {
textarea {
height: 10em;
&#artist_other_names_comma {
&#artist_other_names_string {
height: 3em;
}
}

View File

@@ -3,8 +3,10 @@ class Artist < ApplicationRecord
class RevertError < Exception ; end
attr_accessor :url_string_changed
array_attribute :other_names
before_validation :normalize_name
before_validation :normalize_other_names
after_save :create_version
after_save :categorize_tag
after_save :update_wiki
@@ -239,16 +241,8 @@ class Artist < ApplicationRecord
name.tr("_", " ")
end
def other_names_array
other_names.try(:split, /[[:space:]]+/)
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(" ")
def normalize_other_names
self.other_names = other_names.map { |x| Artist.normalize_name(x) }
end
end
@@ -308,7 +302,7 @@ class Artist < ApplicationRecord
self.name = version.name
self.url_string = version.urls.join("\n")
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
save
end
@@ -466,13 +460,21 @@ class Artist < ApplicationRecord
where(name: normalize_name(name))
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)
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
normalized_name = normalize_name(query)
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
@@ -492,9 +494,12 @@ class Artist < ApplicationRecord
q = super
q = q.search_text_attribute(:name, params)
q = q.search_text_attribute(:other_names, 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?
q = q.any_name_matches(params[:any_name_matches])
end
@@ -536,12 +541,6 @@ class Artist < ApplicationRecord
end
end
module ApiMethods
def hidden_attributes
super + [:other_names_index]
end
end
include UrlMethods
include NameMethods
include GroupMethods
@@ -551,7 +550,6 @@ class Artist < ApplicationRecord
include TagMethods
include BanMethods
extend SearchMethods
include ApiMethods
def status
if is_banned? && is_active?

View File

@@ -68,7 +68,7 @@ class ArtistVersion < ApplicationRecord
end
def other_names_diff(version)
latest_names = artist.other_names_array || []
latest_names = artist.other_names || []
new_names = other_names
old_names = version.present? ? version.other_names : []

View File

@@ -14,7 +14,7 @@
<p><%= @artist.name %></p>
<% end %>
</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 :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." %>

View File

@@ -7,7 +7,7 @@
<li><strong>Tag Alias</strong> <%= artist.tag_alias_name %></li>
<% end %>
<% 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 %>
<% if artist.group_name.present? %>
<li><strong>Group</strong> <%= link_to_artist(artist.group_name) %></li>

View File

@@ -19,7 +19,7 @@
(group: <%= link_to(artist.group_name, artist_path(artist)) %>)
<% end %>
</td>
<td><%= artist.other_names %></td>
<td><%= artist.other_names_string %></td>
<td><%= artist.status %></td>
<% end %>
<% end %>

View File

@@ -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

View File

@@ -765,8 +765,7 @@ CREATE TABLE public.artists (
creator_id integer NOT NULL,
is_active boolean DEFAULT true NOT NULL,
is_banned boolean DEFAULT false NOT NULL,
other_names text,
other_names_index tsvector,
other_names text[] DEFAULT '{}'::text[],
group_name character varying,
created_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);
--
-- 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);
CREATE INDEX index_artists_on_other_names ON public.artists USING gin (other_names);
--
@@ -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();
--
-- 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: -
--
@@ -7574,6 +7559,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20181108205842'),
('20181113174914'),
('20181114180205'),
('20181114185032');
('20181114185032'),
('20181114202744');

View File

@@ -407,8 +407,8 @@ class ArtistTest < ActiveSupport::TestCase
end
should "normalize its other names" do
artist = FactoryBot.create(:artist, :name => "a1", :other_names_comma => "aaa, bbb, ccc ddd")
assert_equal("aaa, bbb, ccc_ddd", artist.other_names_comma)
artist = FactoryBot.create(:artist, :name => "a1", :other_names => "aaa bbb ccc_ddd")
assert_equal("aaa bbb ccc_ddd", artist.other_names_string)
end
should "search on its name should return results" do
@@ -421,11 +421,11 @@ class ArtistTest < ActiveSupport::TestCase
end
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_not_nil(Artist.search(other_names_like: "*aaa*").first)
assert_not_nil(Artist.search(other_names_like: "*ccc_ddd*").first)
assert_nil(Artist.search(any_other_name_like: "*artist*").first)
assert_not_nil(Artist.search(any_other_name_like: "*aaa*").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(:any_name_matches => "aaa").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)
artist.revert_to!(first_version)
artist.reload
assert_equal("yyy", artist.other_names)
assert_equal(%w[yyy], artist.other_names)
end
should "update the category of the tag when created" do