From 2f8f3404bb5ecaaa18a9b1c91f28934d407e2071 Mon Sep 17 00:00:00 2001 From: Manuel Bustillo Date: Thu, 11 Jul 2024 20:06:48 +0200 Subject: [PATCH] Format currencies --- Gemfile | 1 + Gemfile.lock | 3 +++ app/views/expenses/index.html.erb | 4 ++-- config/initializers/ruby_extensions.rb | 5 +++++ 4 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 config/initializers/ruby_extensions.rb diff --git a/Gemfile b/Gemfile index 39f769e..ed9e507 100644 --- a/Gemfile +++ b/Gemfile @@ -61,3 +61,4 @@ group :development do # gem "spring" end +gem "money" \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index b7a8a41..e3c0418 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -117,6 +117,8 @@ GEM marcel (1.0.4) mini_mime (1.1.5) minitest (5.24.1) + money (6.16.0) + i18n (>= 0.6.4, <= 2) msgpack (1.7.2) mutex_m (0.2.0) net-imap (0.4.14) @@ -252,6 +254,7 @@ DEPENDENCIES debug importmap-rails jbuilder + money pg (~> 1.1) puma (>= 5.0) rails (~> 7.1.3, >= 7.1.3.2) diff --git a/app/views/expenses/index.html.erb b/app/views/expenses/index.html.erb index a55b835..e63f662 100644 --- a/app/views/expenses/index.html.erb +++ b/app/views/expenses/index.html.erb @@ -12,14 +12,14 @@ <% @expenses.each do |expense| %> <%= expense.name %> - <%= expense.amount %> + <%= expense.amount.to_currency %> <%= link_to "Show", expense %> <%= link_to "Edit", edit_expense_path(expense) %> <% end %> Total - <%= @expenses.sum(&:amount) %> + <%= @expenses.sum(&:amount).to_currency %> diff --git a/config/initializers/ruby_extensions.rb b/config/initializers/ruby_extensions.rb new file mode 100644 index 0000000..e3a0f9c --- /dev/null +++ b/config/initializers/ruby_extensions.rb @@ -0,0 +1,5 @@ +class Numeric + def to_currency + Money.from_amount(self, "EUR").format + end +end \ No newline at end of file