Skip to content

Commit d6e4f8b

Browse files
committed
update
1 parent 212b253 commit d6e4f8b

4 files changed

Lines changed: 48 additions & 46 deletions

File tree

app/controllers/api/scratch/projects_controller.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@ def create
1717
original_project = load_original_project(source_project_identifier)
1818
return render json: { error: I18n.t('errors.admin.unauthorized') }, status: :unauthorized unless current_ability.can?(:show, original_project)
1919

20+
remix_params = create_params
21+
return render json: { error: I18n.t('errors.project.remixing.invalid_params') }, status: :bad_request if remix_params[:scratch_component].blank?
22+
2023
remix_origin = request.origin || request.referer
2124

2225
result = Project::CreateRemix.call(
23-
params: create_params,
26+
params: remix_params,
2427
user_id: current_user.id,
2528
original_project:,
2629
remix_origin:
@@ -73,8 +76,7 @@ def scratch_content_params
7376
end
7477

7578
def valid_original_project?(project)
76-
project.project_type == Project::Types::CODE_EDITOR_SCRATCH &&
77-
(scratch_content_params.present? || project.scratch_component.present?)
79+
project.project_type == Project::Types::CODE_EDITOR_SCRATCH
7880
end
7981
end
8082
end

lib/concepts/project/operations/create_remix.rb

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,54 @@ def validate_params(response, _params, user_id, original_project, remix_origin)
2323
end
2424

2525
def remix_project(response, params, user_id, original_project, remix_origin)
26-
response[:project] = create_remix(original_project, params, user_id, remix_origin)
26+
response[:project] = if scratch_project?(original_project)
27+
create_scratch_remix(original_project, params, user_id, remix_origin)
28+
else
29+
create_remix(original_project, params, user_id, remix_origin)
30+
end
2731
response[:project].save!
2832
response
2933
end
3034

3135
def create_remix(original_project, params, user_id, remix_origin)
3236
remix = format_project(original_project, params, user_id, remix_origin)
33-
copy_media_attachments(original_project, remix)
3437

35-
(params[:components].presence || original_project.components).each do |component|
36-
remix.components.build(record_attributes(component).slice(:name, :extension, :content))
38+
original_project.images.each do |image|
39+
remix.images.attach(image.blob)
3740
end
3841

39-
scratch_component = params[:scratch_component] || original_project.scratch_component
40-
remix.build_scratch_component(content: record_attributes(scratch_component)[:content]) if scratch_component.present?
42+
original_project.videos.each do |video|
43+
remix.videos.attach(video.blob)
44+
end
45+
46+
original_project.audio.each do |audio_file|
47+
remix.audio.attach(audio_file.blob)
48+
end
49+
50+
params[:components].each do |component|
51+
remix.components.build(component.slice(:name, :extension, :content))
52+
end
53+
54+
remix
55+
end
56+
57+
def create_scratch_remix(original_project, params, user_id, remix_origin)
58+
remix = format_project(original_project, params, user_id, remix_origin)
59+
60+
original_project.images.each do |image|
61+
remix.images.attach(image.blob)
62+
end
63+
64+
original_project.videos.each do |video|
65+
remix.videos.attach(video.blob)
66+
end
67+
68+
original_project.audio.each do |audio_file|
69+
remix.audio.attach(audio_file.blob)
70+
end
71+
72+
scratch_component = params.fetch(:scratch_component)
73+
remix.build_scratch_component(content: scratch_component[:content] || scratch_component['content'])
4174

4275
remix
4376
end
@@ -54,14 +87,8 @@ def format_project(original_project, params, user_id, remix_origin)
5487
end
5588
end
5689

57-
def record_attributes(record)
58-
(record.respond_to?(:attributes) ? record.attributes : record.to_h).symbolize_keys
59-
end
60-
61-
def copy_media_attachments(original_project, remix)
62-
original_project.images.each { |image| remix.images.attach(image.blob) }
63-
original_project.videos.each { |video| remix.videos.attach(video.blob) }
64-
original_project.audio.each { |audio_file| remix.audio.attach(audio_file.blob) }
90+
def scratch_project?(project)
91+
project.project_type == Project::Types::CODE_EDITOR_SCRATCH
6592
end
6693
end
6794
end

spec/concepts/project/create_remix_spec.rb

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -129,22 +129,6 @@
129129
end
130130
end
131131

132-
context 'when empty components are submitted' do
133-
let(:remix_params) do
134-
{
135-
name: 'My remixed project',
136-
components: []
137-
}
138-
end
139-
140-
it 'falls back to the original components' do
141-
remixed_project = create_remix[:project]
142-
143-
expect(remixed_project.components.map { |component| component_props(component) })
144-
.to eq(original_project.components.map { |component| component_props(component) })
145-
end
146-
end
147-
148132
context 'when the original project has Scratch content' do
149133
let!(:original_project) do
150134
create(:project, project_type: Project::Types::CODE_EDITOR_SCRATCH, locale: nil)
@@ -162,15 +146,6 @@
162146
create(:scratch_component, project: original_project, content: original_scratch_project)
163147
end
164148

165-
context 'when new Scratch content is not provided' do
166-
let(:remix_params) { { name: 'My remixed project' } }
167-
168-
it 'copies the original Scratch component' do
169-
expect(create_remix[:project].scratch_component.content.to_h)
170-
.to eq(original_scratch_project.deep_stringify_keys)
171-
end
172-
end
173-
174149
context 'when new Scratch content is provided' do
175150
let(:remixed_scratch_project) do
176151
{

spec/features/scratch/creating_a_scratch_project_spec.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,10 @@ def make_request(query: request_query, request_headers: headers, request_params:
104104
expect(response).to have_http_status(:unauthorized)
105105
end
106106

107-
it 'responds 404 Not Found when no Scratch content exists on the original and none is submitted' do
108-
original_project.scratch_component.destroy!
109-
107+
it 'responds 400 Bad Request when no Scratch content is submitted' do
110108
make_request(request_params: {})
111109

112-
expect(response).to have_http_status(:not_found)
110+
expect(response).to have_http_status(:bad_request)
113111
end
114112

115113
it 'creates a remix, associates it to the current user, and returns the new identifier' do

0 commit comments

Comments
 (0)