Skip to content

Commit

Permalink
Allow tasks to be loaded from nested task directories
Browse files Browse the repository at this point in the history
Before this commit, only tasks under `app/tasks` will be loaded in
development. After this commit, one level of nesting is supported.
  • Loading branch information
styrmis committed Apr 19, 2023
1 parent 0e56981 commit 13989bf
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 1 deletion.
17 changes: 16 additions & 1 deletion app/models/maintenance_tasks/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,23 @@ def load_constants
namespace = MaintenanceTasks.tasks_module.safe_constantize
return unless namespace

namespace.constants.map { |constant| namespace.const_get(constant) }
constants = []

namespace.constants.each do |constant_name|
constant = namespace.const_get(constant_name)

case constant
when Module
constant.constants.each do |nested_constant_name|
constants << constant.const_get(nested_constant_name)
end
when Class
constants << constant
end
end
end

constants
end

# The contents of a CSV file to be processed by a Task.
Expand Down
11 changes: 11 additions & 0 deletions test/dummy/app/tasks/maintenance/nested/nested_task.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

module Maintenance
module Nested
class NestedTask < MaintenanceTasks::Task
def process(rows)
# Task only exists to verify correct loading of tasks within subfolders
end
end
end
end
1 change: 1 addition & 0 deletions test/models/maintenance_tasks/task_data_index_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class TaskDataIndexTest < ActiveSupport::TestCase
"Maintenance::EnqueueErrorTask",
"Maintenance::ErrorTask",
"Maintenance::ImportPostsTask",
"Maintenance::Nested::NestedTask",
"Maintenance::NoCollectionTask",
# duplicate due to fixtures containing two active runs of this task
"Maintenance::NoCollectionTask",
Expand Down
1 change: 1 addition & 0 deletions test/models/maintenance_tasks/task_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class TaskTest < ActiveSupport::TestCase
"Maintenance::EnqueueErrorTask",
"Maintenance::ErrorTask",
"Maintenance::ImportPostsTask",
"Maintenance::Nested::NestedTask",
"Maintenance::NoCollectionTask",
"Maintenance::ParamsTask",
"Maintenance::TestTask",
Expand Down
1 change: 1 addition & 0 deletions test/system/maintenance_tasks/tasks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class TasksTest < ApplicationSystemTestCase
"Maintenance::CancelledEnqueueTask\nNew",
"Maintenance::EnqueueErrorTask\nNew",
"Maintenance::ErrorTask\nNew",
"Maintenance::Nested::NestedTask\nNew",
"Maintenance::ParamsTask\nNew",
"Maintenance::TestTask\nNew",
"Maintenance::UpdatePostsInBatchesTask\nNew",
Expand Down

0 comments on commit 13989bf

Please sign in to comment.