FEATURE: per-category default topic list sort order

This commit is contained in:
Neil Lalonde 2016-11-01 12:18:31 -04:00
parent 90ee4e743b
commit 9ef1688a76
10 changed files with 86 additions and 2 deletions

View File

@ -1,7 +1,25 @@
import { setting } from 'discourse/lib/computed';
import { buildCategoryPanel } from 'discourse/components/edit-category-panel';
import computed from "ember-addons/ember-computed-decorators";
export default buildCategoryPanel('settings', {
emailInEnabled: setting('email_in'),
showPositionInput: setting('fixed_category_positions'),
isDefaultSortOrder: Em.computed.empty('category.sort_order'),
@computed
availableSorts() {
return ['likes', 'op_likes', 'views', 'posts', 'activity', 'posters', 'category', 'created']
.map(s => ({ name: I18n.t('category.sort_options.' + s), value: s }))
.sort((a,b) => { return a.name > b.name; });
},
@computed
sortAscendingOptions() {
return [
{name: I18n.t('category.sort_ascending'), value: 'true'},
{name: I18n.t('category.sort_descending'), value: 'false'}
];
}
});

View File

@ -98,7 +98,9 @@ const Category = RestModel.extend({
topic_template: this.get('topic_template'),
suppress_from_homepage: this.get('suppress_from_homepage'),
allowed_tags: this.get('allowed_tags'),
allowed_tag_groups: this.get('allowed_tag_groups')
allowed_tag_groups: this.get('allowed_tag_groups'),
sort_order: this.get('sort_order'),
sort_ascending: this.get('sort_ascending')
},
type: id ? 'PUT' : 'POST'
});

View File

@ -19,6 +19,16 @@
</label>
</section>
<section class="field">
<label>
{{i18n "category.sort_order"}}
{{combo-box valueAttribute="value" content=availableSorts value=category.sort_order none="category.sort_options.default"}}
{{#unless isDefaultSortOrder}}
{{combo-box valueAttribute="value" content=sortAscendingOptions value=category.sort_ascending none="category.sort_options.default"}}
{{/unless}}
</label>
</section>
{{#if emailInEnabled}}
<section class='field'>
<label>

View File

@ -238,6 +238,8 @@ class CategoriesController < ApplicationController
:slug,
:allow_badges,
:topic_template,
:sort_order,
:sort_ascending,
:custom_fields => [params[:custom_fields].try(:keys)],
:permissions => [*p.try(:keys)],
:allowed_tags => [],

View File

@ -536,6 +536,8 @@ end
# topic_template :text
# suppress_from_homepage :boolean default(FALSE)
# contains_messages :boolean
# sort_order :string
# sort_ascending :boolean
#
# Indexes
#

View File

@ -19,7 +19,9 @@ class BasicCategorySerializer < ApplicationSerializer
:notification_level,
:can_edit,
:topic_template,
:has_children
:has_children,
:sort_order,
:sort_ascending
def include_parent_category_id?
parent_category_id

View File

@ -1883,6 +1883,7 @@ en:
email_in_disabled: "Posting new topics via email is disabled in the Site Settings. To enable posting new topics via email, "
email_in_disabled_click: 'enable the "email in" setting.'
suppress_from_homepage: "Suppress this category from the homepage."
sort_order: "Default Sort:"
allow_badges_label: "Allow badges to be awarded in this category"
edit_permissions: "Edit Permissions"
add_permission: "Add Permission"
@ -1908,6 +1909,18 @@ en:
muted:
title: "Muted"
description: "You will never be notified of anything about new topics in these categories, and they will not appear in latest."
sort_options:
default: "default"
likes: "Likes"
op_likes: "Original Post Likes"
views: "Views"
posts: "Posts"
activity: "Activity"
posters: "Posters"
category: "Category"
created: "Created"
sort_ascending: 'Ascending'
sort_descending: 'Descending'
flagging:
title: 'Thanks for helping to keep our community civil!'

View File

@ -0,0 +1,6 @@
class AddSortFieldsToCategories < ActiveRecord::Migration
def change
add_column :categories, :sort_order, :string
add_column :categories, :sort_ascending, :boolean
end
end

View File

@ -450,6 +450,15 @@ class TopicQuery
result = result.where('categories.id = :category_id OR (categories.parent_category_id = :category_id AND categories.topic_id <> topics.id)', category_id: category_id)
end
result = result.references(:categories)
if !@options[:order]
# category default sort order
sort_order, sort_ascending = Category.where(id: category_id).pluck(:sort_order, :sort_ascending).first
if sort_order
options[:order] = sort_order
options[:ascending] = !!sort_ascending ? 'true' : 'false'
end
end
end
# ALL TAGS: something like this?

View File

@ -379,6 +379,26 @@ describe TopicQuery do
).to eq([topic_in_cat2.id, topic_category.id, topic_in_cat1.id])
end
end
describe "category default sort order" do
it "can use category's default sort order" do
category.update_attributes!(sort_order: 'created', sort_ascending: true)
topic_ids = TopicQuery.new(user, category: category.id).list_latest.topics.map(&:id)
expect(topic_ids - [topic_category.id]).to eq([topic_in_cat1.id, topic_in_cat2.id])
end
it "ignores invalid order value" do
category.update_attributes!(sort_order: 'funny')
topic_ids = TopicQuery.new(user, category: category.id).list_latest.topics.map(&:id)
expect(topic_ids - [topic_category.id]).to eq([topic_in_cat2.id, topic_in_cat1.id])
end
it "can be overridden" do
category.update_attributes!(sort_order: 'created', sort_ascending: true)
topic_ids = TopicQuery.new(user, category: category.id, order: 'activity').list_latest.topics.map(&:id)
expect(topic_ids - [topic_category.id]).to eq([topic_in_cat2.id, topic_in_cat1.id])
end
end
end
context 'unread / read topics' do