Skip to content

Commit 24cfdd7

Browse files
committed
Teach CreateRemix how to save Scratch remixes
Keep the existing non-Scratch remix flow intact and add a Scratch-specific branch that stores scratch_component content for remixed projects. The unit coverage now exercises the Scratch remix path directly.
1 parent 8235678 commit 24cfdd7

2 files changed

Lines changed: 61 additions & 2 deletions

File tree

lib/concepts/project/operations/create_remix.rb

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ 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
@@ -50,17 +54,29 @@ def create_remix(original_project, params, user_id, remix_origin)
5054
remix
5155
end
5256

57+
def create_scratch_remix(original_project, params, user_id, remix_origin)
58+
remix = format_project(original_project, params, user_id, remix_origin)
59+
scratch_component = params.fetch(:scratch_component)
60+
remix.build_scratch_component(content: scratch_component[:content] || scratch_component['content'])
61+
62+
remix
63+
end
64+
5365
def format_project(original_project, params, user_id, remix_origin)
5466
original_project.dup.tap do |proj|
5567
proj.identifier = PhraseIdentifier.generate
5668
proj.locale = nil
57-
proj.name = params[:name]
69+
proj.name = params[:name] || original_project.name
5870
proj.user_id = user_id
5971
proj.remixed_from_id = original_project.id
6072
proj.remix_origin = remix_origin
6173
proj.lesson_id = nil # Only the original can have a lesson id
6274
end
6375
end
76+
77+
def scratch_project?(project)
78+
project.project_type == Project::Types::CODE_EDITOR_SCRATCH
79+
end
6480
end
6581
end
6682
end

spec/concepts/project/create_remix_spec.rb

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

132+
context 'when the original project has Scratch content' do
133+
let!(:original_project) do
134+
create(:project, project_type: Project::Types::CODE_EDITOR_SCRATCH, locale: nil)
135+
end
136+
let(:original_scratch_project) do
137+
{
138+
meta: { semver: '3.0.0' },
139+
targets: ['original target'],
140+
monitors: [],
141+
extensions: []
142+
}
143+
end
144+
145+
before do
146+
create(:scratch_component, project: original_project, content: original_scratch_project)
147+
end
148+
149+
context 'when new Scratch content is provided' do
150+
let(:remixed_scratch_project) do
151+
{
152+
meta: { semver: '3.0.0' },
153+
targets: ['remixed target'],
154+
monitors: [],
155+
extensions: ['pen']
156+
}
157+
end
158+
let(:remix_params) do
159+
{
160+
name: 'My remixed project',
161+
identifier: original_project.identifier,
162+
scratch_component: {
163+
content: remixed_scratch_project
164+
}
165+
}
166+
end
167+
168+
it 'uses the supplied Scratch content' do
169+
expect(create_remix[:project].scratch_component.content.to_h)
170+
.to eq(remixed_scratch_project.deep_stringify_keys)
171+
end
172+
end
173+
end
174+
132175
context 'when user_id is not present' do
133176
let(:user_id) { nil }
134177
let(:params) { { project_id: original_project.identifier } }

0 commit comments

Comments
 (0)