From e7db3fc700fc6a822c74f5da1d0b4107c1c3305a Mon Sep 17 00:00:00 2001 From: APB9785 <74077809+APB9785@users.noreply.github.com> Date: Fri, 22 Sep 2023 18:19:48 -0500 Subject: [PATCH] Try strings --- lib/beacon/loader/error_module_loader.ex | 39 +++++++++---------- .../loader/error_module_loader_test.exs | 4 +- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/lib/beacon/loader/error_module_loader.ex b/lib/beacon/loader/error_module_loader.ex index 9770d851..e8f9ca72 100644 --- a/lib/beacon/loader/error_module_loader.ex +++ b/lib/beacon/loader/error_module_loader.ex @@ -7,8 +7,8 @@ defmodule Beacon.Loader.ErrorModuleLoader do def load_error_pages!(error_pages, site) do error_module = Loader.error_module_for_site(site) component_module = Loader.component_module_for_site(site) - layout_functions = Enum.map(error_pages, &build_layout_fn/1) - render_functions = Enum.map(error_pages, &build_render_fn(&1, error_module)) + # layout_functions = Enum.map(error_pages, &build_layout_fn/1) + render_functions = Enum.map(error_pages, &build_render_fn(&1, component_module)) ast = quote do @@ -16,12 +16,8 @@ defmodule Beacon.Loader.ErrorModuleLoader do use Phoenix.HTML require EEx import Phoenix.Component - unquote(Loader.maybe_import_my_component(component_module, render_functions ++ layout_functions)) require Logger - # One function per error page - unquote_splicing(layout_functions) - # One function per error page unquote_splicing(render_functions) @@ -46,25 +42,28 @@ defmodule Beacon.Loader.ErrorModuleLoader do {:ok, error_module, ast} end - defp build_layout_fn(%ErrorPage{} = error_page) do - %{site: site, layout: %{id: layout_id}, status: status} = error_page - %{template: template} = Content.get_published_layout(site, layout_id) - compiled = EEx.compile_string(template, engine: Phoenix.HTML.Engine) + # defp build_layout_fn(%ErrorPage{} = error_page) do + # %{site: site, layout: %{id: layout_id}, status: status} = error_page + # %{template: template} = Content.get_published_layout(site, layout_id) + # compiled = EEx.compile_string(template, engine: Phoenix.HTML.Engine) - quote do - def layout(unquote(status), var!(assigns)) when is_map(var!(assigns)) do - unquote(compiled) - end - end - end + # quote do + # def layout(unquote(status), var!(assigns)) when is_map(var!(assigns)) do + # unquote(compiled) + # end + # end + # end + + defp build_render_fn(%ErrorPage{} = error_page, component_module) do + %{site: site, template: template, status: status, layout: %{id: layout_id}} = error_page + %{template: layout_template} = Content.get_published_layout(site, layout_id) - defp build_render_fn(%ErrorPage{} = error_page, error_module) do - %{template: template, status: status} = error_page + full_template = "<% import :\"#{component_module}\" %>\n#{String.replace(layout_template, "<%= @inner_content %>", template)}" quote do def render(var!(conn), unquote(status)) do - var!(assigns) = %{conn: var!(conn), inner_content: unquote(error_module).layout(unquote(status), %{inner_content: unquote(template)})} - unquote(error_module).root_layout(var!(assigns)) + %{} = var!(conn) + unquote(full_template) end end end diff --git a/test/beacon/loader/error_module_loader_test.exs b/test/beacon/loader/error_module_loader_test.exs index 7cc97ef6..1d13461b 100644 --- a/test/beacon/loader/error_module_loader_test.exs +++ b/test/beacon/loader/error_module_loader_test.exs @@ -70,12 +70,12 @@ defmodule Beacon.Loader.ErrorModuleLoaderTest do assert error_module.layout(501, %{inner_content: error_page.template}) == {:safe, ["#custom_layout#", "error_501"]} end - test "user-defined component in layout" do + test "user-defined component in layout", %{conn: conn} do layout = published_layout_fixture(template: ~S|<%= my_component("sample_component", val: 1 )%><%= @inner_content %>|, site: @site) error_page = error_page_fixture(layout: layout, template: "error", status: 502, site: @site) error_module = load_error_pages_module(@site) - assert error_module.layout(502, %{inner_content: error_page.template}) == {:safe, ["1", "error"]} + assert error_module.render(conn, 502) == {:safe, ["1", "error"]} end test "user-defined component in page" do