FEATURE: category setting for default top period

This commit is contained in:
Neil Lalonde 2017-03-22 16:54:12 -04:00
parent 0c46f51412
commit 11ce73b8ed
10 changed files with 49 additions and 34 deletions

View File

@ -27,6 +27,13 @@ export default buildCategoryPanel('settings', {
]; ];
}, },
@computed
availableTopPeriods() {
return ['all', 'yearly', 'quarterly', 'monthly', 'weekly', 'daily'].map((p) => {
return {name: I18n.t(`filters.top.${p}.title`), value: p};
});
},
@computed @computed
availableSorts() { availableSorts() {
return ['likes', 'op_likes', 'views', 'posts', 'activity', 'posters', 'category', 'created'] return ['likes', 'op_likes', 'views', 'posts', 'activity', 'posters', 'category', 'created']

View File

@ -106,7 +106,8 @@ const Category = RestModel.extend({
show_subcategory_list: this.get('show_subcategory_list'), show_subcategory_list: this.get('show_subcategory_list'),
num_featured_topics: this.get('num_featured_topics'), num_featured_topics: this.get('num_featured_topics'),
default_view: this.get('default_view'), default_view: this.get('default_view'),
subcategory_list_style: this.get('subcategory_list_style') subcategory_list_style: this.get('subcategory_list_style'),
default_top_period: this.get('default_top_period')
}, },
type: id ? 'PUT' : 'POST' type: id ? 'PUT' : 'POST'
}); });

View File

@ -44,6 +44,13 @@
</label> </label>
</section> </section>
<section class="field default-top-period-field">
<label>
{{i18n "category.default_top_period"}}
{{combo-box valueAttribute="value" content=availableTopPeriods value=category.default_top_period}}
</label>
</section>
<section class="field"> <section class="field">
<label> <label>
{{i18n "category.sort_order"}} {{i18n "category.sort_order"}}

View File

@ -249,6 +249,7 @@ class CategoriesController < ApplicationController
:num_featured_topics, :num_featured_topics,
:default_view, :default_view,
:subcategory_list_style, :subcategory_list_style,
:default_top_period,
:custom_fields => [params[:custom_fields].try(:keys)], :custom_fields => [params[:custom_fields].try(:keys)],
:permissions => [*p.try(:keys)], :permissions => [*p.try(:keys)],
:allowed_tags => [], :allowed_tags => [],

View File

@ -366,8 +366,15 @@ class ListController < ApplicationController
exclude_category_ids.pluck(:id) exclude_category_ids.pluck(:id)
end end
def self.best_period_with_topics_for(previous_visit_at, category_id=nil) def self.best_period_for(previous_visit_at, category_id=nil)
best_periods_for(previous_visit_at).each do |period| default_period = ((category_id && Category.where(id: category_id).pluck(:default_top_period).first) ||
SiteSetting.top_page_default_timeframe).to_sym
best_period_with_topics_for(previous_visit_at, category_id, default_period) || default_period
end
def self.best_period_with_topics_for(previous_visit_at, category_id=nil, default_period=SiteSetting.top_page_default_timeframe)
best_periods_for(previous_visit_at, default_period.to_sym).each do |period|
top_topics = TopTopic.where("#{period}_score > 0") top_topics = TopTopic.where("#{period}_score > 0")
top_topics = top_topics.joins(:topic).where("topics.category_id = ?", category_id) if category_id top_topics = top_topics.joins(:topic).where("topics.category_id = ?", category_id) if category_id
top_topics = top_topics.limit(SiteSetting.topics_per_period_in_top_page) top_topics = top_topics.limit(SiteSetting.topics_per_period_in_top_page)
@ -377,14 +384,8 @@ class ListController < ApplicationController
false false
end end
def self.best_period_for(previous_visit_at, category_id=nil) def self.best_periods_for(date, default_period=:all)
best_period_with_topics_for(previous_visit_at, category_id) ||
SiteSetting.top_page_default_timeframe.to_sym
end
def self.best_periods_for(date)
date ||= 1.year.ago date ||= 1.year.ago
default_period = SiteSetting.top_page_default_timeframe.to_sym
periods = [] periods = []
periods << default_period if :all != default_period periods << default_period if :all != default_period
periods << :daily if :daily != default_period && date > 8.days.ago periods << :daily if :daily != default_period && date > 8.days.ago

View File

@ -561,6 +561,7 @@ end
# num_featured_topics :integer default(3) # num_featured_topics :integer default(3)
# default_view :string(50) # default_view :string(50)
# subcategory_list_style :string(50) default("rows_with_featured_topics") # subcategory_list_style :string(50) default("rows_with_featured_topics")
# default_top_period :string(20) default("all")
# #
# Indexes # Indexes
# #

View File

@ -23,7 +23,8 @@ class BasicCategorySerializer < ApplicationSerializer
:show_subcategory_list, :show_subcategory_list,
:num_featured_topics, :num_featured_topics,
:default_view, :default_view,
:subcategory_list_style :subcategory_list_style,
:default_top_period
has_one :uploaded_logo, embed: :object, serializer: CategoryUploadSerializer has_one :uploaded_logo, embed: :object, serializer: CategoryUploadSerializer
has_one :uploaded_background, embed: :object, serializer: CategoryUploadSerializer has_one :uploaded_background, embed: :object, serializer: CategoryUploadSerializer

View File

