From bdf5912dc9c18bffb4f213822e9677d743d6e820 Mon Sep 17 00:00:00 2001 From: BrokenEagle Date: Sun, 5 Nov 2017 23:03:36 -0800 Subject: [PATCH] Reduce tag config variables co-dependency --- app/models/tag.rb | 4 +- config/danbooru_default_config.rb | 65 +++++++++++++++++-------------- 2 files changed, 37 insertions(+), 32 deletions(-) diff --git a/app/models/tag.rb b/app/models/tag.rb index 4cbd5d87e..55282a8e3 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -27,7 +27,7 @@ class Tag < ApplicationRecord class CategoryMapping Danbooru.config.reverse_tag_category_mapping.each do |value, category| - define_method(category.downcase) do + define_method(category) do value end end @@ -124,7 +124,7 @@ class Tag < ApplicationRecord end def category_name - Danbooru.config.reverse_tag_category_mapping[category] + Danbooru.config.reverse_tag_category_mapping[category].capitalize end def update_category_cache_for_all diff --git a/config/danbooru_default_config.rb b/config/danbooru_default_config.rb index 3b2e2710e..be742d0ae 100644 --- a/config/danbooru_default_config.rb +++ b/config/danbooru_default_config.rb @@ -214,44 +214,49 @@ module Danbooru "albert" end + #Full tag configuration info for all tags + def full_tag_config_info + @full_tag_category_mapping ||= { + "general" => { + "category" => 0, + "short" => "gen", + "extra" => [] + }, + "character" => { + "category" => 4, + "short" => "char", + "extra" => ["ch"] + }, + "copyright" => { + "category" => 3, + "short" => "copy", + "extra" => ["co"] + }, + "artist" => { + "category" => 1, + "short" => "art", + "extra" => [] + } + } + end + # Returns a hash mapping various tag categories to a numerical value. - # Be sure to update the reverse_tag_category_mapping also. def tag_category_mapping - @tag_category_mapping ||= { - "general" => 0, - "gen" => 0, - - "artist" => 1, - "art" => 1, - - "copyright" => 3, - "copy" => 3, - "co" => 3, - - "character" => 4, - "char" => 4, - "ch" => 4 - } + @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 ||= { - "General" => 0, - "Artist" => 1, - "Copyright" => 3, - "Character" => 4 - } + @canonical_tag_category_mapping ||= Hash[full_tag_config_info.map {|k,v| [k.capitalize,v["category"]]}] end - # Returns a hash maping numerical category values to their - # string equivalent. Be sure to update the tag_category_mapping also. + # Returns a hash mapping numerical category values to their string equivalent. def reverse_tag_category_mapping - @reverse_tag_category_mapping ||= { - 0 => "General", - 1 => "Artist", - 3 => "Copyright", - 4 => "Character" - } + @reverse_tag_category_mapping ||= Hash[full_tag_config_info.map {|k,v| [v["category"],k]}] end # If enabled, users must verify their email addresses.