Skip to content

Commit

Permalink
Add support for Procs that take no argument
Browse files Browse the repository at this point in the history
  • Loading branch information
lavoiesl committed Jun 7, 2024
1 parent 4e7c7ce commit 0920983
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 4 deletions.
6 changes: 4 additions & 2 deletions app/helpers/maintenance_tasks/tasks_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
#
Expand All @@ -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

Expand Down
2 changes: 2 additions & 0 deletions test/dummy/app/tasks/maintenance/params_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion test/helpers/maintenance_tasks/tasks_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
1 change: 1 addition & 0 deletions test/models/maintenance_tasks/task_data_show_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
Expand Down
8 changes: 7 additions & 1 deletion test/system/maintenance_tasks/tasks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,22 @@ 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])
boolean_dropdown_field_options = boolean_dropdown_field.find_all("option").map { |option| option[:value] }
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)
Expand Down

0 comments on commit 0920983

Please sign in to comment.