@ -1993,6 +1993,7 @@ en:
subcategory_list_style: "Subcategory List Style:" subcategory_list_style: "Subcategory List Style:"
sort_order: "Topic List Sort By:" sort_order: "Topic List Sort By:"
default_view: "Default Topic List:" default_view: "Default Topic List:"
default_top_period: "Default Top Period:"
allow_badges_label: "Allow badges to be awarded in this category" allow_badges_label: "Allow badges to be awarded in this category"
edit_permissions: "Edit Permissions" edit_permissions: "Edit Permissions"
add_permission: "Add Permission" add_permission: "Add Permission"

View File

@ -0,0 +1,5 @@
class AddDefaultTopPeriodToCategories < ActiveRecord::Migration
def change
add_column :categories, :default_top_period, :string, limit: 20, default: 'all'
end
end

View File

@ -193,8 +193,8 @@ describe ListController do
describe "category default views" do describe "category default views" do
it "top default view" do it "top default view" do
category.update_attributes!(default_view: 'top') category.update_attributes!(default_view: 'top', default_top_period: 'monthly')
described_class.expects(:best_period_for).returns('yearly') described_class.expects(:best_period_with_topics_for).with(anything, category.id, :monthly).returns(:monthly)
xhr :get, :category_default, category: category.slug xhr :get, :category_default, category: category.slug
expect(response).to be_success expect(response).to be_success
end end
@ -291,62 +291,52 @@ describe ListController do
describe "best_periods_for" do describe "best_periods_for" do
it "returns yearly for more than 180 days" do it "returns yearly for more than 180 days" do
SiteSetting.top_page_default_timeframe = 'all' expect(ListController.best_periods_for(nil, :all)).to eq([:yearly])
expect(ListController.best_periods_for(nil)).to eq([:yearly]) expect(ListController.best_periods_for(180.days.ago, :all)).to eq([:yearly])
expect(ListController.best_periods_for(180.days.ago)).to eq([:yearly])
end end
it "includes monthly when less than 180 days and more than 35 days" do it "includes monthly when less than 180 days and more than 35 days" do
SiteSetting.top_page_default_timeframe = 'all'
(35...180).each do |date| (35...180).each do |date|
expect(ListController.best_periods_for(date.days.ago)).to eq([:monthly, :yearly]) expect(ListController.best_periods_for(date.days.ago, :all)).to eq([:monthly, :yearly])
end end
end end
it "includes weekly when less than 35 days and more than 8 days" do it "includes weekly when less than 35 days and more than 8 days" do
SiteSetting.top_page_default_timeframe = 'all'
(8...35).each do |date| (8...35).each do |date|
expect(ListController.best_periods_for(date.days.ago)).to eq([:weekly, :monthly, :yearly]) expect(ListController.best_periods_for(date.days.ago, :all)).to eq([:weekly, :monthly, :yearly])
end end
end end
it "includes daily when less than 8 days" do it "includes daily when less than 8 days" do
SiteSetting.top_page_default_timeframe = 'all'
(0...8).each do |date| (0...8).each do |date|
expect(ListController.best_periods_for(date.days.ago)).to eq([:daily, :weekly, :monthly, :yearly]) expect(ListController.best_periods_for(date.days.ago, :all)).to eq([:daily, :weekly, :monthly, :yearly])
end end
end end
it "returns default even for more than 180 days" do it "returns default even for more than 180 days" do
SiteSetting.top_page_default_timeframe = 'monthly' expect(ListController.best_periods_for(nil, :monthly)).to eq([:monthly, :yearly])
expect(ListController.best_periods_for(nil)).to eq([:monthly, :yearly]) expect(ListController.best_periods_for(180.days.ago, :monthly)).to eq([:monthly, :yearly])
expect(ListController.best_periods_for(180.days.ago)).to eq([:monthly, :yearly])
end end
it "returns default even when less than 180 days and more than 35 days" do it "returns default even when less than 180 days and more than 35 days" do
SiteSetting.top_page_default_timeframe = 'weekly'
(35...180).each do |date| (35...180).each do |date|
expect(ListController.best_periods_for(date.days.ago)).to eq([:weekly, :monthly, :yearly]) expect(ListController.best_periods_for(date.days.ago, :weekly)).to eq([:weekly, :monthly, :yearly])
end end
end end
it "returns default even when less than 35 days and more than 8 days" do it "returns default even when less than 35 days and more than 8 days" do
SiteSetting.top_page_default_timeframe = 'daily'
(8...35).each do |date| (8...35).each do |date|
expect(ListController.best_periods_for(date.days.ago)).to eq([:daily, :weekly, :monthly, :yearly]) expect(ListController.best_periods_for(date.days.ago, :daily)).to eq([:daily, :weekly, :monthly, :yearly])
end end
end end
it "doesn't return default when set to all" do it "doesn't return default when set to all" do
SiteSetting.top_page_default_timeframe = 'all' expect(ListController.best_periods_for(nil, :all)).to eq([:yearly])
expect(ListController.best_periods_for(nil)).to eq([:yearly])
end end
it "doesn't return value twice when matches default" do it "doesn't return value twice when matches default" do
SiteSetting.top_page_default_timeframe = 'yearly' expect(ListController.best_periods_for(nil, :yearly)).to eq([:yearly])
expect(ListController.best_periods_for(nil)).to eq([:yearly])
end end
end end
describe "categories suppression" do describe "categories suppression" do