From ddc590e7e8387c0ae86fba1cce0d16372bf38a9d Mon Sep 17 00:00:00 2001 From: Toks Date: Wed, 27 Nov 2013 00:03:06 -0500 Subject: [PATCH] fixes #2009 --- app/models/post.rb | 7 +++++++ test/unit/post_test.rb | 9 +++++++++ 2 files changed, 16 insertions(+) diff --git a/app/models/post.rb b/app/models/post.rb index aa12b0382..39a700cc9 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -416,6 +416,7 @@ class Post < ActiveRecord::Base normalized_tags = Tag.scan_tags(tag_string) normalized_tags = filter_metatags(normalized_tags) normalized_tags = normalized_tags.map{|tag| tag.downcase} + normalized_tags = remove_negated_tags(normalized_tags) normalized_tags = normalized_tags.map {|x| Tag.find_or_create_by_name(x).name} normalized_tags = TagAlias.to_aliased(normalized_tags) normalized_tags = TagImplication.with_descendants(normalized_tags) @@ -424,6 +425,12 @@ class Post < ActiveRecord::Base set_tag_string(normalized_tags.uniq.sort.join(" ")) end + def remove_negated_tags(tags) + negated_tags, tags = tags.partition {|x| x =~ /\A-/i} + negated_tags.map!{|x| x[1..-1]} + return tags - negated_tags + end + def filter_metatags(tags) @pre_metatags, tags = tags.partition {|x| x =~ /\A(?:rating|parent):/i} @post_metatags, tags = tags.partition {|x| x =~ /\A(?:-pool|pool|newpool|fav):/i} diff --git a/test/unit/post_test.rb b/test/unit/post_test.rb index 3dad74be9..82632e836 100644 --- a/test/unit/post_test.rb +++ b/test/unit/post_test.rb @@ -578,6 +578,15 @@ class PostTest < ActiveSupport::TestCase end end + context "tagged with a negated tag" do + should "remove the tag if present" do + @post.update_attributes(:tag_string => "aaa bbb ccc") + @post.update_attributes(:tag_string => "aaa bbb ccc -bbb") + @post.reload + assert_equal("aaa ccc", @post.tag_string) + end + end + should "have an array representation of its tags" do post = FactoryGirl.create(:post) post.set_tag_string("aaa bbb")