forked from kipcole9/money_sql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.ex
29 lines (26 loc) · 947 Bytes
/
schema.ex
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
defmodule Organization do
use Ecto.Schema
import Ecto.Changeset
@primary_key false
schema "organizations" do
field :payroll, Money.Ecto.Composite.Type, default_currency: :JPY
field :tax, Money.Ecto.Composite.Type, fractional_digits: 4
field :value, Money.Ecto.Composite.Type, default: Money.new(:USD, 0)
field :revenue, Money.Ecto.Map.Type, default: Money.new(:AUD, 0)
field :name, :string
field :employee_count, :integer
embeds_many :customers, Customer do
field :name, :string
field :revenue, Money.Ecto.Map.Type, default: Money.new(:USD, 0)
end
timestamps()
end
def changeset(organization, params \\ %{}) do
organization
|> cast(params, [:payroll])
|> cast_embed(:customers, with: &customer_changeset/2)
end
def customer_changeset(customer, params \\ %{}) do
cast(customer, params, [:name, :revenue])
end
end