Skip to content

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

DEV: Add category helper for theme migration (#38816)

Natalie Tayd4e27688a2d2
diff --git a/app/services/theme_settings_migrations_runner.rb b/app/services/theme_settings_migrations_runner.rb
index 6b5fb455f50..e52e1c1ce0f 100644
--- a/app/services/theme_settings_migrations_runner.rb
+++ b/app/services/theme_settings_migrations_runner.rb
@@ -16,6 +16,13 @@ class ThemeSettingsMigrationsRunner
Category.where("name_lower = LOWER(?)", category_name).pick(:id)
end
+ # @param [String] Slug of the category to retrieve the id of.
+ # @return [Integer|nil] The id of the category with the given slug or nil if a category does not exist for the given
+ # slug.
+ def get_category_id_by_slug(category_slug)
+ Category.where(slug: category_slug).pick(:id)
+ end
+
# @param [String] URL string to check if it is a valid absolute URL, path or anchor.
# @return [Boolean] True if the URL is a valid URL or path, false otherwise.
def is_valid_url(url)
diff --git a/spec/services/theme_settings_migrations_runner_spec.rb b/spec/services/theme_settings_migrations_runner_spec.rb
index 7053650c548..20a9d4943c2 100644
--- a/spec/services/theme_settings_migrations_runner_spec.rb
+++ b/spec/services/theme_settings_migrations_runner_spec.rb
@@ -373,5 +373,24 @@ describe ThemeSettingsMigrationsRunner do
expect(results[0][:settings_after]).to eq({ "integer_setting" => category.id })
end
+
+ it "attaches the getCategoryIdBySlug() function to the context of the migrations" do
+ category = Fabricate(:category, slug: "some-category")
+
+ theme.update_setting(:integer_setting, -10)
+ theme.save!
+
+ migration_field.update!(value: <<~JS)
+ export default function migrate(settings, helpers) {
+ const categoryId = helpers.getCategoryIdBySlug("some-category");
+ settings.set("integer_setting", categoryId);
+ return settings;
+ }
+ JS
+
+ results = described_class.new(theme).run
+
+ expect(results[0][:settings_after]).to eq({ "integer_setting" => category.id })
+ end
end
end