diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 7ff472bf4..4b5c5f74b 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -278,9 +278,8 @@ class ApplicationController < ActionController::Base end def render_not_found_page(status=404) - f = Topic.where(deleted_at: nil, archetype: "regular") - @latest = f.order('views desc').take(10) - @recent = f.order('created_at desc').take(10) + @top_viewed = TopicQuery.top_viewed(10) + @recent = TopicQuery.recent(10) @slug = params[:slug].class == String ? params[:slug] : '' @slug = (params[:id].class == String ? params[:id] : '') if @slug.blank? @slug.gsub!('-',' ') diff --git a/app/models/topic.rb b/app/models/topic.rb index 08161471f..f302e3079 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -96,7 +96,7 @@ class Topic < ActiveRecord::Base scope :created_since, lambda { |time_ago| where('created_at > ?', time_ago) } - scope :secured, lambda {|guardian| + scope :secured, lambda {|guardian=nil| ids = guardian.secure_category_ids if guardian # Query conditions diff --git a/app/views/exceptions/not_found.html.erb b/app/views/exceptions/not_found.html.erb index 5bf26a88b..f5852f326 100644 --- a/app/views/exceptions/not_found.html.erb +++ b/app/views/exceptions/not_found.html.erb @@ -4,8 +4,8 @@
-

<%= t 'page_not_found.latest_topics' %>

- <% @latest.each do |t| %> +

<%= t 'page_not_found.popular_topics' %>

+ <% @top_viewed.each do |t| %> <%= link_to t.title, t.relative_url %>
<% end %>
@@ -17,7 +17,7 @@ <%= link_to t.title, t.relative_url %>
<% end %>
- <%= t 'page_not_found.see_more' %>… + <%= t 'page_not_found.see_more' %>…
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index d8341bdf2..282533255 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -1007,7 +1007,7 @@ en: page_not_found: title: "The page you requested doesn't exist on this discussion forum. Perhaps we can help find it, or another topic like it:" - latest_topics: "Latest topics" + popular_topics: "Popular topics" recent_topics: "Recent topics" see_more: "See More" search_title: "Search for this topic" diff --git a/lib/topic_query.rb b/lib/topic_query.rb index c5e6664c1..7696a21e5 100644 --- a/lib/topic_query.rb +++ b/lib/topic_query.rb @@ -63,6 +63,14 @@ class TopicQuery "CASE WHEN (topics.pinned_at IS NOT NULL) THEN 0 ELSE 1 END, topics.bumped_at DESC" end + def top_viewed(max) + Topic.listable_topics.visible.secured.order('views desc').take(10) + end + + def recent(max) + Topic.listable_topics.visible.secured.order('created_at desc').take(10) + end + end def initialize(user=nil, opts={}) diff --git a/spec/components/topic_query_spec.rb b/spec/components/topic_query_spec.rb index f190ca105..9433e4d74 100644 --- a/spec/components/topic_query_spec.rb +++ b/spec/components/topic_query_spec.rb @@ -20,17 +20,22 @@ describe TopicQuery do category.save topic = Fabricate(:topic, category: category) + topic = Fabricate(:topic, visible: false) TopicQuery.new(nil).list_latest.topics.count.should == 0 TopicQuery.new(user).list_latest.topics.count.should == 0 - # mods can see every group - TopicQuery.new(moderator).list_latest.topics.count.should == 2 + TopicQuery.top_viewed(10).count.should == 0 + TopicQuery.recent(10).count.should == 0 + + # mods can see every group and hidden topics + TopicQuery.new(moderator).list_latest.topics.count.should == 3 group.add(user) group.save TopicQuery.new(user).list_latest.topics.count.should == 2 + end end