diff --git a/app/controllers/list_controller.rb b/app/controllers/list_controller.rb index 0f969194d..ce73e8842 100644 --- a/app/controllers/list_controller.rb +++ b/app/controllers/list_controller.rb @@ -186,7 +186,7 @@ class ListController < ApplicationController respond_to do |format| format.html do @list = list - store_preloaded("topic_list_#{list.filter}", MultiJson.dump(TopicListSerializer.new(list, scope: guardian))) + store_preloaded(list.preload_key, MultiJson.dump(TopicListSerializer.new(list, scope: guardian))) render 'list' end format.json do diff --git a/app/models/topic_list.rb b/app/models/topic_list.rb index b8ffab9f1..c5ba27a52 100644 --- a/app/models/topic_list.rb +++ b/app/models/topic_list.rb @@ -11,10 +11,20 @@ class TopicList :filter, :for_period - def initialize(filter, current_user, topics) + def initialize(filter, current_user, topics, opts=nil) @filter = filter @current_user = current_user @topics_input = topics + @opts = opts || {} + end + + def preload_key + if @opts[:category] + c = Category.find(@opts[:category_id]) + "topic_list_#{c.url.sub(/^\//, '')}/l/#{@filter}" + else + "topic_list_#{@filter}" + end end # Lazy initialization diff --git a/lib/topic_query.rb b/lib/topic_query.rb index 7066ab108..b872daf3e 100644 --- a/lib/topic_query.rb +++ b/lib/topic_query.rb @@ -65,7 +65,7 @@ class TopicQuery # The latest view of topics def list_latest - TopicList.new(:latest, @user, latest_results) + create_list(:latest, {}, latest_results) end # The starred topics @@ -80,11 +80,11 @@ class TopicQuery end def list_new - TopicList.new(:new, @user, new_results) + create_list(:new, {}, new_results) end def list_unread - TopicList.new(:new, @user, unread_results) + create_list(:new, {}, unread_results) end def list_posted @@ -111,19 +111,19 @@ class TopicQuery def list_private_messages(user) list = private_messages_for(user) - TopicList.new(:private_messages, user, list) + create_list(:private_messages, {}, list) end def list_private_messages_sent(user) list = private_messages_for(user) list = list.where(user_id: user.id) - TopicList.new(:private_messages, user, list) + create_list(:private_messages, {}, list) end def list_private_messages_unread(user) list = private_messages_for(user) list = list.where("tu.last_read_post_number IS NULL OR tu.last_read_post_number < topics.highest_post_number") - TopicList.new(:private_messages, user, list) + create_list(:private_messages, {}, list) end def list_category(category) @@ -158,7 +158,7 @@ class TopicQuery def create_list(filter, options={}, topics = nil) topics ||= default_results(options) topics = yield(topics) if block_given? - TopicList.new(filter, @user, topics) + TopicList.new(filter, @user, topics, options.merge(@options)) end def private_messages_for(user) @@ -241,6 +241,7 @@ class TopicQuery end category_id = get_category_id(options[:category]) + @options[:category_id] = category_id if category_id if options[:no_subcategories] result = result.where('categories.id = ?', category_id) diff --git a/spec/components/topic_query_spec.rb b/spec/components/topic_query_spec.rb index 0f0923a2f..1774a8903 100644 --- a/spec/components/topic_query_spec.rb +++ b/spec/components/topic_query_spec.rb @@ -43,7 +43,7 @@ describe TopicQuery do context 'category filter' do let(:category) { Fabricate(:category) } - let(:diff_category) { Fabricate(:category) } + let(:diff_category) { Fabricate(:diff_category) } it "returns topics in the category when we filter to it" do TopicQuery.new(moderator).list_latest.topics.size.should == 0 @@ -51,7 +51,10 @@ describe TopicQuery do # Filter by slug TopicQuery.new(moderator, category: category.slug).list_latest.topics.size.should == 1 TopicQuery.new(moderator, category: "#{category.id}-category").list_latest.topics.size.should == 1 - TopicQuery.new(moderator, category: diff_category.slug).list_latest.topics.size.should == 1 + + list = TopicQuery.new(moderator, category: diff_category.slug).list_latest + list.topics.size.should == 1 + list.preload_key.should == "topic_list_category/different-category/l/latest" # Defaults to no category filter when slug does not exist TopicQuery.new(moderator, category: 'made up slug').list_latest.topics.size.should == 2 diff --git a/spec/fabricators/category_fabricator.rb b/spec/fabricators/category_fabricator.rb index 6ef5a89a9..dae133774 100644 --- a/spec/fabricators/category_fabricator.rb +++ b/spec/fabricators/category_fabricator.rb @@ -2,3 +2,8 @@ Fabricator(:category) do name { sequence(:name) { |n| "Amazing Category #{n}" } } user end + +Fabricator(:diff_category, from: :category) do + name "Different Category" + user +end