From 720e1965e3da431c56cef6719d857c603b7aa9ef Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 22 Feb 2018 09:56:18 +1100 Subject: [PATCH] FEATURE: add category suppress from latest In the past we used suppress_from_homepage, it had mixed semantics it would remove from category list if category list was on home and unconditionally remove from latest. New setting explicitly only removes from latest list but leaves the category list alond --- .../discourse/models/category.js.es6 | 2 +- .../models/topic-tracking-state.js.es6 | 4 +-- .../components/edit-category-settings.hbs | 4 +-- app/controllers/categories_controller.rb | 4 +-- app/controllers/list_controller.rb | 4 +-- app/models/category.rb | 2 +- app/models/category_list.rb | 1 - app/models/site.rb | 4 +-- app/serializers/category_serializer.rb | 4 +-- app/serializers/site_serializer.rb | 2 +- config/locales/client.en.yml | 2 +- db/fixtures/001_categories.rb | 4 +-- ..._add_suppress_from_latest_to_categories.rb | 11 ++++++++ lib/import_export/base_exporter.rb | 2 +- script/import_scripts/discuz_x.rb | 2 +- spec/controllers/list_controller_spec.rb | 6 ++--- spec/fixtures/json/import-export.json | 12 ++++----- spec/requests/list_controller_spec.rb | 27 +++++++++++++++++++ 18 files changed, 67 insertions(+), 30 deletions(-) create mode 100644 db/migrate/20180221215641_add_suppress_from_latest_to_categories.rb diff --git a/app/assets/javascripts/discourse/models/category.js.es6 b/app/assets/javascripts/discourse/models/category.js.es6 index 52c3eb443c7..1eca95047e9 100644 --- a/app/assets/javascripts/discourse/models/category.js.es6 +++ b/app/assets/javascripts/discourse/models/category.js.es6 @@ -97,7 +97,7 @@ const Category = RestModel.extend({ allow_badges: this.get('allow_badges'), custom_fields: this.get('custom_fields'), topic_template: this.get('topic_template'), - suppress_from_homepage: this.get('suppress_from_homepage'), + suppress_from_latest: this.get('suppress_from_latest'), all_topics_wiki: this.get('all_topics_wiki'), allowed_tags: this.get('allowed_tags'), allowed_tag_groups: this.get('allowed_tag_groups'), diff --git a/app/assets/javascripts/discourse/models/topic-tracking-state.js.es6 b/app/assets/javascripts/discourse/models/topic-tracking-state.js.es6 index d4c3a0354a3..5e7cabd775e 100644 --- a/app/assets/javascripts/discourse/models/topic-tracking-state.js.es6 +++ b/app/assets/javascripts/discourse/models/topic-tracking-state.js.es6 @@ -117,8 +117,8 @@ const TopicTrackingState = Discourse.Model.extend({ } if (filter === defaultHomepage()) { - const suppressed_from_homepage_category_ids = Discourse.Site.currentProp("suppressed_from_homepage_category_ids"); - if (_.include(suppressed_from_homepage_category_ids, data.payload.category_id)) { + const suppressed_from_latest_category_ids = Discourse.Site.currentProp("suppressed_from_latest_category_ids"); + if (_.include(suppressed_from_latest_category_ids, data.payload.category_id)) { return; } } diff --git a/app/assets/javascripts/discourse/templates/components/edit-category-settings.hbs b/app/assets/javascripts/discourse/templates/components/edit-category-settings.hbs index fa7b2a5653e..2c9735d40cc 100644 --- a/app/assets/javascripts/discourse/templates/components/edit-category-settings.hbs +++ b/app/assets/javascripts/discourse/templates/components/edit-category-settings.hbs @@ -21,8 +21,8 @@
diff --git a/app/controllers/categories_controller.rb b/app/controllers/categories_controller.rb index 58a95610c4d..9c95a1da071 100644 --- a/app/controllers/categories_controller.rb +++ b/app/controllers/categories_controller.rb @@ -61,7 +61,7 @@ class CategoriesController < ApplicationController topic_options = { per_page: SiteSetting.categories_topics, no_definitions: true, - exclude_category_ids: Category.where(suppress_from_homepage: true).pluck(:id) + exclude_category_ids: Category.where(suppress_from_latest: true).pluck(:id) } result = CategoryAndTopicLists.new @@ -235,7 +235,7 @@ class CategoriesController < ApplicationController :email_in, :email_in_allow_strangers, :mailinglist_mirror, - :suppress_from_homepage, + :suppress_from_latest, :all_topics_wiki, :parent_category_id, :auto_close_hours, diff --git a/app/controllers/list_controller.rb b/app/controllers/list_controller.rb index e79137211c0..e5f30bcae54 100644 --- a/app/controllers/list_controller.rb +++ b/app/controllers/list_controller.rb @@ -63,7 +63,7 @@ class ListController < ApplicationController if filter == :latest list_opts[:no_definitions] = true end - if filter.to_s == current_homepage + if [:latest, :categories].include?(filter) list_opts[:exclude_category_ids] = get_excluded_category_ids(list_opts[:category]) end end @@ -369,7 +369,7 @@ class ListController < ApplicationController end def get_excluded_category_ids(current_category = nil) - exclude_category_ids = Category.where(suppress_from_homepage: true) + exclude_category_ids = Category.where(suppress_from_latest: true) exclude_category_ids = exclude_category_ids.where.not(id: current_category) if current_category exclude_category_ids.pluck(:id) end diff --git a/app/models/category.rb b/app/models/category.rb index cdc03b5bf51..ef8893209e7 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -542,7 +542,7 @@ end # name_lower :string(50) not null # auto_close_based_on_last_post :boolean default(FALSE) # topic_template :text -# suppress_from_homepage :boolean default(FALSE) +# suppress_from_latest :boolean default(FALSE) # contains_messages :boolean # sort_order :string # sort_ascending :boolean diff --git a/app/models/category_list.rb b/app/models/category_list.rb index ccc23d88b65..76511d5b8bc 100644 --- a/app/models/category_list.rb +++ b/app/models/category_list.rb @@ -72,7 +72,6 @@ class CategoryList subcategories: [:topic_only_relative_url] ).secured(@guardian) - @categories = @categories.where(suppress_from_homepage: false) if @options[:is_homepage] @categories = @categories.where("categories.parent_category_id = ?", @options[:parent_category_id].to_i) if @options[:parent_category_id].present? if SiteSetting.fixed_category_positions diff --git a/app/models/site.rb b/app/models/site.rb index 9d90b2c1bc3..b3001e49d9f 100644 --- a/app/models/site.rb +++ b/app/models/site.rb @@ -71,8 +71,8 @@ class Site end end - def suppressed_from_homepage_category_ids - categories.select { |c| c.suppress_from_homepage == true }.map(&:id) + def suppressed_from_latest_category_ids + categories.select { |c| c.suppress_from_latest == true }.map(&:id) end def archetypes diff --git a/app/serializers/category_serializer.rb b/app/serializers/category_serializer.rb index 3c5f98a97bf..d9d37497a2f 100644 --- a/app/serializers/category_serializer.rb +++ b/app/serializers/category_serializer.rb @@ -9,7 +9,7 @@ class CategorySerializer < BasicCategorySerializer :email_in, :email_in_allow_strangers, :mailinglist_mirror, - :suppress_from_homepage, + :suppress_from_latest, :all_topics_wiki, :can_delete, :cannot_delete_reason, @@ -72,7 +72,7 @@ class CategorySerializer < BasicCategorySerializer scope && scope.can_edit?(object) end - def include_suppress_from_homepage? + def include_suppress_from_latest? scope && scope.can_edit?(object) end diff --git a/app/serializers/site_serializer.rb b/app/serializers/site_serializer.rb index 850a8dd8dd3..fce74a802ae 100644 --- a/app/serializers/site_serializer.rb +++ b/app/serializers/site_serializer.rb @@ -16,7 +16,7 @@ class SiteSerializer < ApplicationSerializer :is_readonly, :disabled_plugins, :user_field_max_length, - :suppressed_from_homepage_category_ids, + :suppressed_from_latest_category_ids, :post_action_types, :topic_flag_types, :can_create_tag, diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index a0069439d1e..284f70a6fd5 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -2209,7 +2209,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.' mailinglist_mirror: "Category mirrors a mailing list" - suppress_from_homepage: "Suppress this category from the homepage." + suppress_from_latest: "Suppress category from latest topics." show_subcategory_list: "Show subcategory list above topics in this category." num_featured_topics: "Number of topics shown on the categories page:" subcategory_num_featured_topics: "Number of featured topics on parent category's page:" diff --git a/db/fixtures/001_categories.rb b/db/fixtures/001_categories.rb index 4dc13268ac5..98b0be1a32d 100644 --- a/db/fixtures/001_categories.rb +++ b/db/fixtures/001_categories.rb @@ -28,8 +28,8 @@ end ColumnDropper.drop( table: 'categories', - after_migration: 'AddUploadsToCategories', - columns: ['logo_url', 'background_url'], + after_migration: 'AddSuppressFromLatestToCategories', + columns: ['logo_url', 'background_url', 'suppress_from_homepage'], on_drop: ->() { STDERR.puts 'Removing superflous categories columns!' } diff --git a/db/migrate/20180221215641_add_suppress_from_latest_to_categories.rb b/db/migrate/20180221215641_add_suppress_from_latest_to_categories.rb new file mode 100644 index 00000000000..3a812a3c115 --- /dev/null +++ b/db/migrate/20180221215641_add_suppress_from_latest_to_categories.rb @@ -0,0 +1,11 @@ +class AddSuppressFromLatestToCategories < ActiveRecord::Migration[5.1] + def up + add_column :categories, :suppress_from_latest, :boolean, default: false + execute <<~SQL + UPDATE categories SET suppress_from_latest = suppress_from_homepage + SQL + end + def down + raise "can not be removed" + end +end diff --git a/lib/import_export/base_exporter.rb b/lib/import_export/base_exporter.rb index dec228fbc0b..58d3b77e4e8 100644 --- a/lib/import_export/base_exporter.rb +++ b/lib/import_export/base_exporter.rb @@ -5,7 +5,7 @@ module ImportExport CATEGORY_ATTRS = [:id, :name, :color, :created_at, :user_id, :slug, :description, :text_color, :auto_close_hours, :parent_category_id, :auto_close_based_on_last_post, - :topic_template, :suppress_from_homepage, :all_topics_wiki, :permissions_params] + :topic_template, :suppress_from_latest, :all_topics_wiki, :permissions_params] GROUP_ATTRS = [ :id, :name, :created_at, :mentionable_level, :messageable_level, :visibility_level, :automatic_membership_email_domains, :automatic_membership_retroactive, diff --git a/script/import_scripts/discuz_x.rb b/script/import_scripts/discuz_x.rb index 8ee30b359cd..8f99ae49113 100644 --- a/script/import_scripts/discuz_x.rb +++ b/script/import_scripts/discuz_x.rb @@ -273,7 +273,7 @@ class ImportScripts::DiscuzX < ImportScripts::Base description: row['description'], position: row['position'].to_i + max_position, color: color, - suppress_from_homepage: (row['status'] == (0) || row['status'] == (3)), + suppress_from_latest: (row['status'] == (0) || row['status'] == (3)), post_create_action: lambda do |category| if slug = @category_slug[row['id']] category.update(slug: slug) diff --git a/spec/controllers/list_controller_spec.rb b/spec/controllers/list_controller_spec.rb index e47ceac19d3..7a04838d817 100644 --- a/spec/controllers/list_controller_spec.rb +++ b/spec/controllers/list_controller_spec.rb @@ -364,13 +364,13 @@ describe ListController do describe "categories suppression" do let(:category_one) { Fabricate(:category) } - let(:sub_category) { Fabricate(:category, parent_category: category_one, suppress_from_homepage: true) } + let(:sub_category) { Fabricate(:category, parent_category: category_one, suppress_from_latest: true) } let!(:topic_in_sub_category) { Fabricate(:topic, category: sub_category) } - let(:category_two) { Fabricate(:category, suppress_from_homepage: true) } + let(:category_two) { Fabricate(:category, suppress_from_latest: true) } let!(:topic_in_category_two) { Fabricate(:topic, category: category_two) } - it "suppresses categories from the homepage" do + it "suppresses categories from the latest list" do get SiteSetting.homepage, format: :json expect(response).to be_success diff --git a/spec/fixtures/json/import-export.json b/spec/fixtures/json/import-export.json index 1bc4da072a9..02446824cbb 100644 --- a/spec/fixtures/json/import-export.json +++ b/spec/fixtures/json/import-export.json @@ -4,12 +4,12 @@ {"id":42,"name":"custom_group_import","created_at":"2017-10-26T15:33:46.328Z","mentionable_level":0,"messageable_level":0,"visibility_level":0,"automatic_membership_email_domains":"","automatic_membership_retroactive":false,"primary_group":false,"title":null,"grant_trust_level":null,"incoming_email":null,"user_ids":[2]} ], "categories":[ - {"id":8,"name":"Custom Category","color":"AB9364","created_at":"2017-10-26T15:32:44.083Z","user_id":1,"slug":"custom-category","description":null,"text_color":"FFFFFF","auto_close_hours":null,"parent_category_id":3,"auto_close_based_on_last_post":false,"topic_template":"","suppress_from_homepage":false,"all_topics_wiki":false,"permissions_params":{"custom_group":1,"everyone":2}}, - {"id":10,"name":"Site Feedback Import","color":"808281","created_at":"2017-10-26T17:12:39.995Z","user_id":-1,"slug":"site-feedback-import","description":"Discussion about this site, its organization, how it works, and how we can improve it.","text_color":"FFFFFF","auto_close_hours":null,"parent_category_id":null,"auto_close_based_on_last_post":false,"topic_template":null,"suppress_from_homepage":false,"all_topics_wiki":false,"permissions_params":{}}, - {"id":11,"name":"Uncategorized Import","color":"AB9364","created_at":"2017-10-26T17:12:32.359Z","user_id":-1,"slug":"uncategorized-import","description":"","text_color":"FFFFFF","auto_close_hours":null,"parent_category_id":null,"auto_close_based_on_last_post":false,"topic_template":null,"suppress_from_homepage":false,"all_topics_wiki":false,"permissions_params":{}}, - {"id":12,"name":"Lounge Import","color":"EEEEEE","created_at":"2017-10-26T17:12:39.490Z","user_id":-1,"slug":"lounge-import","description":"A category exclusive to members with trust level 3 and higher.","text_color":"652D90","auto_close_hours":null,"parent_category_id":null,"auto_close_based_on_last_post":false,"topic_template":null,"suppress_from_homepage":false,"all_topics_wiki":false,"permissions_params":{"trust_level_3":1}}, - {"id":13,"name":"Staff Import","color":"283890","created_at":"2017-10-26T17:12:42.806Z","user_id":2,"slug":"staff-import","description":"Private category for staff discussions. Topics are only visible to admins and moderators.","text_color":"FFFFFF","auto_close_hours":null,"parent_category_id":null,"auto_close_based_on_last_post":false,"topic_template":null,"suppress_from_homepage":false,"all_topics_wiki":false,"permissions_params":{"custom_group_import":1,"staff":1}}, - {"id":15,"name":"Custom Category Import","color":"AB9364","created_at":"2017-10-26T15:32:44.083Z","user_id":2,"slug":"custom-category-import","description":null,"text_color":"FFFFFF","auto_close_hours":null,"parent_category_id":10,"auto_close_based_on_last_post":false,"topic_template":"","suppress_from_homepage":false,"all_topics_wiki":false,"permissions_params":{"custom_group_import":1,"everyone":2}} + {"id":8,"name":"Custom Category","color":"AB9364","created_at":"2017-10-26T15:32:44.083Z","user_id":1,"slug":"custom-category","description":null,"text_color":"FFFFFF","auto_close_hours":null,"parent_category_id":3,"auto_close_based_on_last_post":false,"topic_template":"","suppress_from_latest":false,"all_topics_wiki":false,"permissions_params":{"custom_group":1,"everyone":2}}, + {"id":10,"name":"Site Feedback Import","color":"808281","created_at":"2017-10-26T17:12:39.995Z","user_id":-1,"slug":"site-feedback-import","description":"Discussion about this site, its organization, how it works, and how we can improve it.","text_color":"FFFFFF","auto_close_hours":null,"parent_category_id":null,"auto_close_based_on_last_post":false,"topic_template":null,"suppress_from_latest":false,"all_topics_wiki":false,"permissions_params":{}}, + {"id":11,"name":"Uncategorized Import","color":"AB9364","created_at":"2017-10-26T17:12:32.359Z","user_id":-1,"slug":"uncategorized-import","description":"","text_color":"FFFFFF","auto_close_hours":null,"parent_category_id":null,"auto_close_based_on_last_post":false,"topic_template":null,"suppress_from_latest":false,"all_topics_wiki":false,"permissions_params":{}}, + {"id":12,"name":"Lounge Import","color":"EEEEEE","created_at":"2017-10-26T17:12:39.490Z","user_id":-1,"slug":"lounge-import","description":"A category exclusive to members with trust level 3 and higher.","text_color":"652D90","auto_close_hours":null,"parent_category_id":null,"auto_close_based_on_last_post":false,"topic_template":null,"suppress_from_latest":false,"all_topics_wiki":false,"permissions_params":{"trust_level_3":1}}, + {"id":13,"name":"Staff Import","color":"283890","created_at":"2017-10-26T17:12:42.806Z","user_id":2,"slug":"staff-import","description":"Private category for staff discussions. Topics are only visible to admins and moderators.","text_color":"FFFFFF","auto_close_hours":null,"parent_category_id":null,"auto_close_based_on_last_post":false,"topic_template":null,"suppress_from_latest":false,"all_topics_wiki":false,"permissions_params":{"custom_group_import":1,"staff":1}}, + {"id":15,"name":"Custom Category Import","color":"AB9364","created_at":"2017-10-26T15:32:44.083Z","user_id":2,"slug":"custom-category-import","description":null,"text_color":"FFFFFF","auto_close_hours":null,"parent_category_id":10,"auto_close_based_on_last_post":false,"topic_template":"","suppress_from_latest":false,"all_topics_wiki":false,"permissions_params":{"custom_group_import":1,"everyone":2}} ], "users":[ {"id":1,"email":"email@example.com","username":"example","name":"Example","created_at":"2017-10-07T15:01:24.597Z","trust_level":4,"active":true,"last_emailed_at":null}, diff --git a/spec/requests/list_controller_spec.rb b/spec/requests/list_controller_spec.rb index 9a0d21a5c95..abe124e9802 100644 --- a/spec/requests/list_controller_spec.rb +++ b/spec/requests/list_controller_spec.rb @@ -15,6 +15,33 @@ RSpec.describe ListController do end end + describe 'suppress from latest' do + + it 'supresses categories' do + topic + + get "/latest.json" + data = JSON.parse(response.body) + expect(data["topic_list"]["topics"].length).to eq(1) + + get "/categories_and_latest.json" + data = JSON.parse(response.body) + expect(data["topic_list"]["topics"].length).to eq(1) + + topic.category.suppress_from_latest = true + topic.category.save + + get "/latest.json" + data = JSON.parse(response.body) + expect(data["topic_list"]["topics"].length).to eq(0) + + get "/categories_and_latest.json" + data = JSON.parse(response.body) + expect(data["topic_list"]["topics"].length).to eq(0) + end + + end + describe 'titles for crawler layout' do it 'has no title for the default URL' do topic