FIX: add theme field errors (#12880)

FIX: add theme field errors (#12880)

  • FIX: add theme field errors

Expose theme field errors on theme pages Previously these errors were not being displayed on themes.

diff --git a/app/serializers/theme_serializer.rb b/app/serializers/theme_serializer.rb
index e388f6f..b0edeaa 100644
--- a/app/serializers/theme_serializer.rb
+++ b/app/serializers/theme_serializer.rb
@@ -79,6 +79,10 @@ class ThemeSerializer < BasicThemeSerializer
   def initialize(theme, options = {})
     super
     @errors = []
+
+    object.theme_fields.each do |o|
+      @errors << o.error if o.error
+    end
   end
 
   def child_themes
diff --git a/spec/fabricators/theme_field_fabricator.rb b/spec/fabricators/theme_field_fabricator.rb
new file mode 100644
index 0000000..688cfc5
--- /dev/null
+++ b/spec/fabricators/theme_field_fabricator.rb
@@ -0,0 +1,9 @@
+# frozen_string_literal: true
+
+Fabricator(:theme_field) do
+  theme
+  target_id { 0 }
+  name { sequence(:name) { |i| "scss_#{i + 1}" } }
+  value { ".test {color: blue;}" }
+  error { nil }
+end
diff --git a/spec/serializers/theme_serializer_spec.rb b/spec/serializers/theme_serializer_spec.rb
index 78fa138..69557c2 100644
--- a/spec/serializers/theme_serializer_spec.rb
+++ b/spec/serializers/theme_serializer_spec.rb
@@ -4,7 +4,7 @@ require 'rails_helper'
 
 RSpec.describe ThemeSerializer do
   describe "load theme settings" do
-    it 'should add error message when having invalid format' do
+    it 'should add error message when settings format is invalid' do
       theme = Fabricate(:theme)
       Theme.any_instance.stubs(:settings).raises(ThemeSettingsParser::InvalidYaml, I18n.t("themes.settings_errors.invalid_yaml"))
       serialized = ThemeSerializer.new(theme).as_json[:theme]
@@ -12,5 +12,14 @@ RSpec.describe ThemeSerializer do
       expect(serialized[:errors].count).to eq(1)
       expect(serialized[:errors][0]).to eq(I18n.t("themes.settings_errors.invalid_yaml"))
     end
+
+    it 'should add errors messages from theme fields' do
+      error = "error when compiling theme field"
+      theme = Fabricate(:theme)
+      theme_field = Fabricate(:theme_field, error: error, theme: theme)
+      serialized = ThemeSerializer.new(theme.reload).as_json[:theme]
+      expect(serialized[:errors].count).to eq(1)
+      expect(serialized[:errors][0]).to eq(error)
+    end
   end
 end

GitHub sha: e2521801

This commit appears in #12880 which was approved by pmusaraj. It was merged by featheredtoast.