Add tag preview functionality for uploader

This commit is contained in:
Kira 2019-07-06 13:21:35 -07:00
parent 528500e316
commit 826d038cf5
6 changed files with 63 additions and 5 deletions

View File

@ -1,5 +1,5 @@
class TagsController < ApplicationController
before_action :member_only, :only => [:edit, :update]
before_action :member_only, :only => [:edit, :update, :preview]
respond_to :html, :xml, :json
def edit
@ -35,6 +35,15 @@ class TagsController < ApplicationController
end
end
def preview
@preview = TagsPreview.new(tags: params[:tags])
respond_with(@preview) do |format|
format.json do
render json: @preview.serializable_hash
end
end
end
def show
@tag = Tag.find(params[:id])
respond_with(@tag)

View File

@ -0,0 +1,41 @@
class TagsPreview
def initialize(tags: nil)
@tags = Tag.scan_tags(tags).map {|x| {a: x, type: 'tag'}}
aliases
implications
tag_types
end
def aliases
names = @tags.map{ |tag| tag[:a] }.reject {|y| y.blank?}
aliased = TagAlias.to_aliased_with_originals(names).reject {|k,v| k == v }
@tags.map! do |tag|
if aliased[tag[:a]]
{a: tag[:a], b: aliased[tag[:a]], type: 'alias'}
else
tag
end
end
end
def implications
names = @tags.map {|tag| tag[:b] || tag[:a] }
implications = TagImplication.descendants_with_originals(names)
implications.each do |imp|
@tags += imp[1].map { |x| {a: imp[0], b: x, type: 'implication'} }
end
end
def tag_types
names = @tags.map { |tag| tag[:b] || tag[:a] }
categories = Tag.categories_for(names)
@tags.map! do |x|
x[:tagType] = categories.fetch(x[:b] || x[:a], -1)
x
end
end
def serializable_hash(**options)
@tags
end
end

View File

@ -62,10 +62,14 @@ class TagAlias < TagRelationship
end
end
def self.to_aliased(names)
def self.to_aliased_with_originals(names)
Cache.get_multi(Array(names), "ta") do |tag|
ActiveRecord::Base.select_value_sql("select consequent_name from tag_aliases where status in ('active', 'processing') and antecedent_name = ?", tag) || tag.to_s
end.values
end
end
def self.to_aliased(names)
TagAlias.to_aliased_with_originals(names).values
end
def process!(update_topic: true)

View File

@ -14,7 +14,6 @@ class TagImplication < TagRelationship
validate :consequent_is_not_aliased
validate :wiki_pages_present, on: :create, unless: :skip_secondary_validations
scope :old, ->{where("created_at between ? and ?", 2.months.ago, 1.month.ago)}
scope :pending, ->{where(status: "pending")}
module DescendantMethods
extend ActiveSupport::Concern
@ -26,6 +25,10 @@ class TagImplication < TagRelationship
(names + active.where(antecedent_name: names).flat_map(&:descendant_names)).uniq
end
def descendants_with_originals(names)
active.where(antecedent_name: names).map { |x| [x.antecedent_name, x.descendant_names] }.uniq
end
def automatic_tags_for(names)
tags = []
tags += names.grep(/\A(.+)_\(cosplay\)\z/i) { "char:#{TagAlias.to_aliased([$1]).first}" }

View File

@ -580,7 +580,7 @@
this.preview.tags = [];
var self = this;
var data = {tags: this.tags};
jQuery.ajax("/tag/preview.json", {
jQuery.ajax("/tags/preview.json", {
method: 'POST',
type: 'POST',
data: data,

View File

@ -307,6 +307,7 @@ Rails.application.routes.draw do
resource :correction, :only => [:new, :create, :show], :controller => "tag_corrections"
collection do
get :autocomplete
post :preview
end
end
resources :tag_type_versions