Moved non-config tag category info to its own class

This commit is contained in:
BrokenEagle
2017-11-07 16:10:33 -08:00
parent 7cb45fc8d3
commit 6838901aac
17 changed files with 128 additions and 77 deletions

View File

@@ -118,7 +118,7 @@ class PostQueryBuilder
relation = add_range_relation(q[:filesize], "posts.file_size", relation)
relation = add_range_relation(q[:date], "posts.created_at", relation)
relation = add_range_relation(q[:age], "posts.created_at", relation)
Danbooru.config.full_tag_config_info.each_key do |category|
TagCategory.categories.each do |category|
relation = add_range_relation(q["#{category}_tag_count".to_sym], "posts.tag_count_#{category}", relation)
end
relation = add_range_relation(q[:post_tag_count], "posts.tag_count", relation)
@@ -511,11 +511,11 @@ class PostQueryBuilder
when "tagcount_asc"
relation = relation.order("posts.tag_count ASC")
when /(#{Danbooru.config.short_tag_name_mapping.keys.join("|")})tags(?:\Z|_desc)/
relation = relation.order("posts.tag_count_#{Danbooru.config.short_tag_name_mapping[$1]} DESC")
when /(#{TagCategory.short_name_regex})tags(?:\Z|_desc)/
relation = relation.order("posts.tag_count_#{TagCategory.short_name_mapping[$1]} DESC")
when /(#{Danbooru.config.short_tag_name_mapping.keys.join("|")})tags_asc/
relation = relation.order("posts.tag_count_#{Danbooru.config.short_tag_name_mapping[$1]} ASC")
when /(#{TagCategory.short_name_regex})tags_asc/
relation = relation.order("posts.tag_count_#{TagCategory.short_name_mapping[$1]} ASC")
when "rank"
relation = relation.order("log(3, posts.score) + (extract(epoch from posts.created_at) - extract(epoch from timestamp '2005-05-24')) / 35000 DESC")

View File

@@ -0,0 +1,78 @@
class TagCategory
module Mappings
# Returns a hash mapping various tag categories to a numerical value.
def mapping
@@mapping ||= Hash[
Danbooru.config.full_tag_config_info.map {|k,v| v["extra"].map {|y| [y,v["category"]]}}
.reduce([],:+)]
.update(Hash[Danbooru.config.full_tag_config_info.map {|k,v| [v["short"],v["category"]]}])
.update( Hash[Danbooru.config.full_tag_config_info.map {|k,v| [k,v["category"]]}])
end
# Returns a hash mapping more suited for views
def canonical_mapping
@@canonical_mapping ||= Hash[Danbooru.config.full_tag_config_info.map {|k,v| [k.capitalize,v["category"]]}]
end
# Returns a hash mapping numerical category values to their string equivalent.
def reverse_mapping
@@reverse_mapping ||= Hash[Danbooru.config.full_tag_config_info.map {|k,v| [v["category"],k]}]
end
# Returns a hash mapping for the short name usage in metatags
def short_name_mapping
@@short_name_mapping ||= Hash[Danbooru.config.full_tag_config_info.map {|k,v| [v["short"],k]}]
end
# Returns a hash mapping for humanized_essential_tag_string (models/post.rb)
def humanized_mapping
@@humanized_mapping ||= Hash[Danbooru.config.full_tag_config_info.map {|k,v| [k,v["humanized"]]}]
end
# Returns a hash mapping for humanized_essential_tag_string (models/post.rb)
def header_mapping
@@header_mapping ||= Hash[Danbooru.config.full_tag_config_info.map {|k,v| [k,v["header"]]}]
end
# Returns a hash mapping for related tag buttons (javascripts/related_tag.js)
def related_button_mapping
@@related_button_mapping ||= Hash[Danbooru.config.full_tag_config_info.map {|k,v| [k,v["relatedbutton"]]}]
end
end
module Lists
def categories
@@categories ||= Danbooru.config.full_tag_config_info.keys
end
def short_name_list
@@short_name_list ||= short_name_mapping.keys
end
def humanized_list
Danbooru.config.humanized_tag_category_list
end
def split_header_list
Danbooru.config.split_tag_header_list
end
def categorized_list
Danbooru.config.categorized_tag_list
end
def related_button_list
Danbooru.config.related_tag_button_list
end
end
module Regexes
def short_name_regex
@@short_name_regex ||= short_name_list.join("|")
end
end
extend Mappings
extend Lists
extend Regexes
end

View File

@@ -626,11 +626,11 @@ class Post < ApplicationRecord
def set_tag_counts
self.tag_count = 0
Danbooru.config.full_tag_config_info.each_key {|x| set_tag_count(x,0)}
TagCategory.categories {|x| set_tag_count(x,0)}
categories = Tag.categories_for(tag_array, :disable_caching => true)
categories.each_value do |category|
self.tag_count += 1
inc_tag_count(Danbooru.config.reverse_tag_category_mapping[category])
inc_tag_count(TagCategory.reverse_mapping[category])
end
end
@@ -913,7 +913,7 @@ class Post < ApplicationRecord
@typed_tags ||= {}
@typed_tags[name] ||= begin
tag_array.select do |tag|
tag_categories[tag] == Danbooru.config.tag_category_mapping[name]
tag_categories[tag] == TagCategory.mapping[name]
end
end
end
@@ -926,20 +926,19 @@ class Post < ApplicationRecord
@humanized_essential_tag_string ||= Cache.get("hets-#{id}", 1.hour.to_i) do
string = []
Danbooru.config.humanized_tag_category_list.each do |category|
humanizeddata = Danbooru.config.full_tag_config_info[category]["humanized"]
typetags = typed_tags(category) - humanizeddata["exclusion"]
if humanizeddata["slice"] > 0
typetags = typetags.slice(0,humanizeddata["slice"]) + (typetags.length > humanizeddata["slice"] ? ["others"] : [])
TagCategory.humanized_list.each do |category|
typetags = typed_tags(category) - TagCategory.humanized_mapping[category]["exclusion"]
if TagCategory.humanized_mapping[category]["slice"] > 0
typetags = typetags.slice(0,TagCategory.humanized_mapping[category]["slice"]) + (typetags.length > TagCategory.humanized_mapping[category]["slice"] ? ["others"] : [])
end
if humanizeddata["regexmap"] != //
if TagCategory.humanized_mapping[category]["regexmap"] != //
typetags = typetags.map do |tag|
tag.match(humanizeddata["regexmap"])[1]
tag.match(TagCategory.humanized_mapping[category]["regexmap"])[1]
end
end
if typetags.any?
if category != "copyright" || typed_tags("character").any?
string << humanizeddata["formatstr"] % typetags.to_sentence
string << TagCategory.humanized_mapping[category]["formatstr"] % typetags.to_sentence
else
string << typetags.to_sentence
end
@@ -949,7 +948,7 @@ class Post < ApplicationRecord
end
end
Danbooru.config.full_tag_config_info.each_key do |category|
TagCategory.categories.each do |category|
define_method("tag_string_#{category}") do
typed_tags(category).join(" ")
end
@@ -1140,7 +1139,7 @@ class Post < ApplicationRecord
module CountMethods
def fix_post_counts(post)
post.set_tag_counts
args = Hash[Danbooru.config.full_tag_config_info.keys.map {|x| ["tag_count_#{x}",post.send("tag_count_#{x}")]}].update(:tag_count => post.tag_count)
args = Hash[TagCategory.categories.map {|x| ["tag_count_#{x}",post.send("tag_count_#{x}")]}].update(:tag_count => post.tag_count)
post.update_columns(args)
end
@@ -1523,7 +1522,7 @@ class Post < ApplicationRecord
end
def method_attributes
list = super + [:uploader_name, :has_large, :has_visible_children, :children_ids] + Danbooru.config.full_tag_config_info.keys.map {|x| "tag_string_#{x}".to_sym}
list = super + [:uploader_name, :has_large, :has_visible_children, :children_ids] + TagCategory.categories.map {|x| "tag_string_#{x}".to_sym}
if visible?
list += [:file_url, :large_file_url, :preview_file_url]
end

View File

@@ -1,7 +1,7 @@
class Tag < ApplicationRecord
COSINE_SIMILARITY_RELATED_TAG_THRESHOLD = 1000
METATAGS = "-user|user|-approver|approver|commenter|comm|noter|noteupdater|artcomm|-pool|pool|ordpool|-favgroup|favgroup|-fav|fav|ordfav|md5|-rating|rating|-locked|locked|width|height|mpixels|ratio|score|favcount|filesize|source|-source|id|-id|date|age|order|limit|-status|status|tagcount|parent|-parent|child|pixiv_id|pixiv|search|upvote|downvote|filetype|-filetype|flagger|-flagger|appealer|-appealer|" +
Danbooru.config.short_tag_name_mapping.keys.map {|x| "#{x}tags"}.join("|")
TagCategory.short_name_list.map {|x| "#{x}tags"}.join("|")
SUBQUERY_METATAGS = "commenter|comm|noter|noteupdater|artcomm|flagger|-flagger|appealer|-appealer"
attr_accessible :category, :as => [:moderator, :gold, :platinum, :member, :anonymous, :default, :builder, :admin]
attr_accessible :is_locked, :as => [:moderator, :admin]
@@ -27,18 +27,18 @@ class Tag < ApplicationRecord
end
class CategoryMapping
Danbooru.config.reverse_tag_category_mapping.each do |value, category|
TagCategory.reverse_mapping.each do |value, category|
define_method(category) do
value
end
end
def regexp
@regexp ||= Regexp.compile(Danbooru.config.tag_category_mapping.keys.sort_by {|x| -x.size}.join("|"))
@regexp ||= Regexp.compile(TagCategory.mapping.keys.sort_by {|x| -x.size}.join("|"))
end
def value_for(string)
Danbooru.config.tag_category_mapping[string.to_s.downcase] || 0
TagCategory.mapping[string.to_s.downcase] || 0
end
end
@@ -125,7 +125,7 @@ class Tag < ApplicationRecord
end
def category_name
Danbooru.config.reverse_tag_category_mapping[category].capitalize
TagCategory.reverse_mapping[category].capitalize
end
def update_category_cache_for_all
@@ -141,7 +141,7 @@ class Tag < ApplicationRecord
Post.raw_tag_match(name).where("true /* Tag#update_category_post_counts */").find_each do |post|
post.reload
post.set_tag_counts
args = Hash[Danbooru.config.full_tag_config_info.keys.map {|x| ["tag_count_#{x}",post.send("tag_count_#{x}")]}].update(:tag_count => post.tag_count)
args = Hash[TagCategory.categories {|x| ["tag_count_#{x}",post.send("tag_count_#{x}")]}].update(:tag_count => post.tag_count)
Post.where(:id => post.id).update_all(args)
end
end
@@ -177,7 +177,7 @@ class Tag < ApplicationRecord
counts = counts.to_a.select {|x| x[1] > trending_count_limit}
counts = counts.map do |tag_name, recent_count|
tag = Tag.find_or_create_by_name(tag_name)
if tag.category == Danbooru.config.tag_category_mapping["artist"]
if tag.category == Tag.categories.artist
# we're not interested in artists in the trending list
[tag_name, 0]
else
@@ -666,8 +666,8 @@ class Tag < ApplicationRecord
when "tagcount"
q[:post_tag_count] = parse_helper(g2)
when /(#{Danbooru.config.short_tag_name_mapping.keys.join("|")})tags/
q["#{Danbooru.config.short_tag_name_mapping[$1]}_tag_count".to_sym] = parse_helper(g2)
when /(#{TagCategory.short_name_regex})tags/
q["#{TagCategory.short_name_mapping[$1]}_tag_count".to_sym] = parse_helper(g2)
when "parent"
q[:parent] = g2.downcase

View File

@@ -132,7 +132,7 @@ class PostPresenter < Presenter
def categorized_tag_groups
string = []
Danbooru.config.categorized_tag_list.each do |category|
TagCategory.categorized_list.each do |category|
if @post.typed_tags(category).any?
string << @post.typed_tags(category).join(" ")
end

View File

@@ -25,10 +25,10 @@ class TagSetPresenter < Presenter
def split_tag_list_html(template, options = {})
html = ""
Danbooru.config.split_tag_header_list.each do |category|
TagCategory.split_header_list.each do |category|
typetags = typed_tags(category)
if typetags.any?
html << Danbooru.config.full_tag_config_info[category]["header"]
html << TagCategory.header_mapping[category]
html << "<ul>"
typetags.each do |tag|
html << build_list_item(tag, template, options)
@@ -56,7 +56,7 @@ private
@typed_tags ||= {}
@typed_tags[name] ||= begin
@tags.select do |tag|
categories[tag] == Danbooru.config.tag_category_mapping[name]
categories[tag] == TagCategory.mapping[name]
end
end
end

View File

@@ -8,8 +8,8 @@
<% unless cookies[:dm] %>
<meta name="viewport" content="width=device-width,initial-scale=1">
<% end %>
<meta name="tag-category-names" content="<%= Danbooru.config.tag_category_mapping.keys %>">
<meta name="short-tag-category-names" content="<%= Danbooru.config.short_tag_name_mapping.keys %>">
<meta name="tag-category-names" content="<%= TagCategory.categories %>">
<meta name="short-tag-category-names" content="<%= TagCategory.short_name_list %>">
<meta name="current-user-name" content="<%= CurrentUser.name %>">
<meta name="current-user-id" content="<%= CurrentUser.id %>">
<meta name="current-user-can-approve-posts" content="<%= CurrentUser.can_approve_posts? %>">

View File

@@ -78,8 +78,8 @@
<%= button_tag "Related tags", :id => "related-tags-button", :type => "button", :class => "ui-button ui-widget ui-corner-all sub gradient" %>
<% Danbooru.config.related_tag_button_list.each do |category| %>
<%= button_tag "#{Danbooru.config.full_tag_config_info[category]["relatedtag"]}", :id => "related-#{category}-button", :type => "button", :class => "ui-button ui-widget ui-corner-all sub gradient" %>
<% TagCategory.related_button_list.each do |category| %>
<%= button_tag "#{TagCategory.related_button_mapping[category]}", :id => "related-#{category}-button", :type => "button", :class => "ui-button ui-widget ui-corner-all sub gradient" %>
<% end %>
</div>

View File

@@ -153,7 +153,7 @@
<% end %>
<% content_for(:html_header) do %>
<meta name="related-tag-button-list" content="<%= Danbooru.config.related_tag_button_list %>">
<meta name="related-tag-button-list" content="<%= TagCategory.related_button_list %>">
<meta name="description" content="<%= @post.presenter.humanized_tag_string %>">
<meta name="tags" content="<%= @post.tag_string %>">
<meta name="favorites" content="<%= @post.fav_string %>">

View File

@@ -3,7 +3,7 @@
<section>
<%= form_tag(related_tag_path, :method => :get) do %>
<%= text_field_tag "query", params[:query], :data => { :autocomplete => "tag" } %>
<%= select_tag "category", options_for_select([""] + Danbooru.config.canonical_tag_category_mapping.map{|x| [x.first, x.first.downcase]}, params[:category]) %>
<%= select_tag "category", options_for_select([""] + TagCategory.canonical_mapping.map{|x| [x.first, x.first.downcase]}, params[:category]) %>
<%= submit_tag "Show"%>
<% end %>
</section>

View File

@@ -3,7 +3,7 @@
<%= simple_form_for(:search, method: :get, url: tag_aliases_path, defaults: { required: false }, html: { class: "inline-form" }) do |f| %>
<%= f.input :name_matches, label: "Name", input_html: { value: params[:search][:name_matches], data: { autocomplete: "tag" } } %>
<%= f.input :status, label: "Status", collection: ["", "Approved", "Pending"], selected: params[:search][:status] %>
<%= f.input :category, label: "Category", collection: Danbooru.config.canonical_tag_category_mapping.to_a, include_blank: true, selected: params[:search][:category] %>
<%= f.input :category, label: "Category", collection: TagCategory.canonical_mapping.to_a, include_blank: true, selected: params[:search][:category] %>
<%= f.input :order, label: "Order", collection: [%w[Status status], %w[Recently\ created created_at], %w[Recently\ updated updated_at], %w[Name name], %w[Tag\ count tag_count]], selected: params[:search][:order] %>
<%= f.submit "Search" %>
<% end %>

View File

@@ -3,7 +3,7 @@
<%= simple_form_for(:search, method: :get, url: tag_implications_path, defaults: { required: false }, html: { class: "inline-form" }) do |f| %>
<%= f.input :name_matches, label: "Name", input_html: { value: params[:search][:name_matches], data: { autocomplete: "tag" } } %>
<%= f.input :status, label: "Status", collection: ["", "Approved", "Pending"], selected: params[:search][:status] %>
<%= f.input :category, label: "Category", collection: Danbooru.config.canonical_tag_category_mapping.to_a, include_blank: true, selected: params[:search][:category] %>
<%= f.input :category, label: "Category", collection: TagCategory.canonical_mapping.to_a, include_blank: true, selected: params[:search][:category] %>
<%= f.input :order, label: "Order", collection: [%w[Status status], %w[Recently\ created created_at], %w[Recently\ updated updated_at], %w[Name name], %w[Tag\ count tag_count]], selected: params[:search][:order] %>
<%= f.submit "Search" %>
<% end %>

View File

@@ -1,6 +1,6 @@
<%= simple_form_for(:search, url: tags_path, method: :get, defaults: { required: false }, html: { class: "inline-form" }) do |f| %>
<%= f.input :name_matches, label: "Name", hint: "Use * for wildcard", input_html: { value: params[:search][:name_matches], data: { autocomplete: "tag" } } %>
<%= f.input :category, label: "Category", collection: Danbooru.config.canonical_tag_category_mapping.to_a, include_blank: true,selected: params[:search][:category] %>
<%= f.input :category, label: "Category", collection: TagCategory.canonical_mapping.to_a, include_blank: true,selected: params[:search][:category] %>
<%= f.input :order, collection: [%w[Newest date], %w[Count count], %w[Name name]], include_blank: false, selected: params[:search][:order] %>
<%= f.input :hide_empty, label: "Hide empty?", collection: %w[yes no], selected: params[:search][:hide_empty] %>
<%= f.input :has_wiki, label: "Has wiki?", collection: %w[yes no], include_blank: true, selected: params[:search][:has_wiki] %>

View File

@@ -6,7 +6,7 @@
<% if @tag.is_locked? %>
<p>This tag is category locked</p>
<% else %>
<%= f.input :category, :collection => Danbooru.config.canonical_tag_category_mapping.to_a, :include_blank => false %>
<%= f.input :category, :collection => TagCategory.canonical_mapping.to_a, :include_blank => false %>
<% end %>
<% if CurrentUser.is_moderator? %>

View File

@@ -110,9 +110,9 @@
<%= button_tag "Related tags", :id => "related-tags-button", :type => "button", :class => "ui-button ui-widget ui-corner-all sub gradient" %>
<% Danbooru.config.related_tag_button_list.each do |category| %>
<%= button_tag "#{Danbooru.config.full_tag_config_info[category]["relatedtag"]}", :id => "related-#{category}-button", :type => "button", :class => "ui-button ui-widget ui-corner-all sub gradient" %>
<% end %>
<% TagCategory.related_button_list.each do |category| %>
<%= button_tag "#{TagCategory.related_button_mapping[category]}", :id => "related-#{category}-button", :type => "button", :class => "ui-button ui-widget ui-corner-all sub gradient" %>
<% end %>
</div>
<div class="input">

View File

@@ -225,7 +225,7 @@ module Danbooru
"extra" => [],
"header" => "<h1>Tags</h1>",
"humanized" => nil,
"relatedtag" => "General"
"relatedbutton" => "General"
},
"character" => {
"category" => 4,
@@ -238,7 +238,7 @@ module Danbooru
"regexmap" => /^(.+?)(?:_\(.+\))?$/,
"formatstr" => "%s"
},
"relatedtag" => "Characters"
"relatedbutton" => "Characters"
},
"copyright" => {
"category" => 3,
@@ -251,7 +251,7 @@ module Danbooru
"regexmap" => //,
"formatstr" => "(%s)"
},
"relatedtag" => "Copyrights"
"relatedbutton" => "Copyrights"
},
"artist" => {
"category" => 1,
@@ -264,7 +264,7 @@ module Danbooru
"regexmap" => //,
"formatstr" => "drawn by %s"
},
"relatedtag" => "Artists"
"relatedbutton" => "Artists"
},
"meta" => {
"category" => 5,
@@ -272,37 +272,11 @@ module Danbooru
"extra" => [],
"header" => "<h2>Meta</h2>",
"humanized" => nil,
"relatedtag" => nil
"relatedbutton" => nil
}
}
end
#TAG MAPPINGS
# Returns a hash mapping various tag categories to a numerical value.
def tag_category_mapping
@tag_category_mapping ||= Hash[
full_tag_config_info.map {|k,v| v["extra"].map {|y| [y,v["category"]]}}
.reduce([],:+)]
.update(Hash[full_tag_config_info.map {|k,v| [v["short"],v["category"]]}])
.update( Hash[full_tag_config_info.map {|k,v| [k,v["category"]]}])
end
# Returns a hash mapping more suited for views
def canonical_tag_category_mapping
@canonical_tag_category_mapping ||= Hash[full_tag_config_info.map {|k,v| [k.capitalize,v["category"]]}]
end
# Returns a hash mapping numerical category values to their string equivalent.
def reverse_tag_category_mapping
@reverse_tag_category_mapping ||= Hash[full_tag_config_info.map {|k,v| [v["category"],k]}]
end
# Returns a hash mapping for the short name usage in metatags
def short_tag_name_mapping
@short_tag_name_mapping ||= Hash[full_tag_config_info.map {|k,v| [v["short"],k] }]
end
#TAG ORDERS
#Sets the order of the humanized essential tag string (models/post.rb)

View File

@@ -218,7 +218,7 @@ class TagTest < ActiveSupport::TestCase
should_not allow_value("café").for(:name).on(:create)
should_not allow_value("東方").for(:name).on(:create)
metatags = Tag::METATAGS.split("|") + Tag::SUBQUERY_METATAGS.split("|") + Danbooru.config.tag_category_mapping.keys
metatags = Tag::METATAGS.split("|") + Tag::SUBQUERY_METATAGS.split("|") + TagCategory.mapping.keys
metatags.split("|").each do |metatag|
should_not allow_value("#{metatag}:foo").for(:name).on(:create)
end