Skip to content

A platform for community discussion. Free, open, simple.

Merge branch 'master' into pm-tags

Vinoth Kannan2b509eaa9192
diff --cc app/models/topic_tag.rb
index de53e4cf8ad,067e1f79bf1..8c0a8df5012
--- a/app/models/topic_tag.rb
+++ b/app/models/topic_tag.rb
@@@ -1,28 -1,22 +1,28 @@@
class TopicTag < ActiveRecord::Base
belongs_to :topic
- belongs_to :tag, counter_cache: "topic_count"
+ belongs_to :tag
after_create do
- if topic.archetype != Archetype.private_message
- if topic&.category_id
- if stat = CategoryTagStat.where(tag_id: tag_id, category_id: topic.category_id).first
- stat.increment!(:topic_count)
- else
- CategoryTagStat.create(tag_id: tag_id, category_id: topic.category_id, topic_count: 1)
++ if topic && topic.archetype != Archetype.private_message
+ tag.increment!(:topic_count)
+
+ if topic.category_id
+ if stat = CategoryTagStat.where(tag_id: tag_id, category_id: topic.category_id).first
+ stat.increment!(:topic_count)
+ else
+ CategoryTagStat.create(tag_id: tag_id, category_id: topic.category_id, topic_count: 1)
+ end
end
end
end
after_destroy do
- if topic.archetype != Archetype.private_message
- if topic&.category_id
- if stat = CategoryTagStat.where(tag_id: tag_id, category: topic.category_id).first
++ if topic && topic.archetype != Archetype.private_message
+ if topic.category_id && stat = CategoryTagStat.where(tag_id: tag_id, category: topic.category_id).first
stat.topic_count == 1 ? stat.destroy : stat.decrement!(:topic_count)
end
+
+ tag.decrement!(:topic_count)
end
end
end
diff --cc spec/models/tag_spec.rb
index faa6145a80c,06287133a5d..bdccd26fcbd
--- a/spec/models/tag_spec.rb
+++ b/spec/models/tag_spec.rb
@@@ -15,6 -15,58 +15,13 @@@ describe Tag d
SiteSetting.min_trust_level_to_tag_topics = 0
end
+ it "can delete tags on deleted topics" do
+ tag = Fabricate(:tag)
+ topic = Fabricate(:topic, tags: [tag])
+ topic.trash!
+ expect { tag.destroy }.to change { Tag.count }.by(-1)
+ end
+
- describe '#tags_by_count_query' do
- it "returns empty hash if nothing is tagged" do
- expect(described_class.tags_by_count_query.count(Tag::COUNT_ARG)).to eq({})
- end
-
- context "with some tagged topics" do
- before do
- @topics = []
- 3.times { @topics << Fabricate(:topic) }
- make_some_tags(count: 2)
- @topics[0].tags << @tags[0]
- @topics[0].tags << @tags[1]
- @topics[1].tags << @tags[0]
- end
-
- it "returns tag names with topic counts in a hash" do
- counts = described_class.tags_by_count_query.count(Tag::COUNT_ARG)
- expect(counts[@tags[0].name]).to eq(2)
- expect(counts[@tags[1].name]).to eq(1)
- end
-
- it "can be used to filter before doing the count" do
- counts = described_class.tags_by_count_query.where("topics.id = ?", @topics[1].id).count(Tag::COUNT_ARG)
- expect(counts).to eq(@tags[0].name => 1)
- end
-
- it "returns unused tags too" do
- unused = Fabricate(:tag)
- counts = described_class.tags_by_count_query.count(Tag::COUNT_ARG)
- expect(counts[unused.name]).to eq(0)
- end
-
- it "doesn't include deleted topics in counts" do
- deleted_topic_tag = Fabricate(:tag)
- delete_topic = Fabricate(:topic)
- post = Fabricate(:post, topic: delete_topic, user: delete_topic.user)
- delete_topic.tags << deleted_topic_tag
- PostDestroyer.new(Fabricate(:admin), post).destroy
-
- counts = described_class.tags_by_count_query.count(Tag::COUNT_ARG)
- expect(counts[deleted_topic_tag.name]).to eq(0)
- end
- end
- end
-
describe '#top_tags' do
it "returns nothing if nothing has been tagged" do
make_some_tags(tag_a_topic: false)