From 09209832d38a4effeb9f4ac9bbaa5833b226be9a Mon Sep 17 00:00:00 2001 From: Sebastien Lavoie Date: Fri, 7 Jun 2024 14:27:40 -0400 Subject: [PATCH] Add support for Procs that take no argument --- app/helpers/maintenance_tasks/tasks_helper.rb | 6 ++++-- test/dummy/app/tasks/maintenance/params_task.rb | 2 ++ test/helpers/maintenance_tasks/tasks_helper_test.rb | 3 ++- test/models/maintenance_tasks/task_data_show_test.rb | 1 + test/system/maintenance_tasks/tasks_test.rb | 8 +++++++- 5 files changed, 16 insertions(+), 4 deletions(-) diff --git a/app/helpers/maintenance_tasks/tasks_helper.rb b/app/helpers/maintenance_tasks/tasks_helper.rb index 8685af9f..24f9c918 100644 --- a/app/helpers/maintenance_tasks/tasks_helper.rb +++ b/app/helpers/maintenance_tasks/tasks_helper.rb @@ -102,8 +102,9 @@ def csv_file_download_path(run) end # Resolves values covered by the inclusion validator for a task attribute. - # Only Arrays are supported, option types such as: - # Procs, lambdas, symbols, and Range are not supported and return nil. + # Only Arrays are supported. + # Procs and lambdas are also supported, but only if they take no arguments and return an Array. + # Option types such as Symbols, and Range are not supported and return nil. # # Returned values are used to populate a dropdown list of options. # @@ -118,6 +119,7 @@ def resolve_inclusion_value(task_class, parameter_name) return unless inclusion_validator in_option = inclusion_validator.options[:in] || inclusion_validator.options[:within] + in_option = in_option.call if in_option.is_a?(Proc) && in_option.arity.zero? in_option if in_option.is_a?(Array) end diff --git a/test/dummy/app/tasks/maintenance/params_task.rb b/test/dummy/app/tasks/maintenance/params_task.rb index 802d92d3..92562add 100644 --- a/test/dummy/app/tasks/maintenance/params_task.rb +++ b/test/dummy/app/tasks/maintenance/params_task.rb @@ -27,10 +27,12 @@ class ParamsTask < MaintenanceTasks::Task # Dropdown options with unsupported scenarios attribute :text_integer_attr_proc_no_arg, :integer + attribute :text_integer_attr_proc_arg, :integer attribute :text_integer_attr_undefined_symbol, :integer attribute :text_integer_attr_unbounded_range, :integer validates_inclusion_of :text_integer_attr_proc_no_arg, in: proc { [100, 200, 300] }, allow_nil: true + validates_inclusion_of :text_integer_attr_proc_arg, in: proc { |_task| [100, 200, 300] }, allow_nil: true validates_inclusion_of :text_integer_attr_undefined_symbol, in: :undefined_symbol, allow_nil: true validates_inclusion_of :text_integer_attr_unbounded_range, in: (100..), allow_nil: true diff --git a/test/helpers/maintenance_tasks/tasks_helper_test.rb b/test/helpers/maintenance_tasks/tasks_helper_test.rb index 68dce55a..b212c444 100644 --- a/test/helpers/maintenance_tasks/tasks_helper_test.rb +++ b/test/helpers/maintenance_tasks/tasks_helper_test.rb @@ -95,12 +95,13 @@ class TasksHelperTest < ActionView::TestCase [ "integer_dropdown_attr", "boolean_dropdown_attr", + "text_integer_attr_proc_no_arg", ].each do |attribute| assert_match "Select a value", markup(attribute).squish end [ - "text_integer_attr_proc_no_arg", + "text_integer_attr_proc_arg", "text_integer_attr_undefined_symbol", "text_integer_attr_unbounded_range", ].each do |attribute| diff --git a/test/models/maintenance_tasks/task_data_show_test.rb b/test/models/maintenance_tasks/task_data_show_test.rb index 09171ba8..9fb12194 100644 --- a/test/models/maintenance_tasks/task_data_show_test.rb +++ b/test/models/maintenance_tasks/task_data_show_test.rb @@ -90,6 +90,7 @@ class TaskDataShowTest < ActiveSupport::TestCase "integer_dropdown_attr", "boolean_dropdown_attr", "text_integer_attr_proc_no_arg", + "text_integer_attr_proc_arg", "text_integer_attr_undefined_symbol", "text_integer_attr_unbounded_range", ], diff --git a/test/system/maintenance_tasks/tasks_test.rb b/test/system/maintenance_tasks/tasks_test.rb index 6a70c7ea..cd1998b3 100644 --- a/test/system/maintenance_tasks/tasks_test.rb +++ b/test/system/maintenance_tasks/tasks_test.rb @@ -122,6 +122,12 @@ class TasksTest < ApplicationSystemTestCase integer_dropdown_field_options = integer_dropdown_field.find_all("option").map { |option| option[:value] } assert_equal(["", "100", "200", "300"], integer_dropdown_field_options) + integer_dropdown_field = page.find_field("task[text_integer_attr_proc_no_arg]") + assert_equal("select", integer_dropdown_field.tag_name) + assert_equal("select-one", integer_dropdown_field[:type]) + integer_dropdown_field_options = integer_dropdown_field.find_all("option").map { |option| option[:value] } + assert_equal(["", "100", "200", "300"], integer_dropdown_field_options) + boolean_dropdown_field = page.find_field("task[boolean_dropdown_attr]") assert_equal("select", boolean_dropdown_field.tag_name) assert_equal("select-one", boolean_dropdown_field[:type]) @@ -129,9 +135,9 @@ class TasksTest < ApplicationSystemTestCase assert_equal(["", "true", "false"], boolean_dropdown_field_options) [ - "text_integer_attr_proc_no_arg", "text_integer_attr_undefined_symbol", "text_integer_attr_unbounded_range", + "text_integer_attr_proc_arg", ].each do |text_integer_attr| text_integer_dropdown_field = page.find_field("task[#{text_integer_attr}]") assert_equal("input", text_integer_dropdown_field.tag_name)