diff --git a/lib/simple_form/inputs/date_time_input.rb b/lib/simple_form/inputs/date_time_input.rb index 1943a5eb0..59661f70b 100644 --- a/lib/simple_form/inputs/date_time_input.rb +++ b/lib/simple_form/inputs/date_time_input.rb @@ -5,7 +5,7 @@ class DateTimeInput < Base def input(wrapper_options = nil) merged_input_options = merge_wrapper_options(input_html_options, wrapper_options) - if use_html5_inputs? + if html5? @builder.send(:"#{input_type}_field", attribute_name, merged_input_options) else @builder.send(:"#{input_type}_select", attribute_name, input_options, merged_input_options) @@ -15,25 +15,20 @@ def input(wrapper_options = nil) private def label_target - if use_html5_inputs? - attribute_name - else - position = case input_type - when :date, :datetime - date_order = input_options[:order] || I18n.t('date.order') - date_order.first.to_sym - else - :hour - end + return attribute_name if html5? - position = ActionView::Helpers::DateTimeSelector::POSITION[position] - "#{attribute_name}_#{position}i" + position = case input_type + when :date, :datetime + date_order = input_options[:order] || I18n.t('date.order') + date_order.first.to_sym + else + :hour end - end - def use_html5_inputs? - input_options[:html5] + position = ActionView::Helpers::DateTimeSelector::POSITION[position] + "#{attribute_name}_#{position}i" end + end end end diff --git a/test/inputs/datetime_input_test.rb b/test/inputs/datetime_input_test.rb index 5b53ff4cb..550b99680 100644 --- a/test/inputs/datetime_input_test.rb +++ b/test/inputs/datetime_input_test.rb @@ -4,55 +4,37 @@ # Tests for datetime, date and time inputs when HTML5 compatibility is enabled in the wrapper. class DateTimeInputWithHtml5Test < ActionView::TestCase - test 'input generates a datetime input for datetime attributes if HTML5 compatibility is explicitly enbled' do - with_input_for @user, :created_at, :datetime, html5: true - assert_select 'input[type="datetime-local"]' - end - - test 'input generates a datetime select for datetime attributes' do + test 'input generates a datetime input for datetime attributes' do with_input_for @user, :created_at, :datetime - - assert_select 'select.datetime' - end - - test 'input generates a date input for date attributes if HTML5 compatibility is explicitly enbled' do - with_input_for @user, :born_at, :date, html5: true - - assert_select 'input[type="date"]' + assert_select 'input[type="datetime-local"]' end - test 'input generates a date select for date attributes' do + test 'input generates a date input for date attributes' do with_input_for @user, :born_at, :date - - assert_select 'select.date' - end - - test 'input generates a time input for time attributes if HTML5 compatibility is explicitly enbled' do - with_input_for @user, :delivery_time, :time, html5: true - - assert_select 'input[type="time"]' + assert_select 'input[type="date"]' end - test 'input generates a time select for time attributes' do + test 'input generates a time input for time attributes' do with_input_for @user, :delivery_time, :time - - assert_select 'select.time' + assert_select 'input[type="time"]' end test 'input generates required html attribute' do - with_input_for @user, :delivery_time, :time, required: true, html5: true + with_input_for @user, :delivery_time, :time, required: true assert_select 'input.required' assert_select 'input[required]' end test 'input has an aria-required html attribute' do - with_input_for @user, :delivery_time, :time, required: true, html5: true + with_input_for @user, :delivery_time, :time, required: true assert_select 'input[aria-required=true]' end + end # Tests for datetime, date and time inputs when HTML5 compatibility is enabled in the wrapper. class DateTimeInputWithoutHtml5Test < ActionView::TestCase + test 'input generates a datetime select by default for datetime attributes' do swap_wrapper do with_input_for @user, :created_at, :datetime @@ -73,7 +55,7 @@ class DateTimeInputWithoutHtml5Test < ActionView::TestCase end test 'input generates a datetime input for datetime attributes if HTML5 compatibility is explicitly enabled' do - swap_wrapper do + swap_wrapper :default, custom_wrapper_with_optional_html5_component do with_input_for @user, :created_at, :datetime, html5: true assert_select 'input[type="datetime-local"]' end @@ -91,7 +73,7 @@ class DateTimeInputWithoutHtml5Test < ActionView::TestCase test 'input is able to pass options to date select' do with_input_for @user, :born_at, :date, as: :date, html5: false, - disabled: true, prompt: { year: 'ano', month: 'mês', day: 'dia' } + disabled: true, prompt: { year: 'ano', month: 'mês', day: 'dia' } assert_select 'select.date[disabled=disabled]' assert_select 'select.date option', 'ano' @@ -105,9 +87,8 @@ class DateTimeInputWithoutHtml5Test < ActionView::TestCase end test 'input generates a date input for date attributes if HTML5 compatibility is explicitly enabled' do - swap_wrapper do + swap_wrapper :default, custom_wrapper_with_optional_html5_component do with_input_for @user, :born_at, :date, html5: true - assert_select 'input[type="date"]' end end @@ -125,7 +106,7 @@ class DateTimeInputWithoutHtml5Test < ActionView::TestCase test 'input is able to pass options to time select' do with_input_for @user, :delivery_time, :time, required: true, html5: false, - disabled: true, prompt: { hour: 'hora', minute: 'minuto' } + disabled: true, prompt: { hour: 'hora', minute: 'minuto' } assert_select 'select.time[disabled=disabled]' assert_select 'select.time option', 'hora' @@ -133,9 +114,8 @@ class DateTimeInputWithoutHtml5Test < ActionView::TestCase end test 'input generates a time input for time attributes if HTML5 compatibility is explicitly enabled' do - swap_wrapper do + swap_wrapper :default, custom_wrapper_with_optional_html5_component do with_input_for @user, :delivery_time, :time, html5: true - assert_select 'input[type="time"]' end end @@ -173,4 +153,5 @@ class DateTimeInputWithoutHtml5Test < ActionView::TestCase with_input_for :project, :created_at, :date, html5: true assert_select 'label[for=project_created_at]' end + end diff --git a/test/support/misc_helpers.rb b/test/support/misc_helpers.rb index 8b629b711..2dce2e959 100644 --- a/test/support/misc_helpers.rb +++ b/test/support/misc_helpers.rb @@ -69,6 +69,14 @@ def custom_wrapper end end + def custom_wrapper_with_optional_html5_component + SimpleForm.build tag: :section, class: "custom_wrapper" do |b| + b.optional :html5 + b.use :label + b.use :input + end + end + def custom_wrapper_with_wrapped_optional_component SimpleForm.build tag: :section, class: "custom_wrapper" do |b| b.wrapper tag: :div, class: 'no_output_wrapper' do |ba|