From 8ec7b629dea0285243c38ef964eed9d0a0dbf7db Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Tue, 3 Sep 2024 19:01:34 +0200 Subject: [PATCH] Fallback to @page var if no Current.page is set With Alchemy 7.2 (#2701) we started to use `Alchemy::Current.page` for rendering elements of current page. If one has a custom controller to render Alchemy content with: render(template: "alchemy/pages/show") it is now empty if `Alchemy::Current.page` is not set. Someday we might deprecate the `@page` instance variable, but until then we need to fall back to it. (cherry picked from commit 26072abb5211863289c62237c8da57bb03e8b63f) --- app/helpers/alchemy/elements_helper.rb | 2 +- spec/helpers/alchemy/elements_helper_spec.rb | 27 +++++++++++++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/app/helpers/alchemy/elements_helper.rb b/app/helpers/alchemy/elements_helper.rb index c840620e50..6ffad5e9d6 100644 --- a/app/helpers/alchemy/elements_helper.rb +++ b/app/helpers/alchemy/elements_helper.rb @@ -72,7 +72,7 @@ module ElementsHelper # def render_elements(options = {}, &blk) options = { - from_page: Current.page, + from_page: Current.page || @page, render_format: "html" }.update(options) diff --git a/spec/helpers/alchemy/elements_helper_spec.rb b/spec/helpers/alchemy/elements_helper_spec.rb index 127b951e32..48f6f6277b 100644 --- a/spec/helpers/alchemy/elements_helper_spec.rb +++ b/spec/helpers/alchemy/elements_helper_spec.rb @@ -75,9 +75,30 @@ module Alchemy context "without any options" do let(:options) { {} } - it "should render all elements from current pages public version." do - is_expected.to have_selector("##{element.dom_id}") - is_expected.to have_selector("##{another_element.dom_id}") + context "with Current.page set" do + before { Current.page = page } + + it "should render all elements from current pages public version." do + is_expected.to have_selector("##{element.dom_id}") + is_expected.to have_selector("##{another_element.dom_id}") + end + end + + context "with Current.page not set" do + before { Current.page = nil } + + it "should not render any elements." do + is_expected.to be_empty + end + + context "but with @page set" do + before { helper.instance_variable_set(:@page, page) } + + it "should render all elements from current pages public version." do + is_expected.to have_selector("##{element.dom_id}") + is_expected.to have_selector("##{another_element.dom_id}") + end + end end context "in preview_mode" do