resource_specification.ex (2302B)
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.ResourceSpecification do 19 @moduledoc """ 20 Specification of a kind of resource. Could define a material item, 21 service, digital item, currency account, etc. Used instead of a 22 classification when more information is needed, particularly for recipes. 23 """ 24 25 use Zenflows.DB.Schema 26 27 alias Ecto.Changeset 28 alias Zenflows.DB.{Schema, Validate} 29 alias Zenflows.File 30 alias Zenflows.VF.Unit 31 32 @type t() :: %__MODULE__{ 33 name: String.t(), 34 images: [File.t()], 35 resource_classified_as: [String.t()] | nil, 36 note: String.t() | nil, 37 default_unit_of_effort: Unit.t() | nil, 38 default_unit_of_resource: Unit.t() | nil, 39 } 40 41 schema "vf_resource_specification" do 42 field :name, :string 43 has_many :images, File 44 field :resource_classified_as, {:array, :string} 45 field :note, :string 46 belongs_to :default_unit_of_resource, Unit 47 belongs_to :default_unit_of_effort, Unit 48 timestamps() 49 end 50 51 @reqr [:name] 52 @cast @reqr ++ ~w[ 53 resource_classified_as note 54 default_unit_of_effort_id 55 default_unit_of_resource_id 56 ]a 57 58 @doc false 59 @spec changeset(Schema.t(), Schema.params()) :: Changeset.t() 60 def changeset(schema \\ %__MODULE__{}, params) do 61 schema 62 |> Changeset.cast(params, @cast) 63 |> Changeset.validate_required(@reqr) 64 |> Validate.name(:name) 65 |> Validate.note(:note) 66 |> Changeset.cast_assoc(:images) 67 |> Validate.class(:resource_classified_as) 68 |> Changeset.assoc_constraint(:default_unit_of_resource) 69 |> Changeset.assoc_constraint(:default_unit_of_effort) 70 end 71 end