event_or_commitment.ex (1665B)
1 # Zenflows is designed to implement the Valueflows vocabulary, 2 # written and maintained by srfsh <info@dyne.org>. 3 # Copyright (C) 2021-2022 Dyne.org foundation <foundation@dyne.org>. 4 # 5 # This program is free software: you can redistribute it and/or modify 6 # it under the terms of the GNU Affero General Public License as published by 7 # the Free Software Foundation, either version 3 of the License, or 8 # (at your option) any later version. 9 # 10 # This program is distributed in the hope that it will be useful, 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 # GNU Affero General Public License for more details. 14 # 15 # You should have received a copy of the GNU Affero General Public License 16 # along with this program. If not, see <https://www.gnu.org/licenses/>. 17 18 defmodule Zenflows.VF.EventOrCommitment do 19 @moduledoc """ 20 An EconomicEvent or Commitment, mutually exclusive. 21 """ 22 23 use Zenflows.DB.Schema 24 25 alias Ecto.Changeset 26 alias Zenflows.DB.{Schema, Validate} 27 alias Zenflows.VF.{Commitment, EconomicEvent} 28 29 @type t() :: %__MODULE__{ 30 event: EconomicEvent.t() | nil, 31 commitment: Commitment.t() | nil, 32 } 33 34 schema "vf_event_or_commitment" do 35 belongs_to :event, EconomicEvent 36 belongs_to :commitment, Commitment 37 timestamps() 38 end 39 40 @cast ~w[event_id commitment_id]a 41 42 @doc false 43 @spec changeset(Schema.t(), Schema.params()) :: Changeset.t() 44 def changeset(schema \\ %__MODULE__{}, params) do 45 schema 46 |> Changeset.cast(params, @cast) 47 |> Validate.exist_xor([:event_id, :commitment_id], method: :both) 48 |> Changeset.assoc_constraint(:event) 49 |> Changeset.assoc_constraint(:commitment) 50 end 51 end