Skip to content

Commit 2d31eac

Browse files
committed
data model and rake task for course cohort providers
1 parent bba6a70 commit 2d31eac

14 files changed

Lines changed: 200 additions & 1 deletion

app/models/cohort.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class Cohort < ApplicationRecord
88
has_many :statements, dependent: :restrict_with_exception
99
has_many :delivery_partnerships, dependent: :destroy
1010
has_many :delivery_partners, through: :delivery_partnerships
11+
has_many :course_cohorts, dependent: :destroy
1112

1213
enum :funding, {
1314
zero: "zero",

app/models/course.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ class Course < ApplicationRecord
88
NPQ_SENCO = "npq-senco".freeze
99

1010
belongs_to :course_group, optional: true
11+
has_many :course_cohorts, dependent: :destroy
12+
has_many :course_cohort_providers, through: :course_cohorts
13+
has_many :lead_providers, through: :course_cohort_providers
1114

1215
validates :name, presence: true
1316
validates :identifier, presence: true, uniqueness: true

app/models/course_cohort.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class CourseCohort < ApplicationRecord
2+
belongs_to :course
3+
belongs_to :cohort
4+
5+
has_many :course_cohort_providers, dependent: :destroy
6+
has_many :lead_providers, through: :course_cohort_providers
7+
8+
validates :course_id, uniqueness: { scope: :cohort_id }
9+
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class CourseCohortProvider < ApplicationRecord
2+
belongs_to :course_cohort
3+
belongs_to :lead_provider
4+
5+
validates :course_cohort_id, uniqueness: { scope: :lead_provider_id }
6+
end

app/models/lead_provider.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ class LeadProvider < ApplicationRecord
9090
has_many :delivery_partnerships
9191
has_many :delivery_partners, through: :delivery_partnerships
9292

93+
has_many :course_cohort_providers
94+
has_many :course_cohorts, through: :course_cohort_providers
95+
has_many :courses, through: :course_cohorts
96+
9397
validates :name, presence: true
9498
validates :ecf_id, uniqueness: { case_sensitive: false }, allow_nil: true
9599

config/analytics.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,3 +291,15 @@ shared:
291291
- statement_id
292292
- created_at
293293
- updated_at
294+
:course_cohort_providers:
295+
- id
296+
- course_cohort_id
297+
- lead_provider_id
298+
- created_at
299+
- updated_at
300+
:course_cohorts:
301+
- id
302+
- course_id
303+
- cohort_id
304+
- created_at
305+
- updated_at
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class AddCourseCohortProviderTables < ActiveRecord::Migration[8.0]
2+
def change
3+
create_table :course_cohorts do |t|
4+
t.references :course, null: false, foreign_key: true
5+
t.references :cohort, null: false, foreign_key: true
6+
t.timestamps
7+
end
8+
add_index :course_cohorts, %i[course_id cohort_id], unique: true
9+
10+
create_table :course_cohort_providers do |t|
11+
t.references :course_cohort, null: false, foreign_key: true
12+
t.references :lead_provider, null: false, foreign_key: true
13+
t.timestamps
14+
end
15+
add_index :course_cohort_providers, %i[course_cohort_id lead_provider_id], unique: true
16+
end
17+
end

db/schema.rb

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema[8.0].define(version: 2026_04_01_112940) do
13+
ActiveRecord::Schema[8.0].define(version: 2026_04_20_093307) do
1414
# These are extensions that must be enabled in order to support this database
1515
enable_extension "btree_gin"
1616
enable_extension "citext"
@@ -241,6 +241,26 @@
241241
t.index ["statement_id"], name: "index_contracts_on_statement_id"
242242
end
243243

244+
create_table "course_cohort_providers", force: :cascade do |t|
245+
t.bigint "course_cohort_id", null: false
246+
t.bigint "lead_provider_id", null: false
247+
t.datetime "created_at", null: false
248+
t.datetime "updated_at", null: false
249+
t.index ["course_cohort_id", "lead_provider_id"], name: "idx_on_course_cohort_id_lead_provider_id_3527d5c43f", unique: true
250+
t.index ["course_cohort_id"], name: "index_course_cohort_providers_on_course_cohort_id"
251+
t.index ["lead_provider_id"], name: "index_course_cohort_providers_on_lead_provider_id"
252+
end
253+
254+
create_table "course_cohorts", force: :cascade do |t|
255+
t.bigint "course_id", null: false
256+
t.bigint "cohort_id", null: false
257+
t.datetime "created_at", null: false
258+
t.datetime "updated_at", null: false
259+
t.index ["cohort_id"], name: "index_course_cohorts_on_cohort_id"
260+
t.index ["course_id", "cohort_id"], name: "index_course_cohorts_on_course_id_and_cohort_id", unique: true
261+
t.index ["course_id"], name: "index_course_cohorts_on_course_id"
262+
end
263+
244264
create_table "course_groups", force: :cascade do |t|
245265
t.string "name", null: false
246266
t.datetime "created_at", null: false
@@ -685,6 +705,10 @@
685705
add_foreign_key "contracts", "contract_templates"
686706
add_foreign_key "contracts", "courses"
687707
add_foreign_key "contracts", "statements"
708+
add_foreign_key "course_cohort_providers", "course_cohorts"
709+
add_foreign_key "course_cohort_providers", "lead_providers"
710+
add_foreign_key "course_cohorts", "cohorts"
711+
add_foreign_key "course_cohorts", "courses"
688712
add_foreign_key "courses", "course_groups"
689713
add_foreign_key "declarations", "applications"
690714
add_foreign_key "declarations", "cohorts"
-63.9 KB
Loading
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespace :course_cohort_providers do
2+
desc "Populate course cohort providers"
3+
task :load, %i[cohort_identifier course_to_provider_csv dry_run] => :versioned_environment do |_t, args|
4+
dry_run = args[:dry_run] != "false"
5+
6+
logger = Rails.env.test? ? Rails.logger : Logger.new($stdout)
7+
8+
logger.info "Dry Run" if dry_run
9+
10+
raise "Missing required argument: cohort_identifier" unless args.cohort_identifier
11+
12+
cohort = Cohort.find_by(identifier: args.cohort_identifier)
13+
raise "Cohort not found with identifier: #{args.cohort_identifier}" unless cohort
14+
raise "Missing required argument: course_to_provider_csv" unless args.course_to_provider_csv
15+
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
29+
end
30+
end

0 commit comments

Comments
 (0)