mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-22 06:39:39 +02:00
Update hard-coded currency UI with currency specific params (#488)
* Update hard-coded currency UI with currency specific params * Rename extension methods to match currency option names; Move cents extension to numeric class extension * Use currency's precision to show the cents part in accounts show page --------- Co-authored-by: Sriram Krishnan <sriram@seafoodsouq.com>
This commit is contained in:
parent
1968fb0145
commit
7e883c4439
6 changed files with 78 additions and 5 deletions
|
@ -11,7 +11,7 @@
|
||||||
<div class="flex items-center gap-3">
|
<div class="flex items-center gap-3">
|
||||||
<div class="relative cursor-not-allowed">
|
<div class="relative cursor-not-allowed">
|
||||||
<div class="flex items-center gap-2 px-3 py-2">
|
<div class="flex items-center gap-2 px-3 py-2">
|
||||||
<span class="text-gray-900">USD $</span>
|
<span class="text-gray-900"><%= @account.original_currency %> <%= @account.original_currency.unit %></span>
|
||||||
<%= lucide_icon("chevron-down", class: "w-5 h-5 text-gray-500") %>
|
<%= lucide_icon("chevron-down", class: "w-5 h-5 text-gray-500") %>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -29,9 +29,11 @@
|
||||||
<p class="text-sm text-gray-500">Total Value</p>
|
<p class="text-sm text-gray-500">Total Value</p>
|
||||||
<%# TODO: Will need a better way to split a formatted monetary value into these 3 parts %>
|
<%# TODO: Will need a better way to split a formatted monetary value into these 3 parts %>
|
||||||
<p class="text-gray-900">
|
<p class="text-gray-900">
|
||||||
<span class="text-gray-500"><%= number_to_currency(@account.original_balance)[0] %></span>
|
<span class="text-gray-500"><%= @account.original_currency.unit %></span>
|
||||||
<span class="text-xl font-medium"><%= number_with_delimiter(@account.original_balance.round) %></span>
|
<span class="text-xl font-medium"><%= format_currency(@account.original_balance, precision: 0, unit: '') %></span>
|
||||||
<span class="text-gray-500">.<%= number_to_currency(@account.original_balance, precision: 2)[-2, 2] %></span>
|
<%- if @account.original_currency.precision.positive? -%>
|
||||||
|
<span class="text-gray-500"><%= @account.original_currency.separator %><%= @account.original_balance.cents(precision: @account.original_currency.precision) %></span>
|
||||||
|
<% end %>
|
||||||
</p>
|
</p>
|
||||||
<% if @balance_series.nil? %>
|
<% if @balance_series.nil? %>
|
||||||
<p class="text-sm text-gray-500">Data not available for the selected period</p>
|
<p class="text-sm text-gray-500">Data not available for the selected period</p>
|
||||||
|
|
|
@ -12,7 +12,7 @@ CURRENCY_OPTIONS = Hash.new { |hash, key| hash[key] = default_currency_options.d
|
||||||
"NZD": { unit: "NZ$", precision: 2, delimiter: ",", separator: "." },
|
"NZD": { unit: "NZ$", precision: 2, delimiter: ",", separator: "." },
|
||||||
"AUD": { unit: "A$", precision: 2, delimiter: ",", separator: "." },
|
"AUD": { unit: "A$", precision: 2, delimiter: ",", separator: "." },
|
||||||
"KRW": { unit: "₩", precision: 0, delimiter: ",", separator: "." },
|
"KRW": { unit: "₩", precision: 0, delimiter: ",", separator: "." },
|
||||||
"INR": { unit: "₹", precision: 0, delimiter: ",", separator: "." }
|
"INR": { unit: "₹", precision: 2, delimiter: ",", separator: "." }
|
||||||
)
|
)
|
||||||
|
|
||||||
EXCHANGE_RATE_ENABLED = ENV["OPEN_EXCHANGE_APP_ID"].present?
|
EXCHANGE_RATE_ENABLED = ENV["OPEN_EXCHANGE_APP_ID"].present?
|
||||||
|
|
11
config/initializers/numeric_extensions.rb
Normal file
11
config/initializers/numeric_extensions.rb
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
class Numeric
|
||||||
|
def cents(precision: 2)
|
||||||
|
return "" unless precision.positive?
|
||||||
|
|
||||||
|
cents = self.to_s.split(".")[1]
|
||||||
|
cents = "" unless cents.to_i.positive?
|
||||||
|
|
||||||
|
zero_padded_cents = cents.ljust(precision, "0")
|
||||||
|
zero_padded_cents[0..precision - 1]
|
||||||
|
end
|
||||||
|
end
|
13
config/initializers/string_extensions.rb
Normal file
13
config/initializers/string_extensions.rb
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
class String
|
||||||
|
def unit
|
||||||
|
CURRENCY_OPTIONS[self.to_sym][:unit]
|
||||||
|
end
|
||||||
|
|
||||||
|
def separator
|
||||||
|
CURRENCY_OPTIONS[self.to_sym][:separator]
|
||||||
|
end
|
||||||
|
|
||||||
|
def precision
|
||||||
|
CURRENCY_OPTIONS[self.to_sym][:precision]
|
||||||
|
end
|
||||||
|
end
|
28
test/initializers/numeric_extensions_test.rb
Normal file
28
test/initializers/numeric_extensions_test.rb
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
# test/initializers/big_decimal_extensions_test.rb
|
||||||
|
require "test_helper"
|
||||||
|
|
||||||
|
class NumericExtensionsTest < ActiveSupport::TestCase
|
||||||
|
test "#cents returns the cents part with 2 precisions by default" do
|
||||||
|
amount = 123.45
|
||||||
|
assert_equal "45", amount.cents
|
||||||
|
end
|
||||||
|
|
||||||
|
test "#cents returns empty when precision is 0" do
|
||||||
|
amount = 123.45
|
||||||
|
assert_equal "", amount.cents(precision: 0)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "#cents returns the cents part of the string with given precision" do
|
||||||
|
amount = 123.4862
|
||||||
|
assert_equal "4", amount.cents(precision: 1)
|
||||||
|
assert_equal "486", amount.cents(precision: 3)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "#cents pads the cents part with zeros up to the specified precision" do
|
||||||
|
amount_without_decimal = 123
|
||||||
|
amount_with_decimal = 123.4
|
||||||
|
|
||||||
|
assert_equal "00", amount_without_decimal.cents
|
||||||
|
assert_equal "40", amount_with_decimal.cents
|
||||||
|
end
|
||||||
|
end
|
19
test/initializers/string_extensions_test.rb
Normal file
19
test/initializers/string_extensions_test.rb
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
# test/string_extensions_test.rb
|
||||||
|
require "test_helper"
|
||||||
|
|
||||||
|
class StringExtensionsTest < ActiveSupport::TestCase
|
||||||
|
test "#unit returns the currency unit for a given currency code" do
|
||||||
|
assert_equal "$", "USD".unit
|
||||||
|
assert_equal "€", "EUR".unit
|
||||||
|
end
|
||||||
|
|
||||||
|
test "#separator returns the currency separator for a given currency code" do
|
||||||
|
assert_equal ".", "USD".separator
|
||||||
|
assert_equal ",", "EUR".separator
|
||||||
|
end
|
||||||
|
|
||||||
|
test "#precision returns the currency's precision for a given currency code" do
|
||||||
|
assert_equal 2, "USD".precision
|
||||||
|
assert_equal 0, "KRW".precision
|
||||||
|
end
|
||||||
|
end
|
Loading…
Add table
Add a link
Reference in a new issue