Skip to content

Commit a09471d

Browse files
DRY up shared create params in LessonsController
1 parent 28c2261 commit a09471d

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]
@@ -158,44 +175,15 @@ def update_params
158175
end
159176

160177
def bulk_create_params(lesson_project)
161-
lesson_project.permit(
162-
:school_id,
163-
:school_class_id,
164-
:name,
165-
:description,
166-
:visibility,
167-
:due_date,
168-
:origin_identifier,
169-
{
170-
project_attributes: [
171-
:name,
172-
:project_type,
173-
:locale,
174-
{ components: %i[id name extension content index default] },
175-
{ scratch_component: {} }
176-
]
177-
}
178-
).merge(user_id: current_user.id)
178+
permit_lesson_params(lesson_project, :origin_identifier).merge(user_id: current_user.id)
179179
end
180180

181181
def create_params
182-
params.fetch(:lesson, {}).permit(
183-
:school_id,
184-
:school_class_id,
185-
:name,
186-
:description,
187-
:visibility,
188-
:due_date,
189-
{
190-
project_attributes: [
191-
:name,
192-
:project_type,
193-
:locale,
194-
{ components: %i[id name extension content index default] },
195-
{ scratch_component: {} }
196-
]
197-
}
198-
).merge(user_id: current_user.id)
182+
permit_lesson_params(params.fetch(:lesson, {})).merge(user_id: current_user.id)
183+
end
184+
185+
def permit_lesson_params(source, *extra)
186+
source.permit(*LESSON_ATTRIBUTES, *extra, project_attributes: PROJECT_ATTRIBUTES)
199187
end
200188

201189
def school_owner?

0 commit comments

Comments
 (0)