Skip to content

Commit

Permalink
Merge branch 'main' into apb/data-source
Browse files Browse the repository at this point in the history
  • Loading branch information
APB9785 committed Oct 27, 2023
2 parents b6073e1 + cbe3bf7 commit 7260d31
Show file tree
Hide file tree
Showing 34 changed files with 823 additions and 106 deletions.
32 changes: 1 addition & 31 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# How to contribute
# How to Contribute

## Issue Tags

Expand Down Expand Up @@ -45,33 +45,3 @@ get accepted:

If your pull-request addresses an issue then please add the corresponding
issue's number to the description of your pull-request.

# How to work with this project locally

## Installation

First clone this repository:

```sh
git clone [email protected]:BeaconCMS/beacon.git
```

Call setup to install deps:

```sh
mix setup
```

## Running tests

```sh
mix test
```

## Running a local app

```sh
iex -S mix dev
```

Check out https://github.com/BeaconCMS/beacon/blob/main/dev.exs and visit http://localhost:4001/dev/home
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ Main components:
- Admin - LiveView UI to manage layouts, pages, and all other resources. See https://github.com/BeaconCMS/beacon_live_admin
- Page Builder - An easy to use, drag & drop UI for building pages, targeted to non-technical users. Not released yet, in the initial stages of development.

## Contributing

