From 6eae2069dc845c5eb3cb7735bfaa7fe51ca034c6 Mon Sep 17 00:00:00 2001 From: Lauro Caetano Date: Fri, 4 Apr 2014 14:49:18 -0300 Subject: [PATCH] Add support to html markup in I18n options. Closes #764. --- CHANGELOG.md | 1 + lib/simple_form/inputs/collection_input.rb | 8 +++++++- test/inputs/collection_radio_buttons_input_test.rb | 14 ++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 94fbe77a..0f74b977 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## master ### enhancements + * Add support to html markup in the I18n options. [@laurocaetano](https://github.com/laurocaetano) * Add the `full_error` component. [@laurocaetano](https://github.com/laurocaetano) * Add support to `scope` to be used on associations. [@laurocaetano](https://github.com/laurocaetano) * Execute the association `condition` in the object context. [@laurocaetano](https://github.com/laurocaetano) diff --git a/lib/simple_form/inputs/collection_input.rb b/lib/simple_form/inputs/collection_input.rb index 9d4de151..9bf6b108 100644 --- a/lib/simple_form/inputs/collection_input.rb +++ b/lib/simple_form/inputs/collection_input.rb @@ -100,7 +100,13 @@ module SimpleForm def translate_collection if translated_collection = translate_from_namespace(:options) @collection = collection.map do |key| - [translated_collection[key] || key, key.to_s] + html_key = "#{key}_html".to_sym + + if translated_collection[html_key] + [translated_collection[html_key].html_safe || key, key.to_s] + else + [translated_collection[key] || key, key.to_s] + end end true end diff --git a/test/inputs/collection_radio_buttons_input_test.rb b/test/inputs/collection_radio_buttons_input_test.rb index 10702066..982a2fb3 100644 --- a/test/inputs/collection_radio_buttons_input_test.rb +++ b/test/inputs/collection_radio_buttons_input_test.rb @@ -92,6 +92,20 @@ class CollectionRadioButtonsInputTest < ActionView::TestCase end end + test 'input should do automatic collection translation and preserve html markup' do + swap SimpleForm, boolean_style: :nested do + store_translations(:en, simple_form: { options: { user: { + gender: { male_html: 'Male', female_html: 'Female' } + } } } ) do + with_input_for @user, :gender, :radio_buttons, collection: [:male, :female] + assert_select 'input[type=radio][value=male]' + assert_select 'input[type=radio][value=female]' + assert_select 'label[for=user_gender_male]', 'Male' + assert_select 'label[for=user_gender_female]', 'Female' + end + end + end + test 'input should mark the current radio value by default' do @user.name = "Carlos" with_input_for @user, :name, :radio_buttons, collection: ['Jose', 'Carlos']