Skip to content

Commit b50e323

Browse files
DRY up shared create params in LessonsController
1 parent 7ec0b21 commit b50e323

1 file changed

Lines changed: 23 additions & 35 deletions

File tree

app/controllers/api/lessons_controller.rb

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,23 @@ module Api
44
class LessonsController < ApiController
55
include RemixSelection
66

7+
LESSON_ATTRIBUTES = %i[
8+
school_id
9+
school_class_id
10+
name
11+
description
12+
visibility
13+
due_date
14+
].freeze
15+
16+
PROJECT_ATTRIBUTES = [
17+
:name,
18+
:project_type,
19+
:locale,
20+
{ components: %i[id name extension content index default] },
21+
{ scratch_component: {} }
22+
].freeze
23+
724
before_action :authorize_user, except: %i[index show]
825
before_action :verify_school_class_belongs_to_school, only: :create
926
before_action :verify_can_create_scratch_projects, only: %i[create create_copy]
@@ -151,44 +168,15 @@ def update_params
151168
end
152169

153170
def bulk_create_params(lesson_project)
154-
lesson_project.permit(
155-
:school_id,
156-
:school_class_id,
157-
:name,
158-
:description,
159-
:visibility,
160-
:due_date,
161-
:origin_identifier,
162-
{
163-
project_attributes: [
164-
:name,
165-
:project_type,
166-
:locale,
167-
{ components: %i[id name extension content index default] },
168-
{ scratch_component: {} }
169-
]
170-
}
171-
).merge(user_id: current_user.id)
171+
permit_lesson_params(lesson_project, :origin_identifier).merge(user_id: current_user.id)
172172
end
173173

174174
def create_params
175-
params.fetch(:lesson, {}).permit(
176-
:school_id,
177-
:school_class_id,
178-
:name,
179-
:description,
180-
:visibility,
181-
:due_date,
182-
{
183-
project_attributes: [
184-
:name,
185-
:project_type,
186-
:locale,
187-
{ components: %i[id name extension content index default] },
188-
{ scratch_component: {} }
189-
]
190-
}
191-
).merge(user_id: current_user.id)
175+
permit_lesson_params(params.fetch(:lesson, {})).merge(user_id: current_user.id)
176+
end
177+
178+
def permit_lesson_params(source, *extra)
179+
source.permit(*LESSON_ATTRIBUTES, *extra, project_attributes: PROJECT_ATTRIBUTES)
192180
end
193181

194182
def school_owner?

0 commit comments

Comments
 (0)