Skip to content

Commit

Permalink
Try strings
Browse files Browse the repository at this point in the history
  • Loading branch information
APB9785 committed Sep 22, 2023
1 parent 8324828 commit e7db3fc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
39 changes: 19 additions & 20 deletions lib/beacon/loader/error_module_loader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,17 @@ 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
defmodule unquote(error_module) 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)

Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/beacon/loader/error_module_loader_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check failure on line 73 in test/beacon/loader/error_module_loader_test.exs

View workflow job for this annotation

GitHub Actions / test (OTP 23 | Elixir 1.13.0)

test user-defined component in layout (Beacon.Loader.ErrorModuleLoaderTest)

Check failure on line 73 in test/beacon/loader/error_module_loader_test.exs

View workflow job for this annotation

GitHub Actions / test (OTP 26 | Elixir 1.15.1)

test user-defined component in layout (Beacon.Loader.ErrorModuleLoaderTest)
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)

Check warning on line 75 in test/beacon/loader/error_module_loader_test.exs

View workflow job for this annotation

GitHub Actions / test (OTP 23 | Elixir 1.13.0)

variable "error_page" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 75 in test/beacon/loader/error_module_loader_test.exs

View workflow job for this annotation

GitHub Actions / test (OTP 26 | Elixir 1.15.1)

variable "error_page" is unused (if the variable is not meant to be used, prefix it with an underscore)
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

Check failure on line 81 in test/beacon/loader/error_module_loader_test.exs

View workflow job for this annotation

GitHub Actions / test (OTP 23 | Elixir 1.13.0)

test user-defined component in page (Beacon.Loader.ErrorModuleLoaderTest)

Check failure on line 81 in test/beacon/loader/error_module_loader_test.exs

View workflow job for this annotation

GitHub Actions / test (OTP 26 | Elixir 1.15.1)

test user-defined component in page (Beacon.Loader.ErrorModuleLoaderTest)
Expand Down

0 comments on commit e7db3fc

Please sign in to comment.