Skip to content

Commit c9e94f3

Browse files
committed
move rake task logic into a service
1 parent 2d31eac commit c9e94f3

4 files changed

Lines changed: 107 additions & 35 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module CourseCohortProviders
2+
class Updater
3+
def initialize(cohort:, course_to_provider_csv:, dry_run:, logger: Rails.logger)
4+
@cohort = cohort
5+
@course_to_provider_csv = course_to_provider_csv
6+
@dry_run = dry_run
7+
@logger = logger
8+
end
9+
10+
attr_reader :cohort, :course_to_provider_csv, :dry_run, :logger
11+
12+
def call
13+
CourseCohort.transaction do
14+
logger.info "Dry Run" if dry_run
15+
16+
CSV.foreach(course_to_provider_csv, headers: true, header_converters: :symbol, strip: true) do |row|
17+
course = Course.find_by!(identifier: row[:course_identifier])
18+
course_cohort = CourseCohort.find_or_create_by!(course:, cohort:)
19+
lead_provider = LeadProvider.find_by!(name: row[:lead_provider_name])
20+
course_cohort.course_cohort_providers.find_or_create_by!(lead_provider:)
21+
end
22+
23+
if dry_run
24+
logger.info "Dry run: rolling back"
25+
raise ActiveRecord::Rollback
26+
end
27+
end
28+
end
29+
end
30+
end

lib/tasks/course_cohort_providers.rake

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,12 @@ namespace :course_cohort_providers do
55

66
logger = Rails.env.test? ? Rails.logger : Logger.new($stdout)
77

8-
logger.info "Dry Run" if dry_run
9-
108
raise "Missing required argument: cohort_identifier" unless args.cohort_identifier
119

1210
cohort = Cohort.find_by(identifier: args.cohort_identifier)
1311
raise "Cohort not found with identifier: #{args.cohort_identifier}" unless cohort
1412
raise "Missing required argument: course_to_provider_csv" unless args.course_to_provider_csv
1513

16-
CourseCohort.transaction do
17-
CSV.foreach(args[:course_to_provider_csv], headers: true, header_converters: :symbol, strip: true) do |row|
18-
course = Course.find_by!(identifier: row[:course_identifier])
19-
course_cohort = CourseCohort.find_or_create_by!(course:, cohort:)
20-
lead_provider = LeadProvider.find_by!(name: row[:lead_provider_name])
21-
course_cohort.course_cohort_providers.find_or_create_by!(lead_provider:)
22-
end
23-
24-
if dry_run
25-
logger.info "Dry run: rolling back"
26-
raise ActiveRecord::Rollback
27-
end
28-
end
14+
CourseCohortProviders::Updater.new(cohort:, course_to_provider_csv: args.course_to_provider_csv, dry_run:, logger:).call
2915
end
3016
end

spec/lib/tasks/course_cohort_providers_spec.rb

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
let(:cohort_identifier) { cohort.identifier }
1010
let(:headship_course) { create(:course, :headship) }
1111
let(:csv_file_path) { csv_file.path }
12+
let(:updater) { instance_double(CourseCohortProviders::Updater, call: nil) }
1213

1314
let(:csv_file) do
1415
tempfile <<~CSV
@@ -21,6 +22,7 @@
2122
before do
2223
cohort
2324
headship_course
25+
allow(CourseCohortProviders::Updater).to receive(:new).and_return(updater)
2426
end
2527

2628
after do
@@ -54,32 +56,28 @@
5456
context "when dry run is false" do
5557
let(:dry_run) { "false" }
5658

57-
it "creates course cohorts" do
58-
run_task
59-
expect(cohort.course_cohorts.pluck(:course_id)).to contain_exactly(headship_course.id)
60-
end
61-
62-
it "creates course cohort providers" do
63-
run_task
64-
course_cohort = cohort.course_cohorts.find_by(course: headship_course)
65-
expect(headship_course.course_cohort_providers.where(course_cohort:).pluck(:lead_provider_id)).to(
66-
contain_exactly(
67-
LeadProvider.find_by(name: "Ambition Institute").id,
68-
LeadProvider.find_by(name: "Best Practice Network").id,
69-
),
59+
it "calls the updater" do
60+
expect(CourseCohortProviders::Updater).to receive(:new).with(
61+
cohort:,
62+
course_to_provider_csv: csv_file_path,
63+
dry_run: false,
64+
logger: Rails.logger,
7065
)
66+
expect(updater).to receive(:call)
67+
run_task
7168
end
7269
end
7370

7471
context "when dry run is true" do
75-
it "does not create course cohorts" do
76-
run_task
77-
expect(CourseCohort.count).to eq 0
78-
end
79-
80-
it "does not create course cohort providers" do
72+
it "calls the updater" do
73+
expect(CourseCohortProviders::Updater).to receive(:new).with(
74+
cohort:,
75+
course_to_provider_csv: csv_file_path,
76+
dry_run: true,
77+
logger: Rails.logger,
78+
)
79+
expect(updater).to receive(:call)
8180
run_task
82-
expect(CourseCohortProvider.count).to eq 0
8381
end
8482
end
8583
end
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
require "rails_helper"
2+
3+
RSpec.describe CourseCohortProviders::Updater do
4+
let(:cohort) { create(:cohort, :current) }
5+
let(:headship_course) { create(:course, :headship) }
6+
let(:csv_file_path) { csv_file.path }
7+
8+
let(:csv_file) do
9+
tempfile <<~CSV
10+
course_identifier, lead_provider_name
11+
npq-headship, Ambition Institute
12+
npq-headship, Best Practice Network
13+
CSV
14+
end
15+
16+
before do
17+
cohort
18+
headship_course
19+
end
20+
21+
describe ".call" do
22+
subject { described_class.new(cohort:, course_to_provider_csv: csv_file_path, dry_run:).call }
23+
24+
context "when dry run is false" do
25+
let(:dry_run) { false }
26+
27+
it "creates course cohorts" do
28+
subject
29+
expect(cohort.course_cohorts.pluck(:course_id)).to contain_exactly(headship_course.id)
30+
end
31+
32+
it "creates course cohort providers" do
33+
subject
34+
course_cohort = cohort.course_cohorts.find_by(course: headship_course)
35+
expect(headship_course.course_cohort_providers.where(course_cohort:).pluck(:lead_provider_id)).to(
36+
contain_exactly(
37+
LeadProvider.find_by(name: "Ambition Institute").id,
38+
LeadProvider.find_by(name: "Best Practice Network").id,
39+
),
40+
)
41+
end
42+
end
43+
44+
context "when dry run is true" do
45+
let(:dry_run) { true }
46+
47+
it "does not create course cohorts" do
48+
subject
49+
expect(CourseCohort.count).to eq 0
50+
end
51+
52+
it "does not create course cohort providers" do
53+
subject
54+
expect(CourseCohortProvider.count).to eq 0
55+
end
56+
end
57+
end
58+
end

0 commit comments

Comments
 (0)