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

@@ -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 : []