Skip to content

Commit

Permalink
Use custom i18n scope for label required html
Browse files Browse the repository at this point in the history
Since 3.1 it is possible to set a custom `i18n_scope`. This was not
respected to build the required html for label inputs.
  • Loading branch information
tvdeyen committed Mar 26, 2018
1 parent d008a2a commit ea29957
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/simple_form/components/labels.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,24 @@ module Labels
module ClassMethods #:nodoc:
def translate_required_html
i18n_cache :translate_required_html do
I18n.t(:"simple_form.required.html", default:
I18n.t(:"required.html", scope: i18n_scope, default:
%(<abbr title="#{translate_required_text}">#{translate_required_mark}</abbr>)
)
end
end

def translate_required_text
I18n.t(:"simple_form.required.text", default: 'required')
I18n.t(:"required.text", scope: i18n_scope, default: 'required')
end

def translate_required_mark
I18n.t(:"simple_form.required.mark", default: '*')
I18n.t(:"required.mark", scope: i18n_scope, default: '*')
end

private

def i18n_scope
SimpleForm.i18n_scope
end
end

Expand Down
28 changes: 28 additions & 0 deletions test/components/label_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,31 @@ def @controller.action_name; nil; end
end
end

test 'label uses custom i18n scope to find required text' do
store_translations(:en, my_scope: { required: { text: 'Pflichtfeld' } }) do
swap SimpleForm, i18n_scope: :my_scope do
with_label_for @user, :name, :string
assert_select 'form label abbr[title="Pflichtfeld"]', '*'
end
end
end

test 'label uses i18n to find required mark' do
store_translations(:en, simple_form: { required: { mark: '*-*' } }) do
with_label_for @user, :name, :string
assert_select 'form label abbr', '*-*'
end
end

test 'label uses custom i18n scope to find required mark' do
store_translations(:en, my_scope: { required: { mark: '!!' } }) do
swap SimpleForm, i18n_scope: :my_scope do
with_label_for @user, :name, :string
assert_select 'form label abbr', '!!'
end
end
end

test 'label uses i18n to find required string tag' do
store_translations(:en, simple_form: { required: { html: '<span class="required" title="requerido">*</span>' } }) do
with_label_for @user, :name, :string
Expand All @@ -264,6 +282,16 @@ def @controller.action_name; nil; end
end
end

test 'label uses custom i18n scope to find required string tag' do
store_translations(:en, my_scope: { required: { html: '<span class="mandatory" title="Pflichtfeld">!!</span>' } }) do
swap SimpleForm, i18n_scope: :my_scope do
with_label_for @user, :name, :string
assert_no_select 'form label abbr'
assert_select 'form label span.mandatory[title=Pflichtfeld]', '!!'
end
end
end

test 'label allows overwriting input id' do
with_label_for @user, :name, :string, input_html: { id: 'my_new_id' }
assert_select 'label[for=my_new_id]'
Expand Down

0 comments on commit ea29957

Please sign in to comment.