A platform for community discussion. Free, open, simple.
FIX: Pass values of objects typed settings to theme migrations (#26751)
Osama Sayegh2215fa0c8e7c
diff --git a/lib/theme_settings_manager.rb b/lib/theme_settings_manager.rbindex 300c026ff99..7fe1fd47073 100644--- a/lib/theme_settings_manager.rb+++ b/lib/theme_settings_manager.rb@@ -10,7 +10,7 @@ class ThemeSettingsManagerdef self.cast_row_value(row)type_name = self.types.invert[row.data_type].downcase.capitalizeklass = "ThemeSettingsManager::#{type_name}".constantize- klass.cast(row.value)+ klass.cast(klass.extract_value_from_row(row))enddef self.create(name, default, type, theme, opts = {})@@ -23,6 +23,10 @@ class ThemeSettingsManagervalueend+ def self.extract_value_from_row(row)+ row.value+ end+def initialize(name, default, theme, opts = {})@name = name.to_sym@default = defaultdiff --git a/lib/theme_settings_manager/objects.rb b/lib/theme_settings_manager/objects.rbindex 70a15d7260c..47874373259 100644--- a/lib/theme_settings_manager/objects.rb+++ b/lib/theme_settings_manager/objects.rb@@ -1,6 +1,10 @@# frozen_string_literal: trueclass ThemeSettingsManager::Objects < ThemeSettingsManager+ def self.extract_value_from_row(row)+ row.json_value+ end+def valuehas_record? ? db_record.json_value : default.map!(&:deep_stringify_keys)enddiff --git a/spec/services/theme_settings_migrations_runner_spec.rb b/spec/services/theme_settings_migrations_runner_spec.rbindex 2b1e760e003..a986e785a28 100644--- a/spec/services/theme_settings_migrations_runner_spec.rb+++ b/spec/services/theme_settings_migrations_runner_spec.rb@@ -36,6 +36,60 @@ describe ThemeSettingsMigrationsRunner do)end+ it "passes values of `objects` typed settings to migrations and the values are parsed (not json string)" do+ settings_field.update!(value: <<~YAML)+ objects_setting:+ type: objects+ default:+ - text: "hello, default link"+ url: "https://google.com"+ - text: "hi, another default link"+ url: "https://discourse.org"+ schema:+ name: link+ properties:+ text:+ type: string+ url:+ type: string+ YAML+ theme.update_setting(+ :objects_setting,+ [{ text: "custom link 1", url: "https://meta.discourse.org" }],+ )+ theme.save!++ migration_field.update!(value: <<~JS)+ export default function migrate(settings) {+ settings.get("objects_setting").push(+ {+ text: "another custom link",+ url: "https://try.discourse.org"+ }+ )+ return settings;+ }+ JS++ results = described_class.new(theme).run++ expect(results.first[:settings_before]).to eq(+ {+ "objects_setting" => [+ { "url" => "https://meta.discourse.org", "text" => "custom link 1" },+ ],+ },+ )+ expect(results.first[:settings_after]).to eq(+ {+ "objects_setting" => [+ { "url" => "https://meta.discourse.org", "text" => "custom link 1" },+ { "url" => "https://try.discourse.org", "text" => "another custom link" },+ ],+ },+ )+ end+it "passes the output of the previous migration as input to the next one" dotheme.update_setting(:integer_setting, 1)