File size: 788 Bytes
936b397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
defmodule Plausible.Funnel.Step do
  @moduledoc """
  This module defines the database schema for a single Funnel step.
  See: `Plausible.Funnel` for more information.
  """

  use Ecto.Schema
  import Ecto.Changeset

  @type t() :: %__MODULE__{}
  schema "funnel_steps" do
    field :step_order, :integer
    belongs_to :funnel, Plausible.Funnel
    belongs_to :goal, Plausible.Goal
    timestamps()
  end

  def changeset(step, goal_or_attrs \\ %{})

  def changeset(step, %Plausible.Goal{id: goal_id}) do
    changeset(step, %{goal_id: goal_id})
  end

  def changeset(step, attrs) do
    step
    |> cast(attrs, [:goal_id])
    |> cast_assoc(:goal)
    |> validate_required([:goal_id])
    |> unique_constraint(:goal,
      name: :funnel_steps_goal_id_funnel_id_index
    )
  end
end