diff --git a/app/models/post.rb b/app/models/post.rb index 09b0cabe9..becc364b2 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -683,14 +683,14 @@ class Post < ApplicationRecord def normalize_tags normalized_tags = Tag.scan_tags(tag_string) - normalized_tags = filter_metatags(normalized_tags) normalized_tags = normalized_tags.map {|tag| tag.downcase} + normalized_tags = filter_metatags(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 = %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 = 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.sort! set_tag_string(normalized_tags.uniq.sort.join(" ")) @@ -765,11 +765,23 @@ class Post < ApplicationRecord def filter_metatags(tags) @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} apply_pre_metatags return tags 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 return unless @post_metatags @@ -827,6 +839,7 @@ class Post < ApplicationRecord when /^favgroup:(.+)$/i favgroup = FavoriteGroup.named($1).for_creator(CurrentUser.user.id).first favgroup.add!(id) if favgroup + end end end @@ -870,6 +883,7 @@ class Post < ApplicationRecord when /^(-?)locked:status$/i assign_attributes({ is_status_locked: $1 != "-" }, as: CurrentUser.role) + end end end diff --git a/test/unit/post_test.rb b/test/unit/post_test.rb index f3581a776..1b69fc95c 100644 --- a/test/unit/post_test.rb +++ b/test/unit/post_test.rb @@ -643,8 +643,8 @@ class PostTest < ActiveSupport::TestCase assert_equal(Tag.categories.copyright, Tag.find_by_name("abc").category) end - should "update the category cache of the tag" do - assert_equal(Tag.categories.copyright, Cache.get("tc:abc")) + should "1234 update the category cache of the tag" do + assert_equal(Tag.categories.copyright, Cache.get("tc:#{Cache.hash('abc')}")) end should "update the tag counts of the posts" do @@ -734,21 +734,29 @@ class PostTest < ActiveSupport::TestCase end context "tagged with a metatag" do - context "for an alias cosplay tag" do + + context "for typing a tag" do setup do - @alias = FactoryGirl.create(:tag_alias, antecedent_name: "hoge", consequent_name: "moge") - @post = FactoryGirl.create(:post, tag_string: "char:hoge_(cosplay)") + @post = FactoryGirl.create(:post, tag_string: "char:hoge") @tags = @post.tag_array end - should "add the cosplay tag" do - assert(@tags.include?("cosplay")) + should "change the type" do + 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 - should "create the tag" do - assert(Tag.where(name: "someone_(cosplay)").exists?, "expected 'someone_(cosplay)' tag to be created") - assert(Tag.where(name: "someone_(cosplay)", category: 4).exists?, "expected 'someone_(cosplay)' tag to be created as character") - assert(Tag.where(name: "someone", category: 4).exists?, "expected 'someone' tag to be created") + should "change the type" do + assert_equal(["moge"], @tags) + assert(Tag.where(name: "moge", category: 0).exists?, "expected 'moge' tag to be created as a character") + assert(Tag.where(name: "hoge", category: 4).exists?, "expected 'moge' tag to be created as a character") end end