Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render a dropdown list when an inclusion validator is used #925

Merged
merged 6 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion app/helpers/maintenance_tasks/tasks_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,17 @@ def csv_file_download_path(run)
)
end

# Return the appropriate field tag for the parameter
# Return the appropriate field tag for the parameter, based on its type.
# If the parameter has a `validates_inclusion_of` validator, return a dropdown list of options instead.
def parameter_field(form_builder, parameter_name)
inclusion_validator = form_builder.object.class.validators_on(parameter_name).find do |validator|
validator.kind == :inclusion
end

return form_builder.select(
parameter_name, inclusion_validator.options[:in], prompt: "Select a value"
) if inclusion_validator

case form_builder.object.class.attribute_types[parameter_name]
when ActiveModel::Type::Integer
form_builder.number_field(parameter_name)
Expand Down
3 changes: 3 additions & 0 deletions test/dummy/app/tasks/maintenance/params_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class ParamsTask < MaintenanceTasks::Task
attribute :date_attr, :date
attribute :time_attr, :time
attribute :boolean_attr, :boolean
attribute :enum_attr, :integer

validates_inclusion_of :enum_attr, in: [100, 200, 300], allow_nil: true

class << self
attr_accessor :fast_task
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 @@ -87,6 +87,7 @@ class TaskDataShowTest < ActiveSupport::TestCase
"date_attr",
"time_attr",
"boolean_attr",
"enum_attr",
],
TaskDataShow.new("Maintenance::ParamsTask").parameter_names,
)
Expand Down
4 changes: 4 additions & 0 deletions test/system/maintenance_tasks/tasks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ class TasksTest < ApplicationSystemTestCase
boolean_field = page.find_field("task[boolean_attr]")
assert_equal("input", boolean_field.tag_name)
assert_equal("checkbox", boolean_field[:type])
enum_field = page.find_field("task[enum_attr]")
assert_equal("select", enum_field.tag_name)
enum_field_options = enum_field.find_all("option").map { |option| option[:value] }
assert_equal(["", "100", "200", "300"], enum_field_options)
end

test "view a Task with multiple pages of Runs" do
Expand Down
Loading