Skip to content

A platform for community discussion. Free, open, simple.

FIX: Possible crash during category creation (#35277)

David Taylorb620031b8dc9
diff --git a/app/assets/javascripts/discourse/app/controllers/edit-category/tabs.js b/app/assets/javascripts/discourse/app/controllers/edit-category/tabs.js
index de0204e6126..8a1cda89a91 100644
--- a/app/assets/javascripts/discourse/app/controllers/edit-category/tabs.js
+++ b/app/assets/javascripts/discourse/app/controllers/edit-category/tabs.js
@@ -6,10 +6,8 @@ import { service } from "@ember/service";
import { underscore } from "@ember/string";
import { popupAjaxError } from "discourse/lib/ajax-error";
import discourseComputed from "discourse/lib/decorators";
-import { NotificationLevels } from "discourse/lib/notification-levels";
import DiscourseURL from "discourse/lib/url";
import Category from "discourse/models/category";
-import PermissionType from "discourse/models/permission-type";
import { i18n } from "discourse-i18n";
const FIELD_LIST = [
@@ -124,17 +122,11 @@ export default class EditCategoryTabsController extends Controller {
.save()
.then((result) => {
if (!this.model.id) {
- this.model.setProperties({
- slug: result.category.slug,
- id: result.category.id,
- can_edit: result.category.can_edit,
- permission: PermissionType.FULL,
- notification_level: NotificationLevels.REGULAR,
- });
- this.site.updateCategory(this.model);
+ const updatedModel = this.site.updateCategory(result.category);
+ updatedModel.setupGroupsAndPermissions();
this.router.transitionTo(
"editCategory",
- Category.slugFor(this.model)
+ Category.slugFor(updatedModel)
);
}
// force a reload of the category list to track changes to style type
diff --git a/app/assets/javascripts/discourse/app/models/category.js b/app/assets/javascripts/discourse/app/models/category.js
index 4d4bdbd93f8..e5e0f3b1d05 100644
--- a/app/assets/javascripts/discourse/app/models/category.js
+++ b/app/assets/javascripts/discourse/app/models/category.js
@@ -323,10 +323,9 @@ export default class Category extends RestModel {
}
static _includePermissions(category, store, site) {
- const record = store.createRecord("category", category);
- record.setupGroupsAndPermissions();
- site.updateCategory(record);
- return record;
+ const model = site.updateCategory(category);
+ model.setupGroupsAndPermissions();
+ return model;
}
static search(term, opts) {
diff --git a/app/assets/javascripts/discourse/app/models/site.js b/app/assets/javascripts/discourse/app/models/site.js
index cc12215d864..39e8afe5df6 100644
--- a/app/assets/javascripts/discourse/app/models/site.js
+++ b/app/assets/javascripts/discourse/app/models/site.js
@@ -327,6 +327,12 @@ export default class Site extends RestModel {
}
updateCategory(newCategory) {
+ if (newCategory instanceof Category) {
+ throw new Error(
+ "updateCategory should be passed a pojo, not a category model instance"
+ );
+ }
+
const categories = this.categories;
const categoryId = get(newCategory, "id");
const existingCategory = categories.find((c) => c.id === categoryId);
diff --git a/app/assets/javascripts/discourse/tests/fixtures/category-fixtures.js b/app/assets/javascripts/discourse/tests/fixtures/category-fixtures.js
index a223cc33930..0eb1dc2bc45 100644
--- a/app/assets/javascripts/discourse/tests/fixtures/category-fixtures.js
+++ b/app/assets/javascripts/discourse/tests/fixtures/category-fixtures.js
@@ -47,8 +47,68 @@ export default {
color: "0088CC",
text_color: "FFFFFF",
style_type: "square",
+ icon: null,
+ emoji: null,
slug: "testing",
+ topic_count: 0,
+ post_count: 0,
+ position: 1,
+ description: null,
+ description_text: null,
+ description_excerpt: null,
+ topic_url: null,
+ read_restricted: false,
+ permission: 1,
+ notification_level: 1,
can_edit: true,
+ topic_template: null,
+ has_children: null,
+ subcategory_count: null,
+ sort_order: null,
+ sort_ascending: null,
+ show_subcategory_list: false,
+ num_featured_topics: 3,
+ default_view: null,
+ subcategory_list_style: "rows_with_featured_topics",
+ default_top_period: "all",
+ default_list_filter: "all",
+ minimum_required_tags: 0,
+ navigate_to_first_post_after_read: false,
+ custom_fields: {},
+ allowed_tags: [],
+ allowed_tag_groups: [],
+ allow_global_tags: false,
+ read_only_banner: null,
+ form_template_ids: [],
+ available_groups: [
+ "admins",
+ "moderators",
+ "staff",
+ "trust_level_0",
+ "trust_level_1",
+ "trust_level_2",
+ "trust_level_3",
+ "trust_level_4",
+ ],
+ auto_close_hours: null,
+ auto_close_based_on_last_post: false,
+ group_permissions: [{ permission_type: 1, group_name: "everyone" }],
+ email_in: null,
+ email_in_allow_strangers: false,
+ mailinglist_mirror: false,
+ all_topics_wiki: false,
+ allow_unlimited_owner_edits_on_first_post: false,
+ can_delete: true,
+ allow_badges: true,
+ topic_featured_link_allowed: true,
+ search_priority: 0,
+ default_slow_mode_seconds: null,
+ uploaded_logo: null,
+ uploaded_logo_dark: null,
+ uploaded_background: null,
+ uploaded_background_dark: null,
+ required_tag_groups: [{ min_count: 1, name: "TagGroup1" }],
+ category_localizations: [],
},
},
"/c/2481/show.json": {
diff --git a/spec/system/new_category_spec.rb b/spec/system/new_category_spec.rb
index 53beaf0b2a0..b2a3e0a2e5b 100644
--- a/spec/system/new_category_spec.rb
+++ b/spec/system/new_category_spec.rb
@@ -17,7 +17,7 @@ describe "New Category", type: :system do
category_page.find(".edit-category-tab-tags #category-minimum-tags").click
category_page.save_settings
- try_until_success { expect(page).to have_current_path("/c/new-category/edit/general") }
+ expect(page).to have_current_path("/c/new-category/edit/general")
category_page.find(".edit-category-nav .edit-category-tags a").click
expect(category_page.find(".edit-category-tab-tags #category-minimum-tags").value).to eq("0")
diff --git a/spec/system/page_objects/pages/category.rb b/spec/system/page_objects/pages/category.rb
index 90d0a7201cc..6b982af1a5e 100644
--- a/spec/system/page_objects/pages/category.rb
+++ b/spec/system/page_objects/pages/category.rb
@@ -56,6 +56,7 @@ module PageObjects
def save_settings
find("#save-category").click
+ expect(page).to have_css("#save-category", text: I18n.t("js.category.save"))
self
end