diff --git a/lib/bh/core_ext/rails/form/check_box_helper.rb b/lib/bh/core_ext/rails/form/check_box_helper.rb index feeefb6..c9bf98a 100644 --- a/lib/bh/core_ext/rails/form/check_box_helper.rb +++ b/lib/bh/core_ext/rails/form/check_box_helper.rb @@ -19,17 +19,25 @@ def check_box(method, options = {}, checked_value = '1', unchecked_value = '0') def check_box_with_block_label(method, options = {}, &block) append_class! options, 'form-control' unless inline_form? append_class! options, 'checkbox' if horizontal_form? - options[:label] ||= method.to_s.humanize + options[:label] ||= default_label_for(method) base_field method, :checkbox, options, &block end def check_box_with_inline_label(method, options = {}, &block) options.merge! offset: true, use_label: false - options[:label] ||= method.to_s.humanize + options[:label] ||= default_label_for(method) base_field method, :checkbox, options do label_and_field 'checkbox', method, options, &block end end + + def default_label_for(method) + if object.class.respond_to?(:human_attribute_name) + object.class.human_attribute_name(method) + else + method.to_s.humanize + end + end end end end diff --git a/spec/rails/form/check_box_helper_spec.rb b/spec/rails/form/check_box_helper_spec.rb index 628e93d..32de3ba 100644 --- a/spec/rails/form/check_box_helper_spec.rb +++ b/spec/rails/form/check_box_helper_spec.rb @@ -108,5 +108,20 @@ expect(form).not_to include 'form-control' end end + + context 'given translated attribute name' do + let(:attr_name) { 'Your name' } + before do + I18n.backend.store_translations(:en, activemodel: { attributes: { user: { name: attr_name } } }) + end + + specify 'renders label with translated name' do + expect(form).to include(attr_name) + end + + after do + I18n.backend.store_translations(:en, activemodel: nil) + end + end end end \ No newline at end of file diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 4b15bb3..4e58144 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -7,6 +7,7 @@ class User include ActiveModel::Validations include ActiveModel::Conversion extend ActiveModel::Naming + extend ActiveModel::Translation attr_accessor :name