Skip to content

Commit

Permalink
page create - normalize attrs
Browse files Browse the repository at this point in the history
  • Loading branch information
leandrocp committed Jan 8, 2024
1 parent e43b639 commit 35b1a79
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
5 changes: 4 additions & 1 deletion dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -984,7 +984,10 @@ dev_site = [
endpoint: SamplePhoenix.Endpoint,
data_source: BeaconDataSource,
extra_page_fields: [BeaconTagsField],
lifecycle: [upload_asset: [thumbnail: &Beacon.Lifecycle.Asset.thumbnail/2, _480w: &Beacon.Lifecycle.Asset.variant_480w/2]]
lifecycle: [upload_asset: [thumbnail: &Beacon.Lifecycle.Asset.thumbnail/2, _480w: &Beacon.Lifecycle.Asset.variant_480w/2]],
default_meta_tags: [
%{"name" => "default", "content" => "dev"}
]
]

s3_bucket = System.get_env("S3_BUCKET")
Expand Down
16 changes: 10 additions & 6 deletions lib/beacon/content.ex
Original file line number Diff line number Diff line change
Expand Up @@ -460,19 +460,23 @@ defmodule Beacon.Content do
The created page is not published automatically,
you can make as much changes you need and when the page
is ready to be published you can call publish_page/1
is ready to be published you can call `publish_page/1`
It will insert a `created` event into the page timeline,
and no snapshot is created.
"""
@doc type: :pages
@spec create_page(map()) :: {:ok, Page.t()} | {:error, Changeset.t()}
def create_page(attrs) when is_map(attrs) do
site = attrs["site"] || attrs[:site]
attrs =
Map.new(attrs, fn
{key, val} when is_binary(key) -> {key, val}
{key, val} -> {Atom.to_string(key), val}
end)

Repo.transact(fn ->
with {:ok, site} <- Beacon.Types.Site.cast(site),
attrs = put_default_meta_tags(site, attrs),
with {:ok, site} <- Beacon.Types.Site.cast(attrs["site"]),
attrs = maybe_put_default_meta_tags(site, attrs),
changeset = Page.create_changeset(%Page{}, attrs),
{:ok, changeset} <- validate_page_template(changeset),
{:ok, page} <- Repo.insert(changeset),
Expand All @@ -483,9 +487,9 @@ defmodule Beacon.Content do
end)
end

defp put_default_meta_tags(site, attrs) do
defp maybe_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)
Map.put_new(attrs, "meta_tags", default_meta_tags)
end

@doc """
Expand Down

0 comments on commit 35b1a79

Please sign in to comment.