From 5ae238e5da9b87e4a3467f48dac394697f141d88 Mon Sep 17 00:00:00 2001 From: merefield Date: Wed, 13 Jul 2022 21:03:34 +0100 Subject: [PATCH 1/3] FIX: prevent site launch failure when user's locale is nil --- plugin.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin.rb b/plugin.rb index 4932c88..71c7ef8 100644 --- a/plugin.rb +++ b/plugin.rb @@ -228,7 +228,7 @@ add_to_serializer(:site, :categories) do object.categories.map do |c| c[:name] = c[:slug] == "uncategorized" ? I18n.t('uncategorized_category_name', locale: SiteSetting.default_locale) : - ((scope && scope.current_user && c[:slug_path] ? Multilingual::Translation.get("category_name", c[:slug_path])[scope.current_user.locale.to_sym] : + ((scope && scope.current_user && scope.current_user.locale && c[:slug_path] ? Multilingual::Translation.get("category_name", c[:slug_path])[scope.current_user.locale.to_sym] : c[:name]) || c[:name]) c.to_h end From e3e7bc2a57c5abca942bf80593fda8ed830700a7 Mon Sep 17 00:00:00 2001 From: merefield Date: Thu, 14 Jul 2022 08:00:07 +0100 Subject: [PATCH 2/3] extend nil class protection to other serialized objects --- plugin.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/plugin.rb b/plugin.rb index 71c7ef8..b768e15 100644 --- a/plugin.rb +++ b/plugin.rb @@ -203,25 +203,25 @@ add_to_serializer(:basic_category, :name) do object.uncategorized? ? I18n.t('uncategorized_category_name', locale: SiteSetting.default_locale) : - ((scope && scope.current_user ? Multilingual::Translation.get("category_name", object.slug_path)[scope.current_user.locale.to_sym] : + ((scope && scope.current_user && scope.current_user.locale && object.slug_path ? Multilingual::Translation.get("category_name", object.slug_path)[scope.current_user.locale.to_sym] : object.name) || object.name) end add_to_serializer(:basic_category, :description_text) do object.uncategorized? ? I18n.t('category.uncategorized_description', locale: SiteSetting.default_locale) : - ((scope && scope.current_user ? Multilingual::Translation.get("category_description", object.slug_path)[scope.current_user.locale.to_sym] : + ((scope && scope.current_user && scope.current_user.locale && object.slug_path ? Multilingual::Translation.get("category_description", object.slug_path)[scope.current_user.locale.to_sym] : object.description_text) || object.description_text) end add_to_serializer(:basic_category, :description) do object.uncategorized? ? I18n.t('category.uncategorized_description', locale: SiteSetting.default_locale) : - ((scope && scope.current_user ? Multilingual::Translation.get("category_description", object.slug_path)[scope.current_user.locale.to_sym] : + ((scope && scope.current_user && scope.current_user.locale && object.slug_path ? Multilingual::Translation.get("category_description", object.slug_path)[scope.current_user.locale.to_sym] : object.description) || object.description) end add_to_serializer(:basic_category, :description_excerpt) do object.uncategorized? ? I18n.t('category.uncategorized_description', locale: SiteSetting.default_locale) : - ((scope && scope.current_user ? Multilingual::Translation.get("category_description", object.slug_path)[scope.current_user.locale.to_sym] : + ((scope && scope.current_user && scope.current_user.locale && object.slug_path ? Multilingual::Translation.get("category_description", object.slug_path)[scope.current_user.locale.to_sym] : object.description_excerpt) || object.description_excerpt) end From c4ede37416f4c4ff0d1bc64ce1b034ec7f57adee Mon Sep 17 00:00:00 2001 From: Robert Barrow Date: Thu, 14 Jul 2022 16:50:39 +0100 Subject: [PATCH 3/3] Add site serializer tests --- spec/serializers/site_serializer_spec.rb | 41 ++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 spec/serializers/site_serializer_spec.rb diff --git a/spec/serializers/site_serializer_spec.rb b/spec/serializers/site_serializer_spec.rb new file mode 100644 index 0000000..0c37085 --- /dev/null +++ b/spec/serializers/site_serializer_spec.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +describe SiteSerializer do + fab!(:user) { Fabricate(:user, admin: false) } + let(:guardian) { Guardian.new(user) } + let(:category) { Fabricate(:category) } + + after do + Site.clear_cache + end + + it "serializes names as translations" do + Multilingual::CustomTranslation.create( + file_name: "category_name.wbp.yml", + file_type: "category_name", + locale: "wbp", + file_ext: "yml", + translation_data: { category.slug => "pardu-pardu-mani" } + ) + user.locale = "wbp" + serialized = described_class.new(Site.new(guardian), scope: guardian, root: false).as_json + c1 = serialized[:categories].find { |c| c[:id] == category.id } + + expect(c1[:name]).to eq("pardu-pardu-mani") + end + + it "doesn't explode when user locale is nil" do + Multilingual::CustomTranslation.create( + file_name: "category_name.wbp.yml", + file_type: "category_name", + locale: "wbp", + file_ext: "yml", + translation_data: { category.slug => "pardu-pardu-mani" } + ) + user.locale = nil + serialized = described_class.new(Site.new(guardian), scope: guardian, root: false).as_json + c1 = serialized[:categories].find { |c| c[:id] == category.id } + + expect(c1[:name]).to eq(category.name) + end +end