From 740a484b25f39ec21692111fdb27854bf060b69d Mon Sep 17 00:00:00 2001 From: Jeff Ohrstrom Date: Mon, 23 Sep 2024 16:34:01 -0400 Subject: [PATCH] Account for empty datefield (#3817) Account for date_field having an empty value and set to it today if it is empty. --- .../lib/smart_attributes/attribute_factory.rb | 2 ++ .../test/system/batch_connect_test.rb | 27 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/apps/dashboard/app/lib/smart_attributes/attribute_factory.rb b/apps/dashboard/app/lib/smart_attributes/attribute_factory.rb index d598d460bc..72cd0708b2 100644 --- a/apps/dashboard/app/lib/smart_attributes/attribute_factory.rb +++ b/apps/dashboard/app/lib/smart_attributes/attribute_factory.rb @@ -25,6 +25,8 @@ def build(id, opts = {}) if respond_to?(build_method) send(build_method, opts) else + # date_field will throw an error if the value is an empty string. + opts[:value] = Date.today if opts[:widget] == 'date_field' && opts[:value].to_s.empty? Attribute.new(id, opts) end end diff --git a/apps/dashboard/test/system/batch_connect_test.rb b/apps/dashboard/test/system/batch_connect_test.rb index 91210a5188..52da4c6434 100644 --- a/apps/dashboard/test/system/batch_connect_test.rb +++ b/apps/dashboard/test/system/batch_connect_test.rb @@ -1519,4 +1519,31 @@ def get_favorites end end end + + test 'date_fields correctly initialize when empty' do + Dir.mktmpdir do |dir| + "#{dir}/app".tap { |d| Dir.mkdir(d) } + SysRouter.stubs(:base_path).returns(Pathname.new(dir)) + stub_scontrol + stub_sacctmgr + stub_git("#{dir}/app") + + form = <<~HEREDOC + --- + cluster: + - owens + form: + - date + attributes: + date: + widget: 'date_field' + HEREDOC + + Pathname.new("#{dir}/app/").join('form.yml').write(form) + visit new_batch_connect_session_context_url('sys/app') + + value = find('#batch_connect_session_context_date').value + assert_equal(Date.today.to_s, value) + end + end end