wiki pages: convert other_names to array (#3987).

This commit is contained in:
evazion
2018-11-13 18:08:20 -06:00
parent b660aeefd7
commit 308a5021b4
9 changed files with 55 additions and 50 deletions

View File

@@ -5,7 +5,7 @@ class WikiPagesController < ApplicationController
before_action :normalize_search_params, :only => [:index]
def new
@wiki_page = WikiPage.new(wiki_page_create_params)
@wiki_page = WikiPage.new(wiki_page_params(:create))
respond_with(@wiki_page)
end
@@ -54,13 +54,13 @@ class WikiPagesController < ApplicationController
end
def create
@wiki_page = WikiPage.create(wiki_page_create_params)
@wiki_page = WikiPage.create(wiki_page_params(:create))
respond_with(@wiki_page)
end
def update
@wiki_page = WikiPage.find(params[:id])
@wiki_page.update(wiki_page_update_params)
@wiki_page.update(wiki_page_params(:update))
respond_with(@wiki_page)
end
@@ -98,18 +98,11 @@ class WikiPagesController < ApplicationController
end
end
def wiki_page_create_params
permitted_params = %i[title body other_names skip_secondary_validations]
def wiki_page_params(context)
permitted_params = %i[body other_names other_names_string skip_secondary_validations]
permitted_params += %i[is_locked is_deleted] if CurrentUser.is_builder?
permitted_params += %i[title] if context == :create || CurrentUser.is_builder?
params.fetch(:wiki_page, {}).permit(permitted_params)
end
def wiki_page_update_params
permitted_params = %i[body other_names skip_secondary_validations]
permitted_params += %i[title is_locked is_deleted] if CurrentUser.is_builder?
params.fetch(:wiki_page, {}).permit(permitted_params)
end
end

View File

@@ -18,7 +18,7 @@ module WikiPagesHelper
end
def wiki_page_other_names_list(wiki_page)
names_html = wiki_page.other_names_array.map{|name| link_to(name, "http://www.pixiv.net/search.php?s_mode=s_tag_full&word=#{u(name)}", :class => "wiki-other-name")}
names_html = wiki_page.other_names.map{|name| link_to(name, "http://www.pixiv.net/search.php?s_mode=s_tag_full&word=#{u(name)}", :class => "wiki-other-name")}
names_html.join(" ").html_safe
end
end

View File

@@ -187,7 +187,7 @@ module Sources
def translate_tag(untranslated_tag)
return [] if untranslated_tag.blank?
translated_tag_names = WikiPage.active.other_names_equal(untranslated_tag).uniq.pluck(:title)
translated_tag_names = WikiPage.active.other_names_include(untranslated_tag).uniq.pluck(:title)
translated_tag_names = TagAlias.to_aliased(translated_tag_names)
translated_tags = Tag.where(name: translated_tag_names)

View File

@@ -4,14 +4,16 @@ class WikiPage < ApplicationRecord
before_save :normalize_title
before_save :normalize_other_names
after_save :create_version
belongs_to_creator
belongs_to_updater
validates_uniqueness_of :title, :case_sensitive => false
validates_presence_of :title
validates_presence_of :body, :unless => -> { is_deleted? || other_names.present? }
validate :validate_rename
validate :validate_not_locked
attr_accessor :skip_secondary_validations
array_attribute :other_names
belongs_to_creator
belongs_to_updater
has_one :tag, :foreign_key => "name", :primary_key => "title"
has_one :artist, -> {where(:is_active => true)}, :foreign_key => "name", :primary_key => "title"
has_many :versions, -> {order("wiki_page_versions.id ASC")}, :class_name => "WikiPageVersion", :dependent => :destroy
@@ -29,17 +31,16 @@ class WikiPage < ApplicationRecord
order("updated_at DESC").limit(25)
end
def other_names_equal(name)
query_sql = name.unicode_normalize(:nfkc).to_escaped_for_tsquery
where("other_names_index @@ to_tsquery('danbooru', E?)", query_sql)
def other_names_include(name)
where("wiki_pages.other_names @> ARRAY[?]", name.unicode_normalize(:nfkc))
end
def other_names_match(name)
if name =~ /\*/
subquery = WikiPage.from("unnest(string_to_array(other_names, ' ')) AS other_name").where("other_name ILIKE ?", name.to_escaped_for_sql_like)
subquery = WikiPage.from("unnest(other_names) AS other_name").where("other_name ILIKE ?", name.to_escaped_for_sql_like)
where(id: subquery)
else
other_names_equal(name)
other_names_include(name)
end
end
@@ -73,9 +74,9 @@ class WikiPage < ApplicationRecord
end
if params[:other_names_present].to_s.truthy?
q = q.where("other_names is not null and other_names != ''")
q = q.where("other_names is not null and other_names != '{}'")
elsif params[:other_names_present].to_s.falsy?
q = q.where("other_names is null or other_names = ''")
q = q.where("other_names is null or other_names = '{}'")
end
q = q.attribute_matches(:is_locked, params[:is_locked])
@@ -97,7 +98,7 @@ class WikiPage < ApplicationRecord
module ApiMethods
def hidden_attributes
super + [:body_index, :other_names_index]
super + [:body_index]
end
def method_attributes
@@ -145,8 +146,7 @@ class WikiPage < ApplicationRecord
end
def normalize_other_names
normalized_other_names = other_names.to_s.unicode_normalize(:nfkc).scan(/[^[:space:]]+/)
self.other_names = normalized_other_names.uniq.join(" ")
self.other_names = other_names.map { |name| name.unicode_normalize(:nfkc) }.uniq
end
def skip_secondary_validations=(value)
@@ -224,8 +224,4 @@ class WikiPage < ApplicationRecord
def visible?
artist.blank? || !artist.is_banned? || CurrentUser.is_builder?
end
def other_names_array
other_names.to_s.split(/[[:space:]]+/)
end
end

View File

@@ -1,4 +1,5 @@
class WikiPageVersion < ApplicationRecord
array_attribute :other_names
belongs_to :wiki_page
belongs_to_updater
belongs_to :artist, optional: true
@@ -45,8 +46,4 @@ class WikiPageVersion < ApplicationRecord
def category_name
Tag.category_for(title)
end
def other_names_array
other_names.to_s.split(/[[:space:]]+/)
end
end

View File

@@ -10,7 +10,7 @@
<h1 id="wiki-page-title"><%= @wiki_page.pretty_title %></h1>
<% end %>
<%= f.input :other_names, :as => :text, :label => "Other names (<a href='/wiki_pages/help:translated_tags'>view help</a>)".html_safe, :hint => "Names used for this tag on other sites such as Pixiv. Separate with spaces." %>
<%= f.input :other_names_string, as: :text, label: "Other names (#{link_to "help", wiki_pages_path(title: "help:translated_tags")})".html_safe, hint: "Names used for this tag on other sites such as Pixiv. Separate with spaces." %>
<%= dtext_field "wiki_page", "body" %>