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