FIX: Category latest pages were not preloading properly, causing weird

refreshes when clicking the home logo.
This commit is contained in:
Robin Ward 2014-10-08 12:44:47 -04:00
parent 8a88e71b3c
commit 1f26a79899
5 changed files with 30 additions and 11 deletions

View File

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

View File

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

View File

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

View File

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

View File

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