Check out the [CONTRIBUTING.md](https://github.com/BeaconCMS/beacon/blob/main/CONTRIBUTING.md) doc for overall guidelines to contribute to this project,
then follow the [Local Development](https://github.com/BeaconCMS/beacon#local-development) steps to run a local project or watch the video below to understand more
about Beacon internals:

<a href="https://www.youtube.com/watch?v=5jk0fIJOFuc">
<img src="https://raw.githubusercontent.com/BeaconCMS/beacon/main/assets/images/youtube_card.png" width="512" alt="YouTube card - ElixirConf 2023 - Leandro Pereira - Beacon: The next generation of CMS in Phoenix LiveView">
</a>

## Local Development

The file `dev.exs` is a self-contained Phoenix application running Beacon with sample data and code reloading enabled. Follow these steps to get a site up and running:
Expand All @@ -48,3 +58,12 @@ Note that running a named node isn't required unless you're running Beacon LiveA

Finally, visit any of the routes defined in `dev.exs` as http://localhost:4001/dev/home
or request resources from the API as http://localhost:4001/api/pages

## Looking for help with your Elixir project?

<img src="assets/images/dockyard_logo.png" width="256" alt="DockYard logo">

At DockYard we are [ready to help you build your next Elixir project](https://dockyard.com/phoenix-consulting).
We have a unique expertise in Elixir and Phoenix development that is unmatched and we love to [write about Elixir](https://dockyard.com/blog/categories/elixir).

Have a project in mind? [Get in touch](https://dockyard.com/contact/hire-us)!
Binary file added assets/images/dockyard_logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/youtube_card.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ Logger.configure(level: :debug)

Application.put_env(:phoenix, :json_library, Jason)

display_error_pages? = false

Application.put_env(:beacon, SamplePhoenix.Endpoint,
http: [ip: {127, 0, 0, 1}, port: 4001],
server: true,
live_view: [signing_salt: "aaaaaaaa"],
secret_key_base: String.duplicate("a", 64),
debug_errors: true,
debug_errors: !display_error_pages?,
render_errors: [formats: [html: BeaconWeb.ErrorHTML]],
check_origin: false,
pubsub_server: SamplePhoenix.PubSub,
live_reload: [
Expand Down
10 changes: 5 additions & 5 deletions guides/deployment/fly.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Once you have a Beacon site up and running locally, you can have it deployed on

## Fly.io CLI

Firstly instal the fly cli tool as described at https://fly.io/docs/hands-on/install-flyctl. You're gonna use it to deploy your beacon site.
Firstly, install the Fly CLI tool as described at https://fly.io/docs/hands-on/install-flyctl. You're going to use it to deploy your beacon site.

## Sign in or sign up

Expand All @@ -22,7 +22,7 @@ fly auth login

## Dockerfile

Aplications on Fly run on containers, let's generate a Dockerfile and other supporting files, and then make a couple of changes:
Applications on Fly run on containers, let's generate a Dockerfile and other supporting files, and then make a couple of changes:

Run:

Expand Down Expand Up @@ -122,7 +122,7 @@ Before we can access the deployed site let's run seeds to populate some sample d
fly ssh console
```

2. Open a IEx console:
2. Open an IEx console:

```sh
app/bin/my_app remote
Expand All @@ -134,11 +134,11 @@ app/bin/my_app remote
MyApp.Release.beacon_seeds
```

Note that you could save some commands and just call `fly ssh console --command "/app/bin/beacon_seeds"` to run seeds, but it may fail and at this momment it's recommended to connected to the instance as showed before.
Note that you could save some commands and just call `fly ssh console --command "/app/bin/beacon_seeds"` to run seeds, but it may fail and at this momment it's recommended to connect to the instance as shown before.

## Open

Finally run the following command to see your site live:
Finally, run the following command to see your site live:

```sh
fly open my_site/home
Expand Down
10 changes: 5 additions & 5 deletions guides/introduction/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Beacon is an application that runs on top of an existing Phoenix LiveView applic

## TLDR

We recomment following the guide thoroughly, but if you want a short version or to just recap the main steps:
We recommend following the guide thoroughly, but if you want a short version or to just recap the main steps:

1. Install Elixir v1.14+

Expand All @@ -30,7 +30,7 @@ We recomment following the guide thoroughly, but if you want a short version or

6. Run `mix deps.get`

7. Add `:beacon` dependency to `.formatter.exs` in `:
7. Add `:beacon` to `import_deps` in the .formatter.exs file.

8. Run `mix beacon.install --site my_site`

Expand Down Expand Up @@ -112,8 +112,8 @@ mix deps.get

```elixir
[
import_deps: [:ecto, :ecto_sql, :phoenix, :beacon],
# rest of file
import_deps: [:ecto, :ecto_sql, :phoenix, :beacon],
# rest of file
]
```

Expand Down Expand Up @@ -296,7 +296,7 @@ For more info on site options, check out `Beacon.start_link/1`.
<main>
<h2>A blog</h2>
<ul>
<li>Path Params Blog Slug: <%%= @beacon_path_params.blog_slug %></li>
<li>Path Params Blog Slug: <%%= @beacon_path_params["blog_slug"] %></li>
<li>Live Data blog_slug_uppercase: <%%= @beacon_live_data.blog_slug_uppercase %></li>
</ul>
</main>
Expand Down
148 changes: 146 additions & 2 deletions lib/beacon/content.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ defmodule Beacon.Content do
import Ecto.Query

alias Beacon.Content.Component
alias Beacon.Content.ErrorPage
alias Beacon.Content.Layout
alias Beacon.Content.LayoutEvent
alias Beacon.Content.LayoutSnapshot
Expand All @@ -48,7 +49,7 @@ defmodule Beacon.Content do
@doc """
Returns the list of meta tags that are applied to all pages by default.
These meta tags can be overwriten or extended on a Layout or Page level.
These meta tags can be overwritten or extended on a Layout or Page level.
"""
@spec default_site_meta_tags() :: [map()]
def default_site_meta_tags do
Expand All @@ -74,6 +75,18 @@ defmodule Beacon.Content do
Layout.changeset(layout, attrs)
end

@doc """
Returns a map of attrs to load the default layout into new sites.
"""
@spec default_layout() :: map()
@doc type: :layouts
def default_layout do
%{
title: "Default",
template: "<%= @inner_content %>"
}
end

@doc """
Creates a layout.
Expand Down Expand Up @@ -1676,7 +1689,7 @@ defmodule Beacon.Content do
Note that the `:page` assigns is made available as `assigns["page"]` (String.t) due to how Solid works.
Snipets can be used in:
Snippets can be used in:
* Meta Tag value
* Page Schema (structured Schema.org tags)
Expand Down Expand Up @@ -1705,6 +1718,137 @@ defmodule Beacon.Content do
end
end

# ERROR PAGES

@doc """
Returns an `%Ecto.Changeset{}` for tracking error page changes.
## Example
iex> change_error_page(error_page, %{status: 404})
%Ecto.Changeset{data: %ErrorPage{}}
"""
@doc type: :error_pages
@spec change_error_page(ErrorPage.t(), map()) :: Changeset.t()
def change_error_page(%ErrorPage{} = error_page, attrs \\ %{}) do
ErrorPage.changeset(error_page, attrs)
end

@doc """
Returns the error page for a given site and status code, or `nil` if no matching error page exists.
"""
@doc type: :error_pages
@spec get_error_page(Site.t(), ErrorPage.error_status()) :: ErrorPage.t() | nil
def get_error_page(site, status) do
Repo.one(
from e in ErrorPage,
where: e.site == ^site,
where: e.status == ^status
)
end

@doc """
Lists all error pages for a given site.
## Options
* `:per_page` - limit how many records are returned, or pass `:infinity` to return all records.
* `:preloads` - a list of preloads to load.
"""
@doc type: :error_pages
@spec list_error_pages(Site.t(), keyword()) :: [ErrorPage.t()]
def list_error_pages(site, opts \\ []) do
per_page = Keyword.get(opts, :per_page, 20)
preloads = Keyword.get(opts, :preloads, [])

site
|> query_list_error_pages_base()
|> query_list_error_pages_limit(per_page)
|> query_list_error_pages_preloads(preloads)
|> Repo.all()
end

defp query_list_error_pages_base(site) do
from p in ErrorPage,
where: p.site == ^site,
order_by: [asc: p.status]
end

defp query_list_error_pages_limit(query, limit) when is_integer(limit), do: from(q in query, limit: ^limit)
defp query_list_error_pages_limit(query, :infinity = _limit), do: query
defp query_list_error_pages_limit(query, _per_page), do: from(q in query, limit: 20)

defp query_list_error_pages_preloads(query, [_preload | _] = preloads) do
from(q in query, preload: ^preloads)
end

defp query_list_error_pages_preloads(query, _preloads), do: query

@doc """
Creates a new error page.
"""
@doc type: :error_pages
@spec create_error_page(%{site: Site.t(), status: ErrorPage.error_status(), template: binary(), layout_id: Ecto.UUID.t()}) ::
{:ok, ErrorPage.t()} | {:error, Changeset.t()}
def create_error_page(attrs) do
%ErrorPage{}
|> ErrorPage.changeset(attrs)
|> Repo.insert()
end

@doc """
Creates a new error page, raising if the operation fails.
"""
@doc type: :error_pages
@spec create_error_page!(%{site: Site.t(), status: ErrorPage.error_status(), template: binary(), layout_id: Ecto.UUID.t()}) ::
ErrorPage.t()
def create_error_page!(attrs) do
case create_error_page(attrs) do
{:ok, error_page} -> error_page
{:error, changeset} -> raise "failed to create error page, got: #{inspect(changeset.errors)}"
end
end

@doc """
Returns attr data to load the default error_pages into new sites.
"""
@spec default_error_pages() :: [map()]
@doc type: :error_pages
def default_error_pages do
for status <- [404, 500] do
%{
status: status,
template: Plug.Conn.Status.reason_phrase(status)
}
end
end

@doc """
Updates an error page.
"""
@doc type: :error_pages
@spec update_error_page(ErrorPage.t(), map()) :: {:ok, ErrorPage.t()} | {:error, Changeset.t()}
def update_error_page(error_page, attrs) do
error_page
|> ErrorPage.changeset(attrs)
|> Repo.update()
|> tap(&maybe_reload_error_page/1)
end

def maybe_reload_error_page({:ok, error_page}), do: PubSub.error_page_updated(error_page)
def maybe_reload_error_page({:error, _error_page}), do: :noop

@doc """
Deletes an error page.
"""
@doc type: :error_pages
@spec delete_error_page(ErrorPage.t()) :: {:ok, ErrorPage.t()} | {:error, Changeset.t()}
def delete_error_page(error_page) do
Repo.delete(error_page)
end

# PAGE EVENT HANDLERS

@doc """
Expand Down
Loading

0 comments on commit 7260d31

Please sign in to comment.