So this is my first time using embedded schema and I’m having an issue finding information to help me work through how to handle what I need to do in my app.
Basically, a company has the option to white label their site. We’re storing their white label configurations in an embedded schema that we can pull into our front-end applications.
As of now we’re storing the white label information in a white label table in our database and each configuration belongs to a company. I’m unsure of how to handle creating and updating the embedded schema.
defmodule App.WhiteLabels.WhiteLabel do
@moduledoc false
use Ecto.Schema
import Ecto.Changese
schema "white_labels" do
field(:subdomain, :string)
belongs_to(:company, App.Companys.Company
foreign_key: :company_id
)
embeds_one(:themes, App.WhiteLabels.Theme)
timestamps()
end
@doc false
@spec changeset(%__MODULE__{}, map) :: %Ecto.Changeset{}
def changeset(schema, attrs) do
schema
|> cast(attrs, [:subdomain, :company_id])
|> validate_required([:subdomain, :company_id])
|> cast_embed(:themes, required: true)
end
defmodule App.WhiteLabels.Theme do
@moduledoc false
use Ecto.Schema
import Ecto.Changeset
embedded_schema do
field :logo, :string
field :logo_alt, :string
field :favicon, :string
field :background, :string
field :header_background, :string
field :header_text, :string
field :title, :string
field :text, :string
end
@fields [
:logo,
:logo_alt,
:favicon,
:background,
:header_background,
:header_text,
:title,
:text,
]
@doc false
@spec changeset(%__MODULE__{}, map) :: %Ecto.Changeset{}
def changeset(schema, attrs) do
schema
|> cast(attrs, @fields)
end
end
How would I create a new white label when I need to create %WhiteList{} and %Theme{} at the same time? Does this make sense? Most of the examples I find are adding new embedded schema data to already created data or updating an embedded schema.