Skip to content

Commit

Permalink
Start splitting live data schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
APB9785 committed Dec 9, 2023
1 parent 4888dd3 commit ac308ae
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Beacon.Content.LiveData do
defmodule Beacon.Content.LiveDataAssign do
@moduledoc """
Dynamic assigns to be used by page templates and updated with page event handlers.
Expand All @@ -14,31 +14,33 @@ defmodule Beacon.Content.LiveData do

use Beacon.Schema

alias Beacon.Content.LiveDataPath

@type t :: %__MODULE__{
id: Ecto.UUID.t(),
site: Beacon.Types.Site.t(),
path: String.t(),
live_data_path_id: Ecto.UUID.t(),
live_data_path: LiveDataPath.t(),
assign: String.t(),
format: :text | :elixir,
code: String.t()
}

@formats [:text, :elixir]

schema "beacon_live_data" do
field :site, Beacon.Types.Site
field :path, :string
schema "beacon_live_data_assigns" do
field :assign, :string
field :format, Ecto.Enum, values: @formats
field :code, :string

belongs_to :live_data_path, LiveDataPath

timestamps()
end

def changeset(%__MODULE__{} = live_data, attrs) do
fields = ~w(site path assign format code)a
def changeset(%__MODULE__{} = live_data_assign, attrs) do
fields = ~w(assign format code live_data_path_id)a

live_data
live_data_assign
|> cast(attrs, fields)
|> validate_required(fields)
end
Expand Down
50 changes: 50 additions & 0 deletions lib/beacon/content/live_data_path.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
defmodule Beacon.Content.LiveDataPath do
@moduledoc """
Dynamic assigns to be used by page templates and updated with page event handlers.
> #### Do not create or edit live data manually {: .warning}
>
> Use the public functions in `Beacon.Content` instead.
> The functions in that module guarantee that all dependencies
> are created correctly and all processes are updated.
> Manipulating data manually will most likely result
> in inconsistent behavior and crashes.
"""

use Beacon.Schema

alias Beacon.Content.LiveDataAssign

@type t :: %__MODULE__{
id: Ecto.UUID.t(),
site: Beacon.Types.Site.t(),
path: String.t(),
live_data_assigns: [LiveDataAssign]
}

@formats [:text, :elixir]

schema "beacon_live_data" do
field :site, Beacon.Types.Site
field :path, :string

has_many :live_data_assigns, LiveDataAssign

timestamps()
end

def changeset(%__MODULE__{} = live_data_path, attrs) do
fields = ~w(site path)a

live_data
|> cast(attrs, fields)
|> validate_required(fields)
end

def path_changeset(%__MODULE__{} = live_data_path, attrs) do
live_data
|> cast(attrs, [:path])
|> validate_required([:path])
end
end

0 comments on commit ac308ae

Please sign in to comment.