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

Respect attribute translations in #check_box #126

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
12 changes: 10 additions & 2 deletions lib/bh/core_ext/rails/form/check_box_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 15 additions & 0 deletions spec/rails/form/check_box_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class User
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
extend ActiveModel::Translation

attr_accessor :name

Expand Down