From e7a05c54e85695ecc64084916141612acd64f80e Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Mon, 13 Jan 2014 15:02:08 -0500 Subject: [PATCH] FIX: suggested topics sometimes adds new topics from other categories before new topics of the current category --- lib/suggested_topics_builder.rb | 17 +++++++++++------ .../components/suggested_topics_builder_spec.rb | 5 +++++ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/lib/suggested_topics_builder.rb b/lib/suggested_topics_builder.rb index 509a00d35be..9aec8a1daa6 100644 --- a/lib/suggested_topics_builder.rb +++ b/lib/suggested_topics_builder.rb @@ -31,14 +31,19 @@ class SuggestedTopicsBuilder end def splice_results(results, priority) - if @category_id && - priority == :high && - non_category_index = @results.index{|r| r.category_id != @category_id} + if @category_id && priority == :high - category_results, non_category_results = results.partition{|r| r.category_id == @category_id} + # Topics from category @category_id need to be first in the list, all others after. - @results.insert non_category_index, *category_results - @results.concat non_category_results + other_category_index = @results.index { |r| r.category_id != @category_id } + category_results, other_category_results = results.partition{ |r| r.category_id == @category_id } + + if other_category_index + @results.insert other_category_index, *category_results + else + @results.concat category_results + end + @results.concat other_category_results else @results.concat results end diff --git a/spec/components/suggested_topics_builder_spec.rb b/spec/components/suggested_topics_builder_spec.rb index 0ecb50e7476..dc2f17bfa9c 100644 --- a/spec/components/suggested_topics_builder_spec.rb +++ b/spec/components/suggested_topics_builder_spec.rb @@ -37,6 +37,11 @@ describe SuggestedTopicsBuilder do builder.results.map(&:id).should == [2,3] end + + it "inserts multiple results and puts topics in the correct order" do + builder.splice_results([fake_topic(2,1), fake_topic(3,2), fake_topic(4,1)], :high) + builder.results.map(&:id).should == [2,4,3] + end end it "has the correct defaults" do