pools: add ability to search for pools linking to given tag.

Add ability to search for pools linking to a given tag in the pool
description. Example:

    https://danbooru.donmai.us/pools?search[linked_to]=touhou

(This isn't actually exposed in the UI to avoid cluttering the pool
search form with rarely used options.)

Pools with broken links can be found here:

    https://danbooru.donmai.us/dtext_links?search[has_linked_tag]=No&search[has_linked_wiki]=No&search[model_type]=Pool

Lays the groundwork for fixing #4629.
This commit is contained in:
evazion
2022-01-15 20:16:06 -06:00
parent c3c4f5a2a7
commit 33103f6dc4
10 changed files with 72 additions and 42 deletions

View File

@@ -0,0 +1,36 @@
# frozen_string_literal: true
module HasDtextLinks
extend ActiveSupport::Concern
class_methods do
# Declare a field that has DText links. Any wiki links or external links
# contained in this field will be saved in the dtext_links table whenever
# the field is updated. This allows finding wiki pages, forum posts, and
# pool descriptions linking to a given tag.
#
# @param attribute [Symbol] the name of the DText field.
def has_dtext_links(attribute)
has_many :dtext_links, as: :model, dependent: :destroy
before_save :update_dtext_links, if: :dtext_links_changed?
define_method(:dtext_links_changed?) do
attribute_changed?(attribute) && DText.dtext_links_differ?(self[attribute], attribute_was(attribute))
end
define_method(:update_dtext_links) do
self.dtext_links = DtextLink.new_from_dtext(self[attribute])
end
end
# Return pages (e.g. wikis, forum posts, pool descriptions) that link to the given wiki page.
def linked_to(title)
where(dtext_links: DtextLink.where(model_type: name).wiki_link.where(link_target: WikiPage.normalize_title(title)))
end
# Return pages that don't link to the given wiki page.
def not_linked_to(title)
where.not(dtext_links: DtextLink.where(model_type: name).wiki_link.where(link_target: WikiPage.normalize_title(title)))
end
end
end