Do not double wrap fields with no_label option

Signed-off-by: Alex Coles <alex@alexbcoles.com>
pull/2612/head
Alex Coles 10 years ago
parent 8fa7115729
commit 1e122b9518
  1. 16
      lib/tabular_form_builder.rb
  2. 82
      spec/lib/tabular_form_builder_spec.rb

@ -65,7 +65,7 @@ class TabularFormBuilder < ActionView::Helpers::FormBuilder
options[:class] ||= ''
options[:class] << field_css_class('#{selector}')
(label_for_field(field, options) + container_wrap_field(super, '#{selector}')).html_safe
(label_for_field(field, options) + container_wrap_field(super, '#{selector}', options)).html_safe
end
end
END_SRC
@ -76,14 +76,14 @@ class TabularFormBuilder < ActionView::Helpers::FormBuilder
html_options[:class] ||= ''
html_options[:class] << 'form--select'
label_for_field(field, options) + container_wrap_field(super, 'select')
label_for_field(field, options) + container_wrap_field(super, 'select', options)
end
def collection_select(field, collection, value_method, text_method, options = {}, html_options = {})
html_options[:class] ||= ''
html_options[:class] << 'form--select'
label_for_field(field, options) + container_wrap_field(super, 'select')
label_for_field(field, options) + container_wrap_field(super, 'select', options)
end
private
@ -92,10 +92,11 @@ class TabularFormBuilder < ActionView::Helpers::FormBuilder
'number_field', 'password_field', 'url_field', 'telephone_field', 'email_field'
].freeze
def container_wrap_field(field_html, selector)
content_tag(:span,
content_tag(:span, field_html, class: field_container_css_class(selector)),
class: 'form--field-container')
def container_wrap_field(field_html, selector, options = {})
ret = content_tag(:span, field_html, class: field_container_css_class(selector))
ret = content_tag(:span, ret, class: 'form--field-container') unless options[:no_label]
ret
end
def field_container_css_class(selector)
@ -116,6 +117,7 @@ class TabularFormBuilder < ActionView::Helpers::FormBuilder
# Returns a label tag for the given field
def label_for_field(field, options = {}, translation_form = nil)
options = options.dup
return '' if options.delete(:no_label)
text = options[:label].is_a?(Symbol) ? l(options[:label]) : options[:label]
text ||= @object.class.human_attribute_name(field.to_sym) if @object.is_a?(ActiveRecord::Base)

