Skip to content

Commit

Permalink
feat(ex/db/detour): set :status in changeset when :state changes`
Browse files Browse the repository at this point in the history
  • Loading branch information
firestack committed Feb 4, 2025
1 parent 823ac54 commit 2313474
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/skate/detours/db/detour.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,20 @@ defmodule Skate.Detours.Db.Detour do
def changeset(detour, attrs) do
detour
|> cast(attrs, [:state, :activated_at])
|> validate_required([:state])
|> add_status()
|> validate_required([:state, :status])
|> foreign_key_constraint(:author_id)
end

defp add_status(changeset) do
case fetch_change(changeset, :state) do
{:ok, state} ->
# Once this column is added for all detours, `categorize_detour` logic
# should be moved here and should not be needed anymore
put_change(changeset, :status, Skate.Detours.Detours.categorize_detour(%{state: state}))

_ ->
changeset
end
end
end

0 comments on commit 2313474

Please sign in to comment.