mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-24 07:39:39 +02:00
Add currency to account form to support multiple currencies (#481)
Co-authored-by: Sriram Krishnan <sriram@seafoodsouq.com> Co-authored-by: Zach Gollwitzer <zach.gollwitzer@gmail.com>
This commit is contained in:
parent
aafcf11bdd
commit
1968fb0145
4 changed files with 24 additions and 28 deletions
|
@ -90,32 +90,8 @@ module ApplicationHelper
|
|||
def format_currency(number, options = {})
|
||||
user_currency_preference = Current.family.try(:currency) || "USD"
|
||||
|
||||
case user_currency_preference
|
||||
when "USD"
|
||||
options.reverse_merge!(unit: "$", precision: 2, delimiter: ",", separator: ".")
|
||||
when "EUR"
|
||||
options.reverse_merge!(unit: "€", precision: 2, delimiter: ".", separator: ",")
|
||||
when "GBP"
|
||||
options.reverse_merge!(unit: "£", precision: 2, delimiter: ",", separator: ".")
|
||||
when "CAD"
|
||||
options.reverse_merge!(unit: "C$", precision: 2, delimiter: ",", separator: ".")
|
||||
when "MXN"
|
||||
options.reverse_merge!(unit: "MX$", precision: 2, delimiter: ",", separator: ".")
|
||||
when "HKD"
|
||||
options.reverse_merge!(unit: "HK$", precision: 2, delimiter: ",", separator: ".")
|
||||
when "CHF"
|
||||
options.reverse_merge!(unit: "CHF", precision: 2, delimiter: ".", separator: ",")
|
||||
when "SGD"
|
||||
options.reverse_merge!(unit: "S$", precision: 2, delimiter: ",", separator: ".")
|
||||
when "NZD"
|
||||
options.reverse_merge!(unit: "NZ$", precision: 2, delimiter: ",", separator: ".")
|
||||
when "AUD"
|
||||
options.reverse_merge!(unit: "A$", precision: 2, delimiter: ",", separator: ".")
|
||||
when "KRW"
|
||||
options.reverse_merge!(unit: "₩", precision: 0, delimiter: ",", separator: ".")
|
||||
else
|
||||
options.reverse_merge!(unit: "$", precision: 2, delimiter: ",", separator: ".")
|
||||
end
|
||||
currency_options = CURRENCY_OPTIONS[user_currency_preference.to_sym]
|
||||
options.reverse_merge!(currency_options)
|
||||
|
||||
number_to_currency(number, options)
|
||||
end
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
class ExchangeRate < ApplicationRecord
|
||||
def self.convert(from, to, amount)
|
||||
return amount unless EXCHANGE_RATE_ENABLED
|
||||
|
||||
rate = ExchangeRate.find_by(base_currency: from, converted_currency: to)
|
||||
amount * rate.rate
|
||||
end
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
<button type="button" class="flex items-center justify-between w-20 px-2 py-1 text-sm rounded-lg hover:bg-gray-100 focus:bg-gray-100" data-action="click->currency-dropdown#toggleMenu">
|
||||
<div data-currency-dropdown-target="label"><%= f.object.original_currency %></div>
|
||||
<%# Example of how account currency value is updated %>
|
||||
<%#= f.hidden_field :currency, data: {currency_dropdown_target: "input"} %>
|
||||
<%= f.hidden_field :original_currency, data: {currency_dropdown_target: "input"} %>
|
||||
<%= lucide_icon("chevron-down", class: "text-gray-500 w-5 h-5" ) %>
|
||||
</button>
|
||||
<ul data-currency-dropdown-target="menu" class="hidden fixed p-1 bg-white rounded-[10px] min-w-[112px] z-50 translate-y-2 border border-alpha-black-100 shadow-[inset_0_-1px_0_0_rgba(0,0,0,0.1)]">
|
||||
|
@ -12,4 +12,4 @@
|
|||
</li>
|
||||
<% end %>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
|
|
18
config/initializers/constants.rb
Normal file
18
config/initializers/constants.rb
Normal file
|
@ -0,0 +1,18 @@
|
|||
default_currency_options = { unit: "$", precision: 2, delimiter: ",", separator: "." }
|
||||
|
||||
CURRENCY_OPTIONS = Hash.new { |hash, key| hash[key] = default_currency_options.dup }.merge(
|
||||
"USD": { unit: "$", precision: 2, delimiter: ",", separator: "." },
|
||||
"EUR": { unit: "€", precision: 2, delimiter: ".", separator: "," },
|
||||
"GBP": { unit: "£", precision: 2, delimiter: ",", separator: "." },
|
||||
"CAD": { unit: "C$", precision: 2, delimiter: ",", separator: "." },
|
||||
"MXN": { unit: "MX$", precision: 2, delimiter: ",", separator: "." },
|
||||
"HKD": { unit: "HK$", precision: 2, delimiter: ",", separator: "." },
|
||||
"CHF": { unit: "CHF", precision: 2, delimiter: ".", separator: "," },
|
||||
"SGD": { unit: "S$", precision: 2, delimiter: ",", separator: "." },
|
||||
"NZD": { unit: "NZ$", precision: 2, delimiter: ",", separator: "." },
|
||||
"AUD": { unit: "A$", precision: 2, delimiter: ",", separator: "." },
|
||||
"KRW": { unit: "₩", precision: 0, delimiter: ",", separator: "." },
|
||||
"INR": { unit: "₹", precision: 0, delimiter: ",", separator: "." }
|
||||
)
|
||||
|
||||
EXCHANGE_RATE_ENABLED = ENV["OPEN_EXCHANGE_APP_ID"].present?
|
Loading…
Add table
Add a link
Reference in a new issue