Bulk lesson creation#853
Conversation
747fb54 to
05dc28b
Compare
Test coverage91.57% line coverage reported by SimpleCov. |
05dc28b to
1b5947c
Compare
1b5947c to
42098d1
Compare
42098d1 to
aef91db
Compare
aef91db to
7cfd87f
Compare
There was a problem hiding this comment.
Pull request overview
Adds support for bulk lesson creation via POST /api/lessons by accepting a lesson_projects array payload, returning an array response that can optionally echo origin_identifier per entry.
Changes:
- Extend
Api::LessonsController#createto handlelesson_projectsbulk requests and render a new bulk JSON view. - Add
Lesson::CreateBulkoperation to create multiple lessons and propagateorigin_identifierinto each result. - Add/extend request + unit specs to cover bulk creation success, partial failure, and school_class validation.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| spec/features/lesson/creating_a_lesson_spec.rb | Adds request coverage for bulk-create responses, partial errors, and school_class mismatch behavior. |
| spec/concepts/lesson/create_bulk_spec.rb | Adds unit tests for Lesson::CreateBulk, including origin_identifier handling. |
| lib/concepts/lesson/operations/create_bulk.rb | Introduces Lesson::CreateBulk operation to fan out to Lesson::Create. |
| app/views/api/lessons/create_bulk.json.jbuilder | Adds bulk response renderer (array of success/error entries) with optional origin_identifier. |
| app/controllers/api/lessons_controller.rb | Adds bulk-create branching, shared strong params helper, and bulk school_class validation. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
7cfd87f to
129c583
Compare
129c583 to
a09471d
Compare
a09471d to
b50e323
Compare
b50e323 to
cda64cf
Compare
| @@ -4,6 +4,23 @@ module Api | |||
| class LessonsController < ApiController | |||
There was a problem hiding this comment.
This controller is getting longer now and I think having different code paths for the create action and many of the helpers does make it harder to follow and easier to introduce bugs (although I like having the attributes shared between them as it means that they are updated together).
Do you think it's worth improving this as part of this change? I wondered if it would be better or worse to have a different bulk controller, or if other behaviour could be moved outside of the controller.
There was a problem hiding this comment.
I've separated bulk creation into its own controller.
I've also created a LessonCreation concern for params and logic that are shared between single and batch lesson creation.
Let me know if LessonCreation makes it harder to follow the behaviour and I can move those methods back into the controllers.
| expect(response).to have_http_status(:created) | ||
| end | ||
|
|
||
| it 'responds 422 Unprocessable if school_class_id does not correspond to school_id' do |
There was a problem hiding this comment.
What happens if the school_class matches the school_id but neither are associated with the current user?
I haven't confirmed if this works or not in the existing create action - Cancancan should authorise the resource in create actions by creating a new record with the params - create_params or lesson_params here but this won't work with bulk_create_params. (see cancancan docs).
There was a problem hiding this comment.
I've added a before_action to loop through and authorize! each lesson. I've updated the specs to check that the controller responds with 403 if the user does not belong to the school.
cda64cf to
d573af8
Compare
Expand the LessonsController#create to accept a lesson_projects array in the request body and create multiple lessons in one request. Add bulk_create_params to the LessonsController, permitting origin_identifier on each entry so the caller can correlate responses with the original projects. Add Lesson::CreateBulk to create individual Lessons and echo origin_identifier on each result.
Separate batch lesson creation from single-lesson creation Add LessonCreation concern to handle shared params and logic
a6c53b4 to
aba356b
Compare
zetter-rpf
left a comment
There was a problem hiding this comment.
I found the batch controller much easier to understand, thanks for splitting that out.
Status
Closes #1472
Related to Experience CS #1761
What's changed?
origin_identifierwhen supplied so the caller can match responses to submitted projects