DEV: Change category type to categories type for theme object schema (#26339)

Why this change?

This is a follow-up to 86b2e3aa3e8be30a308f1bff3664d76c5d56057a.

Basically, we want to allow people to select more than 1 category as well.

What does this change do?

1. Change `type: category` to `type: categories` and support `min` and `max`
   validations for `type: categories`.

2. Fix the `<SchemaThemeSetting::Types::Categories>` component to support the
   `min` and `max` validations and switch it to use the `<CategorySelector>` component
   instead of the `<CategoryChooser>` component which only supports selecting one category.
This commit is contained in:
Alan Guo Xiang Tan
2024-03-27 10:54:30 +08:00
committed by GitHub
parent 0df50a7e5d
commit 476d91d233
19 changed files with 387 additions and 100 deletions

View File

@ -3,11 +3,11 @@
RSpec.describe ThemeSettingsManager::Objects do
fab!(:theme)
let(:objects_setting) do
let(:theme_setting) do
yaml = File.read("#{Rails.root}/spec/fixtures/theme_settings/objects_settings.yaml")
field = theme.set_field(target: :settings, name: "yaml", value: yaml)
theme.save!
theme.settings[:objects_setting]
theme.settings
end
before { SiteSetting.experimental_objects_type_for_theme_settings = true }
@ -27,7 +27,7 @@ RSpec.describe ThemeSettingsManager::Objects do
},
]
objects_setting.value = new_value
theme_setting[:objects_setting].value = new_value
expect(theme.reload.settings[:objects_setting].value).to eq(new_value)
end
@ -45,9 +45,42 @@ RSpec.describe ThemeSettingsManager::Objects do
},
]
expect { objects_setting.value = new_value }.to raise_error(
expect { theme_setting[:objects_setting].value = new_value }.to raise_error(
Discourse::InvalidParameters,
"The property at JSON Pointer '/0/links/0/name' must be present. The property at JSON Pointer '/1/name' must be present. The property at JSON Pointer '/1/links/0/name' must be at most 20 characters long.",
)
end
describe "#categories" do
fab!(:category_1) { Fabricate(:category) }
fab!(:category_2) { Fabricate(:category) }
fab!(:category_3) { Fabricate(:private_category, group: Fabricate(:group)) }
fab!(:admin)
it "returns an empty array when there are no properties of `categories` type" do
expect(theme_setting[:objects_setting].categories(Guardian.new)).to eq([])
end
it "returns the categories record for all the properties of `categories` type in a flat array" do
new_value = [
{
"category_ids" => [category_1.id, category_2.id],
"child_categories" => [{ "category_ids" => [category_3.id] }],
},
]
theme_setting[:objects_with_categories].value = new_value
expect(theme.reload.settings[:objects_with_categories].value).to eq(new_value)
expect(theme.settings[:objects_with_categories].categories(Guardian.new)).to contain_exactly(
category_1,
category_2,
)
expect(
theme.settings[:objects_with_categories].categories(Guardian.new(admin)),
).to contain_exactly(category_1, category_2, category_3)
end
end
end