diff --git a/lib/topic_query.rb b/lib/topic_query.rb index eebb5a028..6f932f172 100644 --- a/lib/topic_query.rb +++ b/lib/topic_query.rb @@ -132,7 +132,11 @@ class TopicQuery end def list_category_topic_ids(category) - default_results(unordered: true, category: category.id).pluck(:id) + query = default_results(category: category.id) + pinned_ids = query.where('pinned_at IS NOT NULL').order('pinned_at DESC').pluck(:id) + non_pinned_ids = query.where('pinned_at IS NULL').pluck(:id) + + (pinned_ids + non_pinned_ids)[0...@options[:per_page]] end def list_new_in_category(category) diff --git a/spec/models/category_featured_topic_spec.rb b/spec/models/category_featured_topic_spec.rb index 436b7e611..4859ea7cd 100644 --- a/spec/models/category_featured_topic_spec.rb +++ b/spec/models/category_featured_topic_spec.rb @@ -31,6 +31,23 @@ describe CategoryFeaturedTopic do CategoryFeaturedTopic.feature_topics_for(category) expect(CategoryFeaturedTopic.count).to be(1) end + + + it 'should feature stuff in the correct order' do + + category = Fabricate(:category) + _t3 = Fabricate(:topic, category_id: category.id, bumped_at: 7.minutes.ago) + t2 = Fabricate(:topic, category_id: category.id, bumped_at: 4.minutes.ago) + t1 = Fabricate(:topic, category_id: category.id, bumped_at: 5.minutes.ago) + pinned = Fabricate(:topic, category_id: category.id, pinned_at: 10.minutes.ago, bumped_at: 10.minutes.ago) + + CategoryFeaturedTopic.feature_topics_for(category) + + expect( + CategoryFeaturedTopic.where(category_id: category.id).pluck(:topic_id) + ).to eq([pinned.id, t2.id, t1.id]) + + end end end