FIX: suggested topics sometimes adds new topics from other categories before new topics of the current category

This commit is contained in:
Neil Lalonde 2014-01-13 15:02:08 -05:00
parent 819ac61eb9
commit e7a05c54e8
2 changed files with 16 additions and 6 deletions

View File

@ -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

View File

@ -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