Skip to content

Commit

Permalink
feat(optional): Add optional helper text for optional attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
SheldonNunes committed May 31, 2023
1 parent 5d3294c commit 3eceaba
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
10 changes: 10 additions & 0 deletions app/models/maintenance_tasks/task_data_show.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ def parameter_names
end
end

# Returns whether a given attribute name is required
# by checking if a presence validator is added to the attribute.
#
# @param attribute_name [String] the name of the Task attribute.
# @return [Boolean] whether the Task attribute is required
def required_parameter?(attribute_name)
Task.named(name).validators_on(attribute_name)
.any? { |v| v.is_a?(ActiveModel::Validations::PresenceValidator) }
end

# @return [MaintenanceTasks::Task, nil] an instance of the Task class.
# @return [nil] if the Task file was deleted.
def new
Expand Down
3 changes: 3 additions & 0 deletions app/views/maintenance_tasks/tasks/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
<div class="control">
<%= parameter_field(ff, parameter_name) %>
</div>
<% unless @task.required_parameter?(parameter_name) %>
<p class="help">Optional</p>
<% end %>
</div>
<% end %>
<% end %>
Expand Down
9 changes: 9 additions & 0 deletions test/system/maintenance_tasks/tasks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ class TasksTest < ApplicationSystemTestCase
assert_equal("111222333", integer_attr_val)
end

test "task with optional attributes renders with helper text on the form" do
visit maintenance_tasks_path

click_on("Maintenance::ParamsTask")

refute_selector ".field:has(label[for='_task_arguments_post_ids']) .help"
assert_selector ".field:has(label[for='_task_arguments_content']) .help", text: "Optional"
end

test "task with attributes renders correct field tags on the form" do
visit maintenance_tasks_path

Expand Down

0 comments on commit 3eceaba

Please sign in to comment.