From e43b639f8d36018323ae4348458ca402549bff67 Mon Sep 17 00:00:00 2001 From: Leandro Pereira Date: Mon, 8 Jan 2024 14:56:14 -0300 Subject: [PATCH] New Page - include default meta tags (#390) Implemented at https://github.com/BeaconCMS/beacon/commit/62094e85ee59db598909b3df37a80e7d4ec82684 but the code was incorrectly removed during the Context API refactor --- lib/beacon/content.ex | 12 ++++++++++-- test/beacon/content_test.exs | 26 +++++++++++++++++--------- test/test_helper.exs | 4 +--- 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/lib/beacon/content.ex b/lib/beacon/content.ex index c5fb55ad..a8132571 100644 --- a/lib/beacon/content.ex +++ b/lib/beacon/content.ex @@ -468,10 +468,13 @@ defmodule Beacon.Content do @doc type: :pages @spec create_page(map()) :: {:ok, Page.t()} | {:error, Changeset.t()} def create_page(attrs) when is_map(attrs) do - changeset = Page.create_changeset(%Page{}, attrs) + site = attrs["site"] || attrs[:site] Repo.transact(fn -> - with {:ok, changeset} <- validate_page_template(changeset), + with {:ok, site} <- Beacon.Types.Site.cast(site), + attrs = put_default_meta_tags(site, attrs), + changeset = Page.create_changeset(%Page{}, attrs), + {:ok, changeset} <- validate_page_template(changeset), {:ok, page} <- Repo.insert(changeset), {:ok, _event} <- create_page_event(page, "created"), %Page{} = page <- Lifecycle.Page.after_create_page(page) do @@ -480,6 +483,11 @@ defmodule Beacon.Content do end) end + defp put_default_meta_tags(site, attrs) do + default_meta_tags = Beacon.Config.fetch!(site).default_meta_tags + Map.put_new(attrs, :meta_tags, default_meta_tags) + end + @doc """ Creates a page. """ diff --git a/test/beacon/content_test.exs b/test/beacon/content_test.exs index 30feb83a..ecd0c563 100644 --- a/test/beacon/content_test.exs +++ b/test/beacon/content_test.exs @@ -75,7 +75,7 @@ defmodule Beacon.ContentTest do test "validate body heex on create" do assert {:error, %Ecto.Changeset{errors: [template: {"invalid", [compilation_error: compilation_error]}]}} = - Content.create_layout(%{site: :test, title: "test", template: "`" end @@ -95,7 +95,7 @@ defmodule Beacon.ContentTest do layout = layout_fixture() assert {:error, %Ecto.Changeset{errors: [template: {"invalid", [compilation_error: compilation_error]}]}} = - Content.create_page(%{site: :test, path: "/", layout_id: layout.id, template: "`" end @@ -111,30 +111,38 @@ defmodule Beacon.ContentTest do # TODO: require paths starting with / which will make this test fail test "create page with empty path" do - layout = layout_fixture() - assert {:ok, %Page{path: ""}} = Content.create_page(%{ site: "my_site", path: "", template: "

page

", - layout_id: layout.id + layout_id: layout_fixture().id }) end test "create page should create a created event" do - layout = layout_fixture() - Content.create_page!(%{ site: "my_site", path: "/", template: "

page

", - layout_id: layout.id + layout_id: layout_fixture().id }) assert %PageEvent{event: :created} = Repo.one(PageEvent) end + test "create page includes default meta tags" do + page = + Content.create_page!(%{ + site: "default_meta_tags_test", + path: "/", + template: "

page

", + layout_id: layout_fixture().id + }) + + assert page.meta_tags == [%{"name" => "foo", "content" => "bar"}] + end + test "update page should validate invalid templates" do page = page_fixture() @@ -521,7 +529,7 @@ defmodule Beacon.ContentTest do describe "components" do test "validate template heex on create" do assert {:error, %Ecto.Changeset{errors: [body: {"invalid", [compilation_error: compilation_error]}]}} = - Content.create_component(%{site: :test, name: "test", body: "`" end diff --git a/test/test_helper.exs b/test/test_helper.exs index bb5b677e..8e0074f1 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -39,9 +39,7 @@ Supervisor.start_link( endpoint: Beacon.BeaconTest.Endpoint, data_source: Beacon.BeaconTest.BeaconDataSource, default_meta_tags: [ - %{"name" => "foo_meta_tag"}, - %{"name" => "bar_meta_tag"}, - %{"name" => "baz_meta_tag"} + %{"name" => "foo", "content" => "bar"} ] ], [