Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Live Data Source #363

Merged
merged 42 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
ac97f81
First draft
APB9785 Oct 6, 2023
5869110
Load module
APB9785 Oct 13, 2023
e7cd63e
Group data by path for Loader
APB9785 Oct 20, 2023
3a0c003
Get live data for admin
APB9785 Oct 20, 2023
88735ed
Fix some bugs in loader
APB9785 Oct 27, 2023
d7e3f73
unique var
APB9785 Oct 27, 2023
b6073e1
standard var
APB9785 Oct 27, 2023
7260d31
Merge branch 'main' into apb/data-source
APB9785 Oct 27, 2023
f0d33bb
Merge branch 'main' into apb/data-source
APB9785 Nov 17, 2023
4888dd3
Add live data fixture
APB9785 Dec 1, 2023
ac308ae
Start splitting live data schemas
APB9785 Dec 9, 2023
f366c83
Save state
APB9785 Dec 11, 2023
df4e991
Merge branch 'main' into apb/data-source
leandrocp Dec 15, 2023
9a93170
Update loader for live_data_assigns
APB9785 Jan 5, 2024
509cacd
Make tests pass
APB9785 Jan 5, 2024
3bd468a
data source loader test
APB9785 Jan 5, 2024
4bb53f2
Prevent whitespace in assign keys
APB9785 Jan 11, 2024
98467f7
Merge branch 'main' into apb/data-source
leandrocp Jan 12, 2024
3ec048a
Update unit tests
APB9785 Jan 12, 2024
af18330
Remove API route from test support router
APB9785 Jan 12, 2024
6e9bb7f
Extract util
APB9785 Jan 12, 2024
7f434da
Reorder loaders
APB9785 Jan 12, 2024
f3d3fb6
Merge branch 'main' into apb/data-source
leandrocp Jan 18, 2024
e98dbdc
Allow root path
APB9785 Jan 22, 2024
ac95dfb
Fix params in data source
APB9785 Jan 22, 2024
ee8cb6a
Merge branch 'apb/data-source' of https://github.com/BeaconCMS/beacon…
APB9785 Jan 22, 2024
a2ae44a
Update page live test
APB9785 Jan 22, 2024
8d08cba
Remove data source from config
APB9785 Jan 23, 2024
b3511c3
Update docs and tests
APB9785 Jan 23, 2024
dc366b5
Page title + Meta tags
APB9785 Jan 23, 2024
e5e5b1e
Fix some broken tests
APB9785 Jan 23, 2024
ec4addd
Another test fix
APB9785 Jan 23, 2024
bd00a24
Resolve dialyzer missing type
APB9785 Jan 23, 2024
e0d26ed
Add path validation
APB9785 Jan 26, 2024
cbf4244
Update regex with a pattern for each segment type
APB9785 Jan 27, 2024
0ff1969
Merge branch 'main' into apb/data-source
APB9785 Feb 2, 2024
52f8b7d
Fix setup in page_live_test
APB9785 Feb 2, 2024
aaabdaa
Merge branch 'main' into apb/data-source
leandrocp Feb 13, 2024
7e1727d
clean up
leandrocp Feb 13, 2024
56db2a7
better error handling
leandrocp Feb 13, 2024
bb1b18b
Add create_live_data!/1, fix seeds and dev.exs
leandrocp Feb 13, 2024
ae50710
fix fn spec
leandrocp Feb 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .credo.exs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
# If you don't want TODO comments to cause `mix credo` to fail, just
# set this value to 0 (zero).
#
{Credo.Check.Design.TagFIXME, []},
# {Credo.Check.Design.TagFIXME, []},

#
## Readability Checks
Expand Down
54 changes: 26 additions & 28 deletions dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -94,32 +94,6 @@ defmodule SamplePhoenix.Endpoint do
plug SamplePhoenixWeb.Router
end

defmodule BeaconDataSource do
@behaviour Beacon.DataSource.Behaviour

def live_data(:dev, ["home"], _params) do
[img1] = Beacon.MediaLibrary.search(:dev, "dockyard_1")

%{year: Date.utc_today().year, img1: img1}
end

def live_data(:dy, _path, _params) do
%{slogan: "Growth, Uninhibited"}
end

def live_data(_, _, _), do: %{}

def page_title(:dev, %{page_title: page_title}), do: String.upcase(page_title)

def page_title(:dy, _), do: "dy"

def meta_tags(:dev, %{beacon_live_data: %{year: year}, meta_tags: meta_tags}) do
[%{"name" => "year", "content" => year} | meta_tags]
end

