Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce dependencies on I18nRenderer #2076

Merged
merged 6 commits into from
Nov 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion app/presenters/money_question_presenter.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class MoneyQuestionPresenter < QuestionPresenter
include SmartAnswer::FormattingHelper

def response_label(value)
value_for_interpolation(SmartAnswer::Money.new(value))
format_money(SmartAnswer::Money.new(value))
end
end
2 changes: 1 addition & 1 deletion app/presenters/outcome_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def initialize(i18n_prefix, node, state = nil, options = {})
template_name: @node.name.to_s,
locals: @state.to_hash,
helpers: [
SmartAnswer::OutcomeHelper,
SmartAnswer::FormattingHelper,
SmartAnswer::OverseasPassportsHelper,
SmartAnswer::MarriageAbroadHelper
]
Expand Down
4 changes: 1 addition & 3 deletions app/presenters/question_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ class QuestionPresenter < NodePresenter
delegate [
:translate!,
:translate_and_render,
:translate_option,
:value_for_interpolation,
:number_with_delimiter
:translate_option
] => :@renderer

def initialize(i18n_prefix, node, state = nil, options = {})
Expand Down
4 changes: 3 additions & 1 deletion app/presenters/salary_question_presenter.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class SalaryQuestionPresenter < QuestionPresenter
include SmartAnswer::FormattingHelper

def response_label(value)
value_for_interpolation(SmartAnswer::Salary.new(value))
format_salary(SmartAnswer::Salary.new(value))
end
end
2 changes: 2 additions & 0 deletions app/presenters/value_question_presenter.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class ValueQuestionPresenter < QuestionPresenter
include ActionView::Helpers::NumberHelper

def response_label(value)
number_with_delimiter(value)
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
module SmartAnswer
module OutcomeHelper
module FormattingHelper
include ActionView::Helpers::NumberHelper

def format_money(amount)
number_to_currency(amount, precision: ((amount.to_f == amount.to_f.round) ? 0 : 2))
end

def format_salary(salary)
number_to_currency(salary.amount, precision: 0) + " per " + salary.period
end

def format_date(date)
return nil unless date
date.strftime('%e %B %Y')
Expand Down
4 changes: 2 additions & 2 deletions lib/smart_answer/i18n_renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ def translate!(subkey, rescue_exception: true)
nil
end

private

def value_for_interpolation(value, nested = false)
case value
when Date then I18n.localize(value, format: :long)
Expand All @@ -50,8 +52,6 @@ def value_for_interpolation(value, nested = false)
end
end

private

def i18n_node_prefix
"#{@i18n_prefix}.#{@node.name}"
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
require_relative '../test_helper'

module SmartAnswer
class OutcomeHelperTest < ActiveSupport::TestCase
include ActionView::Helpers::NumberHelper
include OutcomeHelper
class FormattingHelperTest < ActiveSupport::TestCase
include FormattingHelper

test "#format_money doesn't add pence for amounts in whole pounds" do
assert_equal '£1', format_money('1.00')
Expand All @@ -20,5 +19,9 @@ class OutcomeHelperTest < ActiveSupport::TestCase
test '#format_date returns nil when the value is nil' do
assert_equal nil, format_date(nil)
end

test '#format_salary returns whole number of pounds plus the period in which it was earned' do
assert_equal '£123 per week', format_salary(Salary.new('123.45', 'week'))
end
end
end