Skip to content

Commit

Permalink
Fail fast if explicit Q-specific option is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
floehopper committed Oct 14, 2015
1 parent 36b201f commit d31b386
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 19 deletions.
11 changes: 4 additions & 7 deletions lib/smart_answer/i18n_renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,13 @@ def translate_and_render(subkey, html: true)
end

def translate_option(option)
translate!("options.#{option}") || begin
I18n.translate!("#{@i18n_prefix}.options.#{option}", @state.to_hash)
rescue I18n::MissingTranslationData
option
end
translate!("options.#{option}", rescue_exception: false)
end

def translate!(subkey)
def translate!(subkey, rescue_exception: true)
I18n.translate!("#{i18n_node_prefix}.#{subkey}", state_for_interpolation)
rescue I18n::MissingTranslationData
rescue I18n::MissingTranslationData => e
raise e unless rescue_exception
nil
end

Expand Down
17 changes: 5 additions & 12 deletions test/unit/question_presenter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def teardown
assert_equal 'response', presenter.to_response('answer-text')
end

test "Options can be looked up from translation file" do
test "Options are looked up from translation file" do
question = Question::MultipleChoice.new(nil, :example_question?)
question.option yes: :yay
question.option no: :nay
Expand All @@ -172,20 +172,13 @@ def teardown
assert_equal "no", presenter.options[1].value
end

test "Options can be looked up from default values in translation file" do
test "Exception is raised if option translation is missing" do
question = Question::MultipleChoice.new(nil, :example_question?)
question.option maybe: :mumble
question.option :missing
presenter = QuestionPresenter.new("flow.test", question)

assert_equal "Mebbe", presenter.options[0].label
end

test "Options label falls back to option value" do
question = Question::MultipleChoice.new(nil, :example_question?)
question.option something: :mumble
presenter = QuestionPresenter.new("flow.test", question)

assert_equal "something", presenter.options[0].label
e = assert_raises(I18n::MissingTranslationData) { presenter.options[0].label }
assert_equal "translation missing: en-GB.flow.test.example_question?.options.missing", e.message
end

test "Avoids displaying the year for a date question when the year is 0" do
Expand Down

0 comments on commit d31b386

Please sign in to comment.