recipe_resource.ex (2351B)
1 # Zenflows is designed to implement the Valueflows vocabulary, 2 # written and maintained by srfsh <info@dyne.org>. 3 # Copyright (C) 2021-2023 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.RecipeResource do 19 @moduledoc """ 20 Specifies the resource as part of a recipe for use in planning from 21 recipe. 22 """ 23 24 use Zenflows.DB.Schema 25 26 alias Ecto.Changeset 27 alias Zenflows.DB.{Schema, Validate} 28 alias Zenflows.File 29 alias Zenflows.VF.{ResourceSpecification, Unit} 30 31 @type t() :: %__MODULE__{ 32 name: String.t(), 33 unit_of_resource: Unit.t() | nil, 34 unit_of_effort: Unit.t() | nil, 35 resource_conforms_to: ResourceSpecification.t() | nil, 36 substitutable: boolean(), 37 images: [File.t()], 38 note: String.t() | nil, 39 } 40 41 schema "vf_recipe_resource" do 42 field :name, :string 43 belongs_to :unit_of_resource, Unit 44 belongs_to :unit_of_effort, Unit 45 field :resource_classified_as, {:array, :string} 46 belongs_to :resource_conforms_to, ResourceSpecification 47 field :substitutable, :boolean, default: false 48 has_many :images, File 49 field :note, :string 50 timestamps() 51 end 52 53 @reqr [:name] 54 @cast @reqr ++ ~w[ 55 unit_of_resource_id unit_of_effort_id 56 resource_classified_as resource_conforms_to_id 57 substitutable note 58 ]a 59 60 @doc false 61 @spec changeset(Schema.t(), Schema.params()) :: Changeset.t() 62 def changeset(schema \\ %__MODULE__{}, params) do 63 schema 64 |> Changeset.cast(params, @cast) 65 |> Changeset.validate_required(@reqr) 66 |> Validate.name(:name) 67 |> Validate.note(:note) 68 |> Changeset.cast_assoc(:images) 69 |> Validate.class(:resource_conforms_to) 70 |> Changeset.assoc_constraint(:unit_of_resource) 71 |> Changeset.assoc_constraint(:unit_of_effort) 72 |> Changeset.assoc_constraint(:resource_conforms_to) 73 end 74 end