Skip to content

Commit

Permalink
Money amount formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
sergio1990 committed Jan 8, 2017
1 parent 1f772b9 commit 7cf4583
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 6 deletions.
9 changes: 9 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ config :guardian, Guardian,
secret_key: ";ofjm98y983yr82y387m238ymz23ymr8zmy23rym32ry23yrm32rm3hrm3xhhmkhmajdaou",
serializer: MoneyTracker.GuardianSerializer

config :number, currency: [
unit: "USD",
precision: 2,
delimiter: " ",
separator: ".",
format: "%u %n", # "USD 30.00"
negative_format: "(%u %n)" # "(USD 30.00)"
]

# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.
import_config "#{Mix.env}.exs"
5 changes: 3 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ defmodule MoneyTracker.Mixfile do
def application do
[mod: {MoneyTracker, []},
applications: [:phoenix, :phoenix_pubsub, :phoenix_html, :cowboy, :logger, :gettext,
:phoenix_ecto, :mariaex, :comeonin, :guardian]]
:phoenix_ecto, :mariaex, :comeonin, :guardian, :number]]
end

# Specifies which paths to compile per environment.
Expand All @@ -45,7 +45,8 @@ defmodule MoneyTracker.Mixfile do
{:mock, "~> 0.2.0", only: :test},
{:ex_machina, "~> 1.0.1", only: :test},
{:faker, "~> 0.5", only: :test},
{:exrm, "~> 1.0"}
{:exrm, "~> 1.0"},
{:number, "~> 0.5.0"}
]
end

Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"meck": {:hex, :meck, "0.8.4", "59ca1cd971372aa223138efcf9b29475bde299e1953046a0c727184790ab1520", [:make, :rebar], []},
"mime": {:hex, :mime, "1.0.1", "05c393850524767d13a53627df71beeebb016205eb43bfbd92d14d24ec7a1b51", [:mix], []},
"mock": {:hex, :mock, "0.2.0", "5991877be6bb514b647dbd6f4869bc12bd7f2829df16e86c98d6108f966d34d7", [:mix], [{:meck, "~> 0.8.2", [hex: :meck, optional: false]}]},
"number": {:hex, :number, "0.5.0", "52047c6db2168ecba7e4fc3c2b93c4f7366283dec8792eaae33de7c4c7f0efe4", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, optional: false]}]},
"phoenix": {:hex, :phoenix, "1.2.1", "6dc592249ab73c67575769765b66ad164ad25d83defa3492dc6ae269bd2a68ab", [:mix], [{:cowboy, "~> 1.0", [hex: :cowboy, optional: true]}, {:phoenix_pubsub, "~> 1.0", [hex: :phoenix_pubsub, optional: false]}, {:plug, "~> 1.1", [hex: :plug, optional: false]}, {:poison, "~> 1.5 or ~> 2.0", [hex: :poison, optional: false]}]},
"phoenix_ecto": {:hex, :phoenix_ecto, "3.2.1", "6cf11d590c61977f50fcb98ad8a10ee90ba8670a82cbf5eaf49cfaee2e95e8b7", [:mix], [{:ecto, "~> 2.1", [hex: :ecto, optional: false]}, {:phoenix_html, "~> 2.9", [hex: :phoenix_html, optional: false]}, {:plug, "~> 1.0", [hex: :plug, optional: false]}]},
"phoenix_html": {:hex, :phoenix_html, "2.9.2", "371160b30cf4e10443b015efce6f03e1f19aae98ff6487620477b13a5b2ef660", [:mix], [{:plug, "~> 1.0", [hex: :plug, optional: false]}]},
Expand Down
2 changes: 1 addition & 1 deletion web/templates/page/index.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<%= for {currency, balance} <- balances_per_currency(@current_user) do %>
<tr>
<td><%= currency %></td>
<td><%= Float.to_string(balance, decimals: 2) %></td>
<td><%= number_to_currency(balance, unit: "") %></td>
</tr>
<% end %>
</tbody>
Expand Down
2 changes: 1 addition & 1 deletion web/templates/place_transaction/index.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<tbody>
<%= for transaction <- @transactions do %>
<tr>
<td><%= Float.to_string(transaction.amount, decimals: 2) %>&nbsp;<%= @place.currency %></td>
<td><%= number_to_currency(transaction.amount, unit: @place.currency) %></td>
<td><%= transaction.description %></td>
<td><%= transaction.inserted_at %></td>

Expand Down
2 changes: 1 addition & 1 deletion web/templates/transaction/index.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<td>
<%= link transaction.place.title, to: place_path(@conn, :show, transaction.place) %>
</td>
<td><%= Float.to_string(transaction.amount, decimals: 2) %>&nbsp;<%= transaction.place.currency %></td>
<td><%= number_to_currency(transaction.amount, unit: transaction.place.currency) %></td>
<td><%= transaction.description %></td>
<td><%= transaction.inserted_at %></td>
</tr>
Expand Down
4 changes: 3 additions & 1 deletion web/views/place_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ defmodule MoneyTracker.PlaceView do
"""
@spec place_balance(Place.t) :: float
def place_balance(place) do
Float.to_string(MoneyTracker.Place.BalanceCalculator.run(place), decimals: 2)
place
|> Place.BalanceCalculator.run
|> number_to_currency(decimals: 2, unit: place.currency)
end
end
1 change: 1 addition & 0 deletions web/web.ex
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ defmodule MoneyTracker.Web do
import MoneyTracker.ErrorHelpers
import MoneyTracker.Gettext
import MoneyTracker.AuthViewHelpers
import Number.Currency
end
end

Expand Down

0 comments on commit 7cf4583

Please sign in to comment.