mirror of
https://github.com/ash-project/ash_money.git
synced 2024-09-19 12:53:11 +12:00
fix: handle missing ex_money_sql
better
This commit is contained in:
parent
b7795a977c
commit
5eec41c3c1
3 changed files with 14 additions and 5 deletions
|
@ -33,6 +33,8 @@ attribute :balance, :money
|
|||
|
||||
## AshPostgres Support
|
||||
|
||||
Add the `:ex_money_sql` dependency to your `mix.exs` file.
|
||||
|
||||
Thanks to `ex_money_sql`, there are excellent tools for lowering support for money into your postgres database. This allows for things like aggregates that sum amounts, and referencing money in expressions:
|
||||
|
||||
```elixir
|
||||
|
|
|
@ -10,11 +10,18 @@ defmodule AshMoney.Types.Money do
|
|||
storage_type: [
|
||||
type: :atom,
|
||||
default: :money_with_currency,
|
||||
doc: "The storage type for the money value. Can be `:money_with_currency` or `:map`."
|
||||
doc:
|
||||
"The storage type for the money value. Can be `:money_with_currency` or `:map`. There is no difference between the two unless `ex_money_sql` is installed."
|
||||
]
|
||||
]
|
||||
end
|
||||
|
||||
@composite_type if(Code.ensure_loaded?(Money.Ecto.Composite.Type)) do
|
||||
Money.Ecto.Composite.Type
|
||||
else
|
||||
Money.Ecto.Map.Type
|
||||
end
|
||||
|
||||
@impl true
|
||||
def cast_in_query?(constraints) do
|
||||
Keyword.get(constraints, :storage_type, :money_with_currency) == :money_with_currency
|
||||
|
@ -43,7 +50,7 @@ defmodule AshMoney.Types.Money do
|
|||
if constraints[:storage_type] == :map do
|
||||
Money.Ecto.Map.Type.cast(value)
|
||||
else
|
||||
Money.Ecto.Composite.Type.cast(value)
|
||||
@composite_type.cast(value)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -52,7 +59,7 @@ defmodule AshMoney.Types.Money do
|
|||
def cast_stored(%Money{} = value, _), do: {:ok, value}
|
||||
|
||||
def cast_stored({_amount, _currency} = value, _constraints),
|
||||
do: Money.Ecto.Composite.Type.load(value)
|
||||
do: @composite_type.load(value)
|
||||
|
||||
def cast_stored(value, _constraints) do
|
||||
Money.Ecto.Map.Type.load(value)
|
||||
|
@ -72,7 +79,7 @@ defmodule AshMoney.Types.Money do
|
|||
if constraints[:storage_type] == :map do
|
||||
Money.Ecto.Map.Type.dump(value)
|
||||
else
|
||||
Money.Ecto.Composite.Type.dump(value)
|
||||
@composite_type.dump(value)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
2
mix.exs
2
mix.exs
|
@ -144,7 +144,7 @@ defmodule AshMoney.MixProject do
|
|||
[
|
||||
{:ash, ash_version("~> 2.0")},
|
||||
{:ex_money, "~> 5.15"},
|
||||
{:ex_money_sql, "~> 1.0", optional: true},
|
||||
{:ex_money_sql, "~> 1.0"},
|
||||
{:ex_doc, github: "elixir-lang/ex_doc", only: [:dev, :test], runtime: false},
|
||||
{:ex_check, "~> 0.12", only: [:dev, :test]},
|
||||
{:credo, ">= 0.0.0", only: [:dev, :test], runtime: false},
|
||||
|
|
Loading…
Reference in a new issue