DEV: Load theme objects typed setting metadata when routing to editor (#26354)

Why this change?

Previously, we were preloading the necessary metadata for
`adminCustomizeThemes.show.schema` route in the
`adminCustomizeThemes.show` route. This is wasteful because we're
loading data upfront when the objects setting editor may not be used.

This change also lays the ground work for a future commit where we need
to be shipping down additional metadata which may further add to the
payload.
This commit is contained in:
Alan Guo Xiang Tan
2024-03-26 14:02:05 +08:00
committed by GitHub
parent fbde7f8bc1
commit ef99b97ea7
13 changed files with 186 additions and 68 deletions

View File

@@ -1313,4 +1313,83 @@ RSpec.describe Admin::ThemesController do
delete "/admin/themes/bulk_destroy.json", params: { theme_ids: theme_ids }
end
end
describe "#objects_setting_metadata" do
fab!(:theme)
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
end
before { SiteSetting.experimental_objects_type_for_theme_settings = true }
it "returns 404 if user is not an admin" do
get "/admin/themes/#{theme.id}/objects_setting_metadata/objects_with_categories.json"
expect(response.status).to eq(404)
sign_in(user)
get "/admin/themes/#{theme.id}/objects_setting_metadata/objects_with_categories.json"
expect(response.status).to eq(404)
sign_in(moderator)
get "/admin/themes/#{theme.id}/objects_setting_metadata/objects_with_categories.json"
expect(response.status).to eq(404)
end
context "when user is an admin" do
before { sign_in(admin) }
it "returns 403 if `experimental_objects_type_for_theme_settings` site setting is not enabled" do
SiteSetting.experimental_objects_type_for_theme_settings = false
get "/admin/themes/#{theme.id}/objects_setting_metadata/objects_with_categories.json"
expect(response.status).to eq(403)
end
it "returns 400 if the `id` param is not the id of a valid theme" do
get "/admin/themes/some_invalid_id/objects_setting_metadata/objects_with_categories.json"
expect(response.status).to eq(400)
end
it "returns 400 if the `setting_name` param does not match a valid setting" do
get "/admin/themes/#{theme.id}/objects_setting_metadata/some_invalid_setting_name.json"
expect(response.status).to eq(400)
end
it "returns 200 with the right `property_descriptions` attributes" do
theme.set_field(
target: :translations,
name: "en",
value: File.read("#{Rails.root}/spec/fixtures/theme_locales/objects_settings/en.yaml"),
)
theme.save!
theme_setting
get "/admin/themes/#{theme.id}/objects_setting_metadata/objects_setting.json"
expect(response.status).to eq(200)
expect(response.parsed_body["property_descriptions"]).to eq(
{
"links.name" => "Name of the link",
"links.url" => "URL of the link",
"name" => "Section Name",
},
)
end
end
end
end

View File

@@ -0,0 +1,40 @@
# frozen_string_literal: true
RSpec.describe ThemeObjectsSettingMetadataSerializer do
fab!(:theme)
let(:theme_setting) do
yaml = File.read("#{Rails.root}/spec/fixtures/theme_settings/objects_settings.yaml")
theme.set_field(target: :settings, name: "yaml", value: yaml)
theme.save!
theme.settings
end
before { SiteSetting.experimental_objects_type_for_theme_settings = true }
describe "#property_descriptions" do
let(:objects_setting_locale) do
theme.set_field(
target: :translations,
name: "en",
value: File.read("#{Rails.root}/spec/fixtures/theme_locales/objects_settings/en.yaml"),
)
theme.save!
end
it "should return a hash of the settings property descriptions" do
objects_setting_locale
payload = described_class.new(theme_setting[:objects_setting], root: false).as_json
expect(payload[:property_descriptions]).to eq(
{
"links.name" => "Name of the link",
"links.url" => "URL of the link",
"name" => "Section Name",
},
)
end
end
end

View File

@@ -19,43 +19,4 @@ RSpec.describe ThemeSettingsSerializer do
expect(payload[:theme_settings][:objects_schema][:name]).to eq("section")
end
end
describe "#objects_schema_property_descriptions" do
let(:objects_setting_locale) do
theme.set_field(
target: :translations,
name: "en",
value: File.read("#{Rails.root}/spec/fixtures/theme_locales/objects_settings/en.yaml"),
)
theme.save!
end
before { SiteSetting.experimental_objects_type_for_theme_settings = true }
it "should not include the attribute when theme setting is not typed objects" do
yaml = File.read("#{Rails.root}/spec/fixtures/theme_settings/valid_settings.yaml")
theme.set_field(target: :settings, name: "yaml", value: yaml)
theme.save!
payload = ThemeSettingsSerializer.new(theme.settings[:string_setting]).as_json
expect(payload[:theme_settings][:objects_schema_property_descriptions]).to be_nil
end
it "should include the attribute when theme setting is of typed objects" do
objects_setting_locale
objects_setting
payload = ThemeSettingsSerializer.new(objects_setting).as_json
expect(payload[:theme_settings][:objects_schema_property_descriptions]).to eq(
{
"links.name" => "Name of the link",
"links.url" => "URL of the link",
"name" => "Section Name",
},
)
end
end
end

View File

@@ -78,12 +78,19 @@ RSpec.describe "Admin editing objects type theme setting", type: :system do
expect(admin_customize_themes_page).to have_overridden_setting("objects_setting")
admin_objects_theme_setting_editor =
admin_customize_themes_page.click_edit_objects_theme_setting_button("objects_setting")
expect(admin_objects_theme_setting_editor).to have_setting_field("name", "some new name")
admin_objects_theme_setting_editor.back
admin_customize_themes_page.reset_overridden_setting("objects_setting")
admin_objects_theme_setting_editor =
admin_customize_themes_page.click_edit_objects_theme_setting_button("objects_setting")
expect(admin_objects_theme_setting_editor).to have_setting_field("name", "some new name")
expect(admin_objects_theme_setting_editor).to have_setting_field("name", "section 1")
end
it "allows an admin to edit a theme setting of objects type via the settings editor" do

View File

@@ -35,6 +35,11 @@ module PageObjects
self
end
def back
find(".customize-themes-show-schema__back").click
self
end
private
def input_field(field_name)