rework how category metatags work #3307
This commit is contained in:
@@ -683,14 +683,14 @@ class Post < ApplicationRecord
|
|||||||
|
|
||||||
def normalize_tags
|
def normalize_tags
|
||||||
normalized_tags = Tag.scan_tags(tag_string)
|
normalized_tags = Tag.scan_tags(tag_string)
|
||||||
normalized_tags = filter_metatags(normalized_tags)
|
|
||||||
normalized_tags = normalized_tags.map {|tag| tag.downcase}
|
normalized_tags = normalized_tags.map {|tag| tag.downcase}
|
||||||
|
normalized_tags = filter_metatags(normalized_tags)
|
||||||
normalized_tags = remove_negated_tags(normalized_tags)
|
normalized_tags = remove_negated_tags(normalized_tags)
|
||||||
normalized_tags = %w(tagme) if normalized_tags.empty?
|
|
||||||
normalized_tags = TagAlias.to_aliased(normalized_tags)
|
normalized_tags = TagAlias.to_aliased(normalized_tags)
|
||||||
|
normalized_tags = %w(tagme) if normalized_tags.empty?
|
||||||
|
normalized_tags = TagImplication.with_descendants(normalized_tags)
|
||||||
normalized_tags = Tag.create_for_list(add_automatic_tags(normalized_tags))
|
normalized_tags = Tag.create_for_list(add_automatic_tags(normalized_tags))
|
||||||
normalized_tags = normalized_tags + Tag.create_for_list(TagImplication.automatic_tags_for(normalized_tags))
|
normalized_tags = normalized_tags + Tag.create_for_list(TagImplication.automatic_tags_for(normalized_tags))
|
||||||
normalized_tags = TagImplication.with_descendants(normalized_tags)
|
|
||||||
normalized_tags = normalized_tags.compact
|
normalized_tags = normalized_tags.compact
|
||||||
normalized_tags.sort!
|
normalized_tags.sort!
|
||||||
set_tag_string(normalized_tags.uniq.sort.join(" "))
|
set_tag_string(normalized_tags.uniq.sort.join(" "))
|
||||||
@@ -765,11 +765,23 @@ class Post < ApplicationRecord
|
|||||||
|
|
||||||
def filter_metatags(tags)
|
def filter_metatags(tags)
|
||||||
@pre_metatags, tags = tags.partition {|x| x =~ /\A(?:rating|parent|-parent|source|-?locked):/i}
|
@pre_metatags, tags = tags.partition {|x| x =~ /\A(?:rating|parent|-parent|source|-?locked):/i}
|
||||||
|
tags = apply_categorization_metatags(tags)
|
||||||
@post_metatags, tags = tags.partition {|x| x =~ /\A(?:-pool|pool|newpool|fav|-fav|child|-favgroup|favgroup|upvote|downvote):/i}
|
@post_metatags, tags = tags.partition {|x| x =~ /\A(?:-pool|pool|newpool|fav|-fav|child|-favgroup|favgroup|upvote|downvote):/i}
|
||||||
apply_pre_metatags
|
apply_pre_metatags
|
||||||
return tags
|
return tags
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def apply_categorization_metatags(tags)
|
||||||
|
tags.map do |x|
|
||||||
|
if x =~ Tag.categories.regexp
|
||||||
|
tag = Tag.find_or_create_by_name(x)
|
||||||
|
tag.name
|
||||||
|
else
|
||||||
|
x
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def apply_post_metatags
|
def apply_post_metatags
|
||||||
return unless @post_metatags
|
return unless @post_metatags
|
||||||
|
|
||||||
@@ -827,6 +839,7 @@ class Post < ApplicationRecord
|
|||||||
when /^favgroup:(.+)$/i
|
when /^favgroup:(.+)$/i
|
||||||
favgroup = FavoriteGroup.named($1).for_creator(CurrentUser.user.id).first
|
favgroup = FavoriteGroup.named($1).for_creator(CurrentUser.user.id).first
|
||||||
favgroup.add!(id) if favgroup
|
favgroup.add!(id) if favgroup
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -870,6 +883,7 @@ class Post < ApplicationRecord
|
|||||||
|
|
||||||
when /^(-?)locked:status$/i
|
when /^(-?)locked:status$/i
|
||||||
assign_attributes({ is_status_locked: $1 != "-" }, as: CurrentUser.role)
|
assign_attributes({ is_status_locked: $1 != "-" }, as: CurrentUser.role)
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -643,8 +643,8 @@ class PostTest < ActiveSupport::TestCase
|
|||||||
assert_equal(Tag.categories.copyright, Tag.find_by_name("abc").category)
|
assert_equal(Tag.categories.copyright, Tag.find_by_name("abc").category)
|
||||||
end
|
end
|
||||||
|
|
||||||
should "update the category cache of the tag" do
|
should "1234 update the category cache of the tag" do
|
||||||
assert_equal(Tag.categories.copyright, Cache.get("tc:abc"))
|
assert_equal(Tag.categories.copyright, Cache.get("tc:#{Cache.hash('abc')}"))
|
||||||
end
|
end
|
||||||
|
|
||||||
should "update the tag counts of the posts" do
|
should "update the tag counts of the posts" do
|
||||||
@@ -734,21 +734,29 @@ class PostTest < ActiveSupport::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
context "tagged with a metatag" do
|
context "tagged with a metatag" do
|
||||||
context "for an alias cosplay tag" do
|
|
||||||
|
context "for typing a tag" do
|
||||||
setup do
|
setup do
|
||||||
@alias = FactoryGirl.create(:tag_alias, antecedent_name: "hoge", consequent_name: "moge")
|
@post = FactoryGirl.create(:post, tag_string: "char:hoge")
|
||||||
@post = FactoryGirl.create(:post, tag_string: "char:hoge_(cosplay)")
|
|
||||||
@tags = @post.tag_array
|
@tags = @post.tag_array
|
||||||
end
|
end
|
||||||
|
|
||||||
should "add the cosplay tag" do
|
should "change the type" do
|
||||||
assert(@tags.include?("cosplay"))
|
assert(Tag.where(name: "hoge", category: 4).exists?, "expected 'moge' tag to be created as a character")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "for typing an aliased tag" do
|
||||||
|
setup do
|
||||||
|
@alias = FactoryGirl.create(:tag_alias, antecedent_name: "hoge", consequent_name: "moge")
|
||||||
|
@post = FactoryGirl.create(:post, tag_string: "char:hoge")
|
||||||
|
@tags = @post.tag_array
|
||||||
end
|
end
|
||||||
|
|
||||||
should "create the tag" do
|
should "change the type" do
|
||||||
assert(Tag.where(name: "someone_(cosplay)").exists?, "expected 'someone_(cosplay)' tag to be created")
|
assert_equal(["moge"], @tags)
|
||||||
assert(Tag.where(name: "someone_(cosplay)", category: 4).exists?, "expected 'someone_(cosplay)' tag to be created as character")
|
assert(Tag.where(name: "moge", category: 0).exists?, "expected 'moge' tag to be created as a character")
|
||||||
assert(Tag.where(name: "someone", category: 4).exists?, "expected 'someone' tag to be created")
|
assert(Tag.where(name: "hoge", category: 4).exists?, "expected 'moge' tag to be created as a character")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user