Files
danbooru/app/models/artist_version.rb
evazion 3114ef3daf searchable: standardize the <field>_matches operator for text fields.
Standardize it so that all fields of type `text` are searchable with
`search[<field>_matches]`.

Before, the `<field>_matches` param was handled manually and some fields
were left out or handled inconsistently. Now it applies to all columns
of type `text`.

This does a full-text search on the field, so for example, searching
`/artist_commentaries?search[translated_description_matches]=smiling`
will match translated commentaries containing either the word "smiling",
"smiles", "smiled", or "smile".

Note that this only applies to columns defined as type `text`, not to
columns defined as `character varying`. The difference is that `text` is
used for fields containing free-form natural language, such as comments,
notes, forum posts, wiki pages, pool descriptions, etc, while `character
varying` is used for short strings not containing free-form language,
such as tag names, wiki page titles, urls, status fields, etc.

API changes:

* Add the `search[original_title_matches]`, `search[original_description_matches]`,
  `search[translated_title_matches]`, `search[translated_description_matches]` params
  to /artist_commentaries and /artist_commentary_versions.
* Remove the `search[name_matches]` and `search[group_name_matches]` params from /artist_versions.
* Remove the `search[title_matches]` param from /wiki_page_versions.
* Change the `search[name_matches]` param on /pools, /favorite_groups, and /pool_versions
  to do a full-text search instead of a substring match.
2022-09-22 01:52:13 -05:00

107 lines
2.3 KiB
Ruby

# frozen_string_literal: true
class ArtistVersion < ApplicationRecord
array_attribute :urls
array_attribute :other_names
belongs_to_updater
belongs_to :artist
def self.visible(user)
if policy(user).can_view_banned?
all
else
where(artist: Artist.unbanned)
end
end
module SearchMethods
def search(params)
q = search_attributes(params, :id, :created_at, :updated_at, :is_deleted, :is_banned, :name, :group_name, :urls, :other_names, :updater, :artist)
if params[:order] == "name"
q = q.order("artist_versions.name").default_order
else
q = q.apply_default_order(params)
end
q
end
end
extend SearchMethods
def previous
@previous ||= ArtistVersion.where("artist_id = ? and created_at < ?", artist_id, created_at).order("created_at desc").limit(1).to_a
@previous.first
end
def current
@previous ||= ArtistVersion.where(artist_id: artist_id).order("created_at desc").limit(1).to_a
@previous.first
end
def self.status_fields
{
name: "Renamed",
urls_changed: "URLs",
other_names_changed: "OtherNames",
group_name: "GroupName",
was_deleted: "Deleted",
was_undeleted: "Undeleted",
was_banned: "Banned",
was_unbanned: "Unbanned",
}
end
def other_names_changed(type)
other = send(type)
((other_names - other.other_names) | (other.other_names - other_names)).length.positive?
end
def urls_changed(type)
other = send(type)
((urls - other.urls) | (other.urls - urls)).length.positive?
end
def was_deleted(type)
other = send(type)
if type == "previous"
is_deleted && !other.is_deleted
else
!is_deleted && other.is_deleted
end
end
def was_undeleted(type)
other = send(type)
if type == "previous"
!is_deleted && other.is_deleted
else
is_deleted && !other.is_deleted
end
end
def was_banned(type)
other = send(type)
if type == "previous"
is_banned && !other.is_banned
else
!is_banned && other.is_banned
end
end
def was_unbanned(type)
other = send(type)
if type == "previous"
!is_banned && other.is_banned
else
is_banned && !other.is_banned
end
end
def self.available_includes
[:updater, :artist]
end
end