Skip to content

Commit

Permalink
FEATURE: respect tags_sort_alphabetically setting when display tags (#…
Browse files Browse the repository at this point in the history
…10889)

Currently, tag labels are displayed in random order.

They should be displayed in alphabetical or popularity order based on SiteSetting (tags_sort_alphabetically)

Meta: https://meta.discourse.org/t/how-to-apply-tag-sorts-by-popularity-to-topic-list-currently-it-seems-only-apply-to-tag-page/163186/7
  • Loading branch information
lis2 committed Oct 12, 2020
1 parent acf5a26 commit 6be60b0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion app/serializers/concerns/topic_tags_mixin.rb
Expand Up @@ -11,7 +11,8 @@ def include_tags?

def tags
# Calling method `pluck` along with `includes` causing N+1 queries
tags = topic.tags.map(&:name)
order_setting = SiteSetting.tags_sort_alphabetically ? { name: :asc } : { topic_count: :desc }
tags = topic.tags.order(order_setting).map(&:name)

if scope.is_staff?
tags
Expand Down
24 changes: 24 additions & 0 deletions spec/serializers/topic_view_serializer_spec.rb
Expand Up @@ -212,6 +212,30 @@ def serialize_topic(topic, user_arg)
end
end

describe 'tags order' do
fab!(:tag1) { Fabricate(:tag, name: 'ctag', topic_count: 5) }
fab!(:tag2) { Fabricate(:tag, name: 'btag', topic_count: 9) }
fab!(:tag3) { Fabricate(:tag, name: 'atag', topic_count: 3) }

before do
SiteSetting.tagging_enabled = true
topic.tags << tag1
topic.tags << tag2
topic.tags << tag3
end

it 'tags are automatically sorted by tag popularity' do
json = serialize_topic(topic, user)
expect(json[:tags]).to eq(%w(btag ctag atag))
end

it 'tags can be sorted alphabetically' do
SiteSetting.tags_sort_alphabetically = true
json = serialize_topic(topic, user)
expect(json[:tags]).to eq(%w(atag btag ctag))
end
end

context "with flags" do
fab!(:post) { Fabricate(:post, topic: topic) }
fab!(:other_post) { Fabricate(:post, topic: topic) }
Expand Down

0 comments on commit 6be60b0

Please sign in to comment.