Skip to content

Commit

Permalink
New Page - include default meta tags (#390)
Browse files Browse the repository at this point in the history
Implemented at 62094e8
but the code was incorrectly removed during the Context API refactor
  • Loading branch information
leandrocp authored Jan 8, 2024
1 parent 1c2cc0d commit e43b639
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
12 changes: 10 additions & 2 deletions lib/beacon/content.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
"""
Expand Down
26 changes: 17 additions & 9 deletions test/beacon/content_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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: "<div"})
Content.create_layout(%{site: :my_site, title: "test", template: "<div"})

assert compilation_error =~ "expected closing `>`"
end
Expand All @@ -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: "<div"})
Content.create_page(%{site: :my_site, path: "/", layout_id: layout.id, template: "<div"})

assert compilation_error =~ "expected closing `>`"
end
Expand All @@ -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: "<p>page</p>",
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: "<p>page</p>",
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: "<p>page</p>",
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()

Expand Down Expand Up @@ -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: "<div"})
Content.create_component(%{site: :my_site, name: "test", body: "<div"})

assert compilation_error =~ "expected closing `>`"
end
Expand Down
4 changes: 1 addition & 3 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
]
],
[
Expand Down

0 comments on commit e43b639

Please sign in to comment.