def meta_tags(:dev, %{meta_tags: meta_tags}), do: meta_tags
end

defmodule BeaconTagsField do
use Phoenix.Component
import BeaconWeb.CoreComponents
Expand Down Expand Up @@ -219,6 +193,31 @@ dev_seeds = fn ->

img2 = Beacon.MediaLibrary.upload(metadata)

home_live_data = Beacon.Content.create_live_data!(%{site: "dev", path: "home"}) |> dbg

Beacon.Content.create_assign_for_live_data(
home_live_data,
%{
format: :elixir,
key: "year",
value: """
Date.utc_today().year
"""
}
)

Beacon.Content.create_assign_for_live_data(
home_live_data,
%{
format: :elixir,
key: "img1",
value: """
[img1] = Beacon.MediaLibrary.search(:dev, "dockyard_1")
img1
"""
}
)

page_home =
Beacon.Content.create_page!(%{
path: "home",
Expand Down Expand Up @@ -982,7 +981,6 @@ end
dev_site = [
site: :dev,
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]],
default_meta_tags: [
Expand Down Expand Up @@ -1012,7 +1010,7 @@ Task.start(fn ->
{Beacon,
sites: [
dev_site,
[site: :dy, endpoint: SamplePhoenix.Endpoint, data_source: BeaconDataSource]
[site: :dy, endpoint: SamplePhoenix.Endpoint]
]},
SamplePhoenix.Endpoint
]
Expand Down
40 changes: 21 additions & 19 deletions guides/introduction/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,19 +159,7 @@ For more details please check out the docs: `mix help beacon.install`
show_sensitive_data_on_connection_error: true
```

3. Create a `BeaconDataSource` module that implements `Beacon.DataSource.Behaviour`:

```elixir
defmodule MyApp.BeaconDataSource do
@behaviour Beacon.DataSource.Behaviour

def live_data(:my_site, ["home"], _params), do: %{vals: ["first", "second", "third"]}
def live_data(:my_site, ["blog", blog_slug], _params), do: %{blog_slug_uppercase: String.upcase(blog_slug)}
def live_data(_, _, _), do: %{}
end
```

4. Edit `lib/my_app_web/router.ex` to add `use Beacon.Router`, create a new `scope`, and call `beacon_site` in your app router:
3. Edit `lib/my_app_web/router.ex` to add `use Beacon.Router`, create a new `scope`, and call `beacon_site` in your app router:

```elixir
use Beacon.Router
Expand All @@ -184,15 +172,15 @@ For more details please check out the docs: `mix help beacon.install`

Make sure you're not adding the macro `beacon_site` into the existing `scope "/", MyAppWeb`, otherwise requests will fail.

5. Include the `Beacon` supervisor in the list of `children` applications in the file `lib/my_app/application.ex`:
4. Include the `Beacon` supervisor in the list of `children` applications in the file `lib/my_app/application.ex`:

```elixir
@impl true
def start(_type, _args) do
children = [
# ommited others for brevity
MyAppWeb.Endpoint,
{Beacon, sites: [[site: :my_site, endpoint: MyAppWeb.Endpoint, data_source: MyApp.BeaconDataSource]]}
{Beacon, sites: [[site: :my_site, endpoint: MyAppWeb.Endpoint]]}
]

opts = [strategy: :one_for_one, name: MyApp.Supervisor]
Expand All @@ -203,10 +191,10 @@ Make sure you're not adding the macro `beacon_site` into the existing `scope "/"
For more info on site options, check out `Beacon.start_link/1`.

**Notes**
- The site identification has to be the same across your environment, in configuration, `beacon_site`, and `live_data`. In this example we're using `:my_site`.
- The site identification has to be the same across your environment, in configuration and `beacon_site`. In this example we're using `:my_site`.
- Include it after your app `Endpoint`.

6. Add some seeds in the seeds file `priv/repo/beacon_seeds.exs`:
5. Add some seeds in the seeds file `priv/repo/beacon_seeds.exs`:

```elixir
# Replace "<%= site %>" with your site name.
Expand All @@ -217,7 +205,7 @@ For more info on site options, check out `Beacon.start_link/1`.
# name: "sample_stylesheet",
# content: "body {cursor: zoom-in;}"
# })

alias Beacon.Content

Content.create_stylesheet!(%{
Expand Down Expand Up @@ -297,6 +285,12 @@ For more info on site options, check out `Beacon.start_link/1`.
|> Content.create_page!()
|> Content.publish_page()

home_live_data = Content.create_live_data!(%{site: "<%= site %>", path: "home"})

Content.create_assign_for_live_data(home_live_data, %{format: :elixir, key: "vals", value: """
["first", "second", "third"]
"""})

%{
path: "blog/:blog_slug",
site: "<%= site %>",
Expand All @@ -313,6 +307,14 @@ For more info on site options, check out `Beacon.start_link/1`.
}
|> Content.create_page!()
|> Content.publish_page()

blog_live_data = Content.create_live_data!(%{site: "<%= site %>", path: "blog/:blog_slug"})

Content.create_assign_for_live_data(blog_live_data, %{
format: :elixir,
key: "blog_slug_uppercase",
value: "String.upcase(blog_slug)"
})
```

6. Include new seeds in the `ecto.setup` alias in `mix.exs`:
Expand Down Expand Up @@ -341,7 +343,7 @@ Open http://localhost:4000/my_site/home and note:

- The Header and Footer from the layout
- The list element from the page
- The three components rendered with the beacon_live_data from your DataSource
- The three components rendered with live data assigns
- The zoom in cursor from the stylesheet

Open http://localhost:4000/my_site/blog/my_first_post and note:
Expand Down
2 changes: 1 addition & 1 deletion lib/beacon/beacon.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ defmodule Beacon do
# config.exs or runtime.exs
config :my_app, Beacon,
sites: [
[site: :my_site, endpoint: MyAppWeb.Endpoint, data_source: MyApp.BeaconDataSource]
[site: :my_site, endpoint: MyAppWeb.Endpoint]
],
authorization_source: MyApp.AuthorizationPolicy

Expand Down
12 changes: 0 additions & 12 deletions lib/beacon/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ defmodule Beacon.Config do
"""
@type endpoint :: module()

@typedoc """
A module that implements `Beacon.DataSource.Behaviour`, used to provide `@assigns` to pages.
"""
@type data_source :: module() | nil

@typedoc """
A module that implements `Beacon.Authorization.Behaviour`, used to provide authorization rules for the admin backend.
"""
Expand Down Expand Up @@ -138,7 +133,6 @@ defmodule Beacon.Config do
@type t :: %__MODULE__{
site: Beacon.Types.Site.t(),
endpoint: endpoint(),
data_source: data_source(),
authorization_source: authorization_source(),
css_compiler: css_compiler(),
tailwind_config: tailwind_config(),
Expand Down Expand Up @@ -170,7 +164,6 @@ defmodule Beacon.Config do

defstruct site: nil,
endpoint: nil,
data_source: nil,
authorization_source: Beacon.Authorization.DefaultPolicy,
css_compiler: Beacon.TailwindCompiler,
tailwind_config: Path.join(Application.app_dir(:beacon, "priv"), "tailwind.config.js.eex"),
Expand All @@ -197,7 +190,6 @@ defmodule Beacon.Config do
@type option ::
{:site, Beacon.Types.Site.t()}
| {:endpoint, endpoint()}
| {:data_source, data_source()}
| {:authorization_source, authorization_source()}
| {:css_compiler, css_compiler()}
| {:tailwind_config, tailwind_config()}
Expand All @@ -220,8 +212,6 @@ defmodule Beacon.Config do

* `:endpoint` - `t:endpoint/0` (required)

* `:data_source` - `t:data_source/0` (optional)

* `:authorization_source` - `t:authorization_source/0` (optional).
Note this config can't be `nil`. Defaults to `Beacon.Authorization.DefaultPolicy`.

Expand Down Expand Up @@ -261,7 +251,6 @@ defmodule Beacon.Config do
iex> Beacon.Config.new(
site: :my_site,
endpoint: MyAppWeb.Endpoint,
data_source: MyApp.SiteDataSource,
authorization_source: MyApp.SiteAuthnPolicy,
tailwind_config: Path.join(Application.app_dir(:my_app, "priv"), "tailwind.config.js.eex"),
template_formats: [
Expand All @@ -288,7 +277,6 @@ defmodule Beacon.Config do
%Beacon.Config{
site: :my_site,
endpoint: MyAppWeb.Endpoint,
data_source: MyApp.SiteDataSource,
authorization_source: MyApp.SiteAuthnPolicy,
css_compiler: Beacon.TailwindCompiler,
tailwind_config: "/my_app/priv/tailwind.config.js.eex",
Expand Down
Loading
Loading