diff --git a/app/models/maintenance_tasks/run.rb b/app/models/maintenance_tasks/run.rb index df59092b..050f79d2 100644 --- a/app/models/maintenance_tasks/run.rb +++ b/app/models/maintenance_tasks/run.rb @@ -36,9 +36,7 @@ class Run < ApplicationRecord enum status: STATUSES.to_h { |status| [status, status.to_s] } - validates :task_name, on: :create, inclusion: { - in: ->(_) { Task.available_tasks.map(&:to_s) }, - } + validate :task_name_belongs_to_a_valid_task, on: :create validate :csv_attachment_presence, on: :create validate :csv_content_type, on: :create validate :validate_task_arguments, on: :create @@ -339,6 +337,15 @@ def stuck? cancelling? && updated_at <= STUCK_TASK_TIMEOUT.ago end + # Performs validation on the task_name attribute. + # A Run must be associated with a valid Task to be valid. + # In order to confirm that, the Task is looked up by name. + def task_name_belongs_to_a_valid_task + Task.named(task_name) + rescue Task::NotFoundError + errors.add(:task_name, "must be the name of an existing Task.") + end + # Performs validation on the presence of a :csv_file attachment. # A Run for a Task that uses CsvCollection must have an attached :csv_file # to be valid. Conversely, a Run for a Task that doesn't use CsvCollection diff --git a/test/models/maintenance_tasks/runner_test.rb b/test/models/maintenance_tasks/runner_test.rb index 7bfd9925..a86eb6af 100644 --- a/test/models/maintenance_tasks/runner_test.rb +++ b/test/models/maintenance_tasks/runner_test.rb @@ -61,7 +61,7 @@ class RunnerTest < ActiveSupport::TestCase end assert_equal( - "Validation failed: Task name is not included in the list", + "Validation failed: Task name must be the name of an existing Task.", error.message, ) end