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: "
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: "