A platform for community discussion. Free, open, simple.
FIX: Regression in custom homepage modifier used in theme components (#27569)
Penar Musarajf4108702c8c9
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rbindex 001a7735139..219786ea9b7 100644--- a/app/controllers/application_controller.rb+++ b/app/controllers/application_controller.rb@@ -492,7 +492,7 @@ class ApplicationController < ActionController::Baseenddef current_homepage- current_user&.user_option&.homepage || HomepageHelper.resolve(@theme_id, current_user)+ current_user&.user_option&.homepage || HomepageHelper.resolve(request, current_user)enddef serialize_data(obj, serializer, opts = nil)diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rbindex 9817ac85a06..e0a0d22a1ef 100644--- a/app/helpers/application_helper.rb+++ b/app/helpers/application_helper.rb@@ -560,7 +560,7 @@ module ApplicationHelperenddef current_homepage- current_user&.user_option&.homepage || HomepageHelper.resolve(theme_id, current_user)+ current_user&.user_option&.homepage || HomepageHelper.resolve(request, current_user)enddef build_plugin_html(name)diff --git a/lib/homepage_constraint.rb b/lib/homepage_constraint.rbindex 3c6fbd2bd40..a80cfbd55f7 100644--- a/lib/homepage_constraint.rb+++ b/lib/homepage_constraint.rb@@ -9,11 +9,10 @@ class HomePageConstraintreturn @filter == "finish_installation" if SiteSetting.has_login_hint?current_user = CurrentUser.lookup_from_env(request.env)-# ensures we resolve the theme id as early as possible- theme_id = ThemeResolver.resolve_theme_id(request, Guardian.new(current_user), current_user)+ ThemeResolver.resolve_theme_id(request, Guardian.new(current_user), current_user)- homepage = current_user&.user_option&.homepage || HomepageHelper.resolve(theme_id, current_user)+ homepage = current_user&.user_option&.homepage || HomepageHelper.resolve(request, current_user)homepage == @filterrescue Discourse::InvalidAccess, Discourse::ReadOnlyfalsediff --git a/lib/homepage_helper.rb b/lib/homepage_helper.rbindex 9eb684508f8..c4a77a24482 100644--- a/lib/homepage_helper.rb+++ b/lib/homepage_helper.rb@@ -1,8 +1,8 @@# frozen_string_literal: trueclass HomepageHelper- def self.resolve(theme_id = nil, current_user = nil)- return "custom" if ThemeModifierHelper.new(theme_ids: theme_id).custom_homepage+ def self.resolve(request = nil, current_user = nil)+ return "custom" if ThemeModifierHelper.new(request: request).custom_homepagecurrent_user ? SiteSetting.homepage : SiteSetting.anonymous_homepageenddiff --git a/spec/system/homepage_spec.rb b/spec/system/homepage_spec.rbindex cd9004021f4..2be3c5d8033 100644--- a/spec/system/homepage_spec.rb+++ b/spec/system/homepage_spec.rb@@ -53,85 +53,85 @@ describe "Homepage", type: :system doexpect(page).to have_css(".navigation-container .categories.active", text: "Categories")end- context "when default theme uses a custom_homepage modifier" do- before do- theme.theme_modifier_set.custom_homepage = true- theme.theme_modifier_set.save!- theme.set_default!- end-- it "shows empty state to regular users" do- sign_in user+ shared_examples "a custom homepage" do+ it "shows the custom homepage component" dovisit "/"+ expect(page).to have_css(".new-home", text: "Hi friends!")expect(page).to have_no_css(".list-container")- expect(page).to have_no_css(".alert-info")- end- it "shows empty state and notice to admins" do- sign_in admin- visit "/"+ find("#sidebar-section-content-community li:first-child").click+ expect(page).to have_css(".list-container")++ find("#site-logo").clickexpect(page).to have_no_css(".list-container")- expect(page).to have_css(".alert-info")+ # ensure clicking on logo brings user back to the custom homepage+ expect(page).to have_css(".new-home", text: "Hi friends!")end- shared_examples "a custom homepage" do- it "shows the custom homepage component" do- visit "/"+ it "respects the user's homepage choice" do+ visit "/"- expect(page).to have_css(".new-home", text: "Hi friends!")- expect(page).to have_no_css(".list-container")+ expect(page).not_to have_css(".list-container")+ expect(page).to have_css(".new-home", text: "Hi friends!")- find("#sidebar-section-content-community li:first-child").click- expect(page).to have_css(".list-container")+ sign_in user- find("#site-logo").click+ visit "/u/#{user.username}/preferences/interface"- expect(page).to have_no_css(".list-container")- # ensure clicking on logo brings user back to the custom homepage- expect(page).to have_css(".new-home", text: "Hi friends!")- end+ homepage_picker = PageObjects::Components::SelectKit.new("#home-selector")+ homepage_picker.expand+ # user overrides theme custom homepage+ homepage_picker.select_row_by_name("Top")+ page.find(".btn-primary.save-changes").click- it "respects the user's homepage choice" do- visit "/"+ # Wait for the save to complete+ find(".btn-primary.save-changes:not([disabled])", wait: 5)- expect(page).not_to have_css(".list-container")- expect(page).to have_css(".new-home", text: "Hi friends!")+ find("#site-logo").click- sign_in user+ expect(page).to have_css(".navigation-container .top.active", text: "Top")+ expect(page).to have_css(".top-lists")- visit "/u/#{user.username}/preferences/interface"+ visit "/u/#{user.username}/preferences/interface"- homepage_picker = PageObjects::Components::SelectKit.new("#home-selector")- homepage_picker.expand- # user overrides theme custom homepage- homepage_picker.select_row_by_name("Top")- page.find(".btn-primary.save-changes").click+ homepage_picker = PageObjects::Components::SelectKit.new("#home-selector")+ homepage_picker.expand+ # user selects theme custom homepage again+ homepage_picker.select_row_by_name("(default)")+ page.find(".btn-primary.save-changes").click- # Wait for the save to complete- find(".btn-primary.save-changes:not([disabled])", wait: 5)+ # Wait for the save to complete+ find(".btn-primary.save-changes:not([disabled])", wait: 5)+ find("#site-logo").click- find("#site-logo").click+ expect(page).not_to have_css(".list-container")+ expect(page).to have_css(".new-home", text: "Hi friends!")+ end+ end- expect(page).to have_css(".navigation-container .top.active", text: "Top")- expect(page).to have_css(".top-lists")+ context "when default theme uses a custom_homepage modifier" do+ before do+ theme.theme_modifier_set.custom_homepage = true+ theme.theme_modifier_set.save!+ theme.set_default!+ end- visit "/u/#{user.username}/preferences/interface"+ it "shows empty state to regular users" do+ sign_in user+ visit "/"- homepage_picker = PageObjects::Components::SelectKit.new("#home-selector")- homepage_picker.expand- # user selects theme custom homepage again- homepage_picker.select_row_by_name("(default)")- page.find(".btn-primary.save-changes").click+ expect(page).to have_no_css(".list-container")+ expect(page).to have_no_css(".alert-info")+ end- # Wait for the save to complete- find(".btn-primary.save-changes:not([disabled])", wait: 5)- find("#site-logo").click+ it "shows empty state and notice to admins" do+ sign_in admin+ visit "/"- expect(page).not_to have_css(".list-container")- expect(page).to have_css(".new-home", text: "Hi friends!")- end+ expect(page).to have_no_css(".list-container")+ expect(page).to have_css(".alert-info")endcontext "when the theme adds content to the [custom-homepage] connector" do@@ -175,4 +175,31 @@ describe "Homepage", type: :system doinclude_examples "a custom homepage"endend++ context "when a theme component uses the custom_homepage modifier" do+ let!(:component) { Fabricate(:theme, component: true) }+ let!(:component_html_field) do+ Fabricate(+ :theme_field,+ theme: component,+ type_id: ThemeField.types[:html],+ target_id: Theme.targets[:common],+ name: "head_tag",+ value: <<~HTML,+ <script type="text/x-handlebars" data-template-name="/connectors/custom-homepage/new-home">+ <div class="new-home">Hi friends!</div>+ </script>+ HTML+ )+ end++ before do+ component.theme_modifier_set.custom_homepage = true+ component.theme_modifier_set.save!+ theme.add_relative_theme!(:child, component)+ theme.set_default!+ end++ include_examples "a custom homepage"+ endend