From b72e43168b43cedce90aaa3900aa7c6261635e27 Mon Sep 17 00:00:00 2001 From: Bertocq Date: Tue, 6 Feb 2018 11:17:03 +0100 Subject: [PATCH 1/3] Use user locale instead of default locale to format currencies --- app/helpers/application_helper.rb | 2 +- app/models/budget.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 3191a2ab9..c676b052d 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -58,7 +58,7 @@ module ApplicationHelper end def format_price(number) - number_to_currency(number, precision: 0, locale: I18n.default_locale) + number_to_currency(number, precision: 0, locale: I18n.locale) end def kaminari_path(url) diff --git a/app/models/budget.rb b/app/models/budget.rb index 9b55dc4a6..6c80e7f1f 100644 --- a/app/models/budget.rb +++ b/app/models/budget.rb @@ -124,7 +124,7 @@ class Budget < ActiveRecord::Base def formatted_amount(amount) ActionController::Base.helpers.number_to_currency(amount, precision: 0, - locale: I18n.default_locale, + locale: I18n.locale, unit: currency_symbol) end From f0dc2a02a508baf523ae64a4ff7ca6e09bb8373f Mon Sep 17 00:00:00 2001 From: Bertocq Date: Thu, 8 Mar 2018 11:42:20 +0100 Subject: [PATCH 2/3] Add Budget#formatted_amount unit test --- spec/models/budget_spec.rb | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/spec/models/budget_spec.rb b/spec/models/budget_spec.rb index 80f0a0c94..72b521861 100644 --- a/spec/models/budget_spec.rb +++ b/spec/models/budget_spec.rb @@ -228,4 +228,38 @@ describe Budget do expect(finished_phase.prev_phase).to eq(reviewing_ballots_phase) end end + + describe "#formatted_amount" do + after do + I18n.locale = :en + end + + it "correctly formats Euros with Spanish" do + budget.update(currency_symbol: '€') + I18n.locale = :es + + expect(budget.formatted_amount(1000.00)).to eq ('1.000 €') + end + + it "correctly formats Dollars with Spanish" do + budget.update(currency_symbol: '$') + I18n.locale = :es + + expect(budget.formatted_amount(1000.00)).to eq ('1.000 $') + end + + it "correctly formats Dollars with English" do + budget.update(currency_symbol: '$') + I18n.locale = :en + + expect(budget.formatted_amount(1000.00)).to eq ('$1,000') + end + + it "correctly formats Euros with English" do + budget.update(currency_symbol: '€') + I18n.locale = :en + + expect(budget.formatted_amount(1000.00)).to eq ('€1,000') + end + end end From 6e843730b074b097f59808dba9038e96b5a252bc Mon Sep 17 00:00:00 2001 From: Bertocq Date: Thu, 8 Mar 2018 13:26:36 +0100 Subject: [PATCH 3/3] Replace format_price for Budget#formatted_amount ApplicationHelper#format_price and Budget#formatted_amount has the same objective and code, but the Budget#formatted_amount method also uses the currency of the Budget to correctly give currencies format. By replacing usage of format_price with formatted_amount we can remove format_price and have a single location for currency format logic. --- app/helpers/application_helper.rb | 4 ---- app/views/budgets/results/_results_table.html.erb | 8 ++++---- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index c676b052d..486759e03 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -57,10 +57,6 @@ module ApplicationHelper SiteCustomization::ContentBlock.block_for(name, locale) end - def format_price(number) - number_to_currency(number, precision: 0, locale: I18n.locale) - end - def kaminari_path(url) "#{root_url.chomp("\/")}#{url}" end diff --git a/app/views/budgets/results/_results_table.html.erb b/app/views/budgets/results/_results_table.html.erb index ebd3c2b15..48983a858 100644 --- a/app/views/budgets/results/_results_table.html.erb +++ b/app/views/budgets/results/_results_table.html.erb @@ -21,7 +21,7 @@ <% if results_type == :compatible %> <%= t("budgets.results.amount_available") %>
- <%= format_price(heading_price) %>
+ <%= @budget.formatted_amount(heading_price) %>
<% end %> @@ -53,12 +53,12 @@ <%= investment.ballot_lines_count %> - <%= format_price investment.price %> + <%= @budget.formatted_amount(investment.price) %> <% if results_type == :compatible %> - <%= format_price amount_available - investment.price %> + title="<%= @budget.formatted_amount(amount_available) %> - <%= @budget.formatted_amount(investment.price) %>"> + <%= @budget.formatted_amount(amount_available - investment.price) %> <% amount_available -= investment.price if investment.winner? %> <% end %>