basic implementation of per-category new/latest etc filters

This commit is contained in:
Sam 2013-10-24 21:05:06 +11:00
parent 89f801ac04
commit b7d3b52e4f
5 changed files with 18 additions and 5 deletions

View File

@ -26,6 +26,9 @@ Discourse.Route.buildRoutes(function() {
Discourse.ListController.filters.forEach(function(filter) { Discourse.ListController.filters.forEach(function(filter) {
router.route(filter, { path: "/" + filter }); router.route(filter, { path: "/" + filter });
router.route(filter, { path: "/" + filter + "/more" }); router.route(filter, { path: "/" + filter + "/more" });
router.route(filter + "Category", { path: "/category/:slug/" + filter });
router.route(filter + "Category", { path: "/category/:slug/" + filter + "/more" });
}); });
// the homepage is the first item of the 'top_menu' site setting // the homepage is the first item of the 'top_menu' site setting

View File

@ -40,7 +40,7 @@ Discourse.ListCategoryRoute = Discourse.FilteredListRoute.extend({
this._super(); this._super();
// Add a search context // Add a search context
this.controllerFor('search').set('searchContext', this.modelFor('listCategory').get('searchContext')); this.controllerFor('search').set('searchContext', this.modelFor(this.get('routeName')).get('searchContext'));
}, },
deactivate: function() { deactivate: function() {
@ -54,3 +54,8 @@ Discourse.ListCategoryRoute = Discourse.FilteredListRoute.extend({
}); });
Discourse.ListController.filters.forEach(function(filter) {
Discourse["List" + (filter.capitalize()) + "CategoryRoute"] = Discourse.ListCategoryRoute.extend({ filter: filter });
});

View File

@ -139,7 +139,8 @@ class ListController < ApplicationController
return { return {
page: params[:page], page: params[:page],
topic_ids: param_to_integer_list(:topic_ids), topic_ids: param_to_integer_list(:topic_ids),
exclude_category: (params[:exclude_category] || menu_item.try(:filter)) exclude_category: (params[:exclude_category] || menu_item.try(:filter)),
category: params[:category]
} }
end end

View File

@ -196,7 +196,6 @@ Discourse::Application.routes.draw do
get 'category/:category.rss' => 'list#category_feed', format: :rss, as: 'category_feed' get 'category/:category.rss' => 'list#category_feed', format: :rss, as: 'category_feed'
get 'category/:category' => 'list#category', as: 'category_list' get 'category/:category' => 'list#category', as: 'category_list'
get 'category/:parent_category/:category' => 'list#category', as: 'category_list_parent'
get 'category/:category/more' => 'list#category', as: 'category_list_more' get 'category/:category/more' => 'list#category', as: 'category_list_more'
# We've renamed popular to latest. If people access it we want a permanent redirect. # We've renamed popular to latest. If people access it we want a permanent redirect.
@ -210,8 +209,13 @@ Discourse::Application.routes.draw do
[:latest, :hot, :favorited, :read, :posted, :unread, :new].each do |filter| [:latest, :hot, :favorited, :read, :posted, :unread, :new].each do |filter|
get "#{filter}" => "list##{filter}" get "#{filter}" => "list##{filter}"
get "#{filter}/more" => "list##{filter}" get "#{filter}/more" => "list##{filter}"
get "category/:category/#{filter}" => "list##{filter}"
get "category/:category/#{filter}/more" => "list##{filter}"
end end
get 'category/:parent_category/:category' => 'list#category', as: 'category_list_parent'
get 'search' => 'search#query' get 'search' => 'search#query'
# Topics resource # Topics resource

View File

@ -7,7 +7,7 @@ require_dependency 'suggested_topics_builder'
class TopicQuery class TopicQuery
# Could be rewritten to %i if Ruby 1.9 is no longer supported # Could be rewritten to %i if Ruby 1.9 is no longer supported
VALID_OPTIONS = %w(except_topic_id exclude_category limit page per_page topic_ids visible).map(&:to_sym) VALID_OPTIONS = %w(except_topic_id exclude_category limit page per_page topic_ids visible category).map(&:to_sym)
class << self class << self
# use the constants in conjuction with COALESCE to determine the order with regard to pinned # use the constants in conjuction with COALESCE to determine the order with regard to pinned
@ -233,7 +233,7 @@ class TopicQuery
result = result.listable_topics.includes(category: :topic_only_relative_url) result = result.listable_topics.includes(category: :topic_only_relative_url)
result = result.where('categories.name is null or categories.name <> ?', options[:exclude_category]).references(:categories) if options[:exclude_category] result = result.where('categories.name is null or categories.name <> ?', options[:exclude_category]).references(:categories) if options[:exclude_category]
result = result.where('categories.name = ?', options[:only_category]).references(:categories) if options[:only_category] result = result.where('categories.slug = ?', options[:category]).references(:categories) if options[:category].present?
result = result.limit(options[:per_page]) unless options[:limit] == false result = result.limit(options[:per_page]) unless options[:limit] == false
result = result.visible if options[:visible] || @user.nil? || @user.regular? result = result.visible if options[:visible] || @user.nil? || @user.regular?
result = result.where('topics.id <> ?', options[:except_topic_id]).references(:topics) if options[:except_topic_id] result = result.where('topics.id <> ?', options[:except_topic_id]).references(:topics) if options[:except_topic_id]