@ -65,14 +65,26 @@ describe TabularFormBuilder do
end
end
shared_examples_for 'wrapped in container span' do |container = 'field-container'|
shared_examples_for 'wrapped in container' do |container = 'field-container'|
it { is_expected.to have_selector "span.form--#{container}" }
end
shared_examples_for 'not wrapped in container span' do |container = 'field-container'|
shared_examples_for 'not wrapped in container' do |container = 'field-container'|
it { is_expected.not_to have_selector "span.form--#{container}" }
end
shared_examples_for 'wrapped in field-container by default' do
context 'by default' do
it_behaves_like 'wrapped in container'
end
context 'with no_label option' do
let(:options) { { no_label: true } }
it_behaves_like 'not wrapped in container'
end
end
describe '#text_field' do
let(:options) { { title: 'Name' } }
@ -81,8 +93,8 @@ describe TabularFormBuilder do
}
it_behaves_like 'labelled by default'
it_behaves_like 'wrapped in container span'
it_behaves_like 'wrapped in container span', 'text-field-container'
it_behaves_like 'wrapped in field-container by default'
it_behaves_like 'wrapped in container', 'text-field-container'
it 'should output element' do
expect(output).to include %{
@ -101,8 +113,8 @@ describe TabularFormBuilder do
}
it_behaves_like 'labelled by default'
it_behaves_like 'wrapped in container span'
it_behaves_like 'wrapped in container span', 'text-area-container'
it_behaves_like 'wrapped in field-container by default'
it_behaves_like 'wrapped in container', 'text-area-container'
it 'should output element' do
expect(output).to include %{
@ -120,8 +132,8 @@ JJ Abrams</textarea>
}
it_behaves_like 'labelled by default'
it_behaves_like 'wrapped in container span'
it_behaves_like 'wrapped in container span', 'select-container'
it_behaves_like 'wrapped in field-container by default'
it_behaves_like 'wrapped in container', 'select-container'
it 'should output element' do
expect(output).to include %{
@ -143,8 +155,8 @@ JJ Abrams</textarea>
}
it_behaves_like 'labelled by default'
it_behaves_like 'wrapped in container span'
it_behaves_like 'wrapped in container span', 'select-container'
it_behaves_like 'wrapped in field-container by default'
it_behaves_like 'wrapped in container', 'select-container'
it 'should output element' do
expect(output).to have_selector 'select.form--select > option', count: 3
@ -161,7 +173,7 @@ JJ Abrams</textarea>
}
it_behaves_like 'labelled by default'
it_behaves_like 'wrapped in container span'
it_behaves_like 'wrapped in field-container by default'
it 'should output element' do
expect(output).to have_selector 'select', count: 3
@ -178,8 +190,8 @@ JJ Abrams</textarea>
}
it_behaves_like 'labelled by default'
it_behaves_like 'wrapped in container span'
it_behaves_like 'wrapped in container span', 'check-box-container'
it_behaves_like 'wrapped in field-container by default'
it_behaves_like 'wrapped in container', 'check-box-container'
it 'should output element' do
expect(output).to include %{
@ -198,8 +210,8 @@ JJ Abrams</textarea>
}
it_behaves_like 'not labelled'
it_behaves_like 'not wrapped in container span'
it_behaves_like 'not wrapped in container span', 'radio-button-container'
it_behaves_like 'not wrapped in container'
it_behaves_like 'not wrapped in container', 'radio-button-container'
it 'should output element' do
expect(output).to eq %{
@ -216,8 +228,8 @@ JJ Abrams</textarea>
}
it_behaves_like 'labelled by default'
it_behaves_like 'wrapped in container span'
it_behaves_like 'wrapped in container span', 'text-field-container'
it_behaves_like 'wrapped in field-container by default'
it_behaves_like 'wrapped in container', 'text-field-container'
it 'should output element' do
expect(output).to include %{
@ -236,8 +248,8 @@ JJ Abrams</textarea>
}
it_behaves_like 'labelled by default'
it_behaves_like 'wrapped in container span'
it_behaves_like 'wrapped in container span', 'range-field-container'
it_behaves_like 'wrapped in field-container by default'
it_behaves_like 'wrapped in container', 'range-field-container'
it 'should output element' do
expect(output).to include %{
@ -256,8 +268,8 @@ JJ Abrams</textarea>
}
it_behaves_like 'labelled by default'
it_behaves_like 'wrapped in container span'
it_behaves_like 'wrapped in container span', 'search-field-container'
it_behaves_like 'wrapped in field-container by default'
it_behaves_like 'wrapped in container', 'search-field-container'
it 'should output element' do
expect(output).to include %{
@ -275,8 +287,8 @@ JJ Abrams</textarea>
}
it_behaves_like 'labelled by default'
it_behaves_like 'wrapped in container span'
it_behaves_like 'wrapped in container span', 'text-field-container'
it_behaves_like 'wrapped in field-container by default'
it_behaves_like 'wrapped in container', 'text-field-container'
it 'should output element' do
expect(output).to include %{
@ -295,8 +307,8 @@ JJ Abrams</textarea>
}
it_behaves_like 'labelled by default'
it_behaves_like 'wrapped in container span'
it_behaves_like 'wrapped in container span', 'text-field-container'
it_behaves_like 'wrapped in field-container by default'
it_behaves_like 'wrapped in container', 'text-field-container'
it 'should output element' do
expect(output).to include %{
@ -315,8 +327,8 @@ JJ Abrams</textarea>
}
it_behaves_like 'labelled by default'
it_behaves_like 'wrapped in container span'
it_behaves_like 'wrapped in container span', 'text-field-container'
it_behaves_like 'wrapped in field-container by default'
it_behaves_like 'wrapped in container', 'text-field-container'
it 'should output element' do
expect(output).to include %{
@ -335,8 +347,8 @@ JJ Abrams</textarea>
}
it_behaves_like 'labelled by default'
it_behaves_like 'wrapped in container span'
it_behaves_like 'wrapped in container span', 'file-field-container'
it_behaves_like 'wrapped in field-container by default'
it_behaves_like 'wrapped in container', 'file-field-container'
it 'should output element' do
expect(output).to include %{
@ -354,8 +366,8 @@ JJ Abrams</textarea>
}
it_behaves_like 'labelled by default'
it_behaves_like 'wrapped in container span'
it_behaves_like 'wrapped in container span', 'text-field-container'
it_behaves_like 'wrapped in field-container by default'
it_behaves_like 'wrapped in container', 'text-field-container'
it 'should output element' do
expect(output).to include %{
@ -372,8 +384,8 @@ JJ Abrams</textarea>
}
it_behaves_like 'not labelled'
it_behaves_like 'not wrapped in container span'
it_behaves_like 'not wrapped in container span', 'submit-container'
it_behaves_like 'not wrapped in container'
it_behaves_like 'not wrapped in container', 'submit-container'
it 'should output element' do
expect(output).to eq %{<input name="commit" type="submit" value="Create User" />}
@ -386,8 +398,8 @@ JJ Abrams</textarea>
}
it_behaves_like 'not labelled'
it_behaves_like 'not wrapped in container span'
it_behaves_like 'not wrapped in container span', 'button-container'
it_behaves_like 'not wrapped in container'
it_behaves_like 'not wrapped in container', 'button-container'
it 'should output element' do
expect(output).to eq %{<button name="button" type="submit">Create User</button>}

Loading…
Cancel
Save