Skip to content

Commit

Permalink
Support Decimal result in Ecto load. (#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
aglassman authored May 16, 2024
1 parent 27199f1 commit 2c17e97
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/money/ecto/amount_type.ex
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ if Code.ensure_loaded?(Ecto.Type) do

@spec load(integer()) :: {:ok, Money.t()}
def load(int) when is_integer(int), do: {:ok, Money.new(int)}
def load(%Decimal{} = decimal), do: {:ok, decimal |> Decimal.to_integer() |> Money.new()}

@spec dump(integer() | Money.t()) :: {:ok, integer()}
def dump(int) when is_integer(int), do: {:ok, int}
Expand Down
4 changes: 4 additions & 0 deletions test/money/ecto/amount_type_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ defmodule Money.Ecto.Amount.TypeTest do
assert Type.load(1000) == {:ok, Money.new(1000, :GBP)}
end

test "load/1 decimal" do
assert Type.load(Decimal.new("1000")) == {:ok, Money.new(1000, :GBP)}
end

test "dump/1 integer" do
assert Type.dump(1000) == {:ok, 1000}
end
Expand Down

0 comments on commit 2c17e97

Please sign in to comment.