Skip to content

Commit 20a2705

Browse files
committed
refactor xml_id_path to use array
1 parent 4c88cf5 commit 20a2705

File tree

6 files changed

+22
-18
lines changed

6 files changed

+22
-18
lines changed

app/services/proforma_service/convert_exercise_to_task.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,18 +105,19 @@ def tests
105105
end
106106

107107
def xml_id_from_file(file)
108+
xml_id_path = file.xml_id_path || []
109+
return xml_id_path if xml_id_path&.any?
110+
108111
type = if file.teacher_defined_assessment?
109112
'test'
110113
elsif file.reference_implementation?
111114
'ms'
112115
else
113116
'file'
114117
end
115-
xml_id_path_parts = file.xml_id_path&.split('/')
116-
xml_id_path = []
117118

118-
xml_id_path << (xml_id_path_parts&.first || "co-#{type}-#{file.id}") unless type == 'file'
119-
xml_id_path << (xml_id_path_parts&.last || file.id)
119+
xml_id_path << "co-#{type}-#{file.id}" unless type == 'file'
120+
xml_id_path << file.id
120121

121122
xml_id_path
122123
end
@@ -190,7 +191,7 @@ def task_files
190191
end
191192

192193
def task_file(file)
193-
file.update(xml_id_path: xml_id_from_file(file).join('/')) if file.xml_id_path.blank?
194+
file.update(xml_id_path: xml_id_from_file(file)) if file.xml_id_path.blank?
194195

195196
xml_id = xml_id_from_file(file).last
196197
task_file = ProformaXML::TaskFile.new(

app/services/proforma_service/convert_task_to_exercise.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,10 @@ def task_files
9797
def codeocean_file_from_task_file(file, parent_object = nil)
9898
extension = File.extname(file.filename)
9999

100-
codeocean_file = CodeOcean::File.where(context: @exercise).where('xml_id_path = ? OR xml_id_path LIKE ?', file.id, "%/#{file.id}").first_or_initialize(context: @exercise)
100+
codeocean_file = CodeOcean::File
101+
.where(context: @exercise)
102+
.where('array_length(xml_id_path, 1) IS NOT NULL AND xml_id_path[array_length(xml_id_path, 1)] = ?', file.id)
103+
.first_or_initialize(context: @exercise)
101104
codeocean_file.assign_attributes(
102105
context: @exercise,
103106
file_type: file_type(extension),
@@ -106,7 +109,7 @@ def codeocean_file_from_task_file(file, parent_object = nil)
106109
read_only: file.usage_by_lms != 'edit',
107110
role: extract_meta_data(@task.meta_data&.dig('meta-data'), 'files', "CO-#{file.id}", 'role'),
108111
path: File.dirname(file.filename).in?(['.', '']) ? nil : File.dirname(file.filename),
109-
xml_id_path: (parent_object.nil? ? '' : "#{parent_object.id}/") + file.id.to_s
112+
xml_id_path: (parent_object.nil? ? [file.id] : [parent_object.id, file.id])
110113
)
111114
if file.binary
112115
codeocean_file.native_file = FileIO.new(file.content.dup.force_encoding('UTF-8'), File.basename(file.filename))

db/migrate/20240903204319_add_xml_path_to_files.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
class AddXmlPathToFiles < ActiveRecord::Migration[7.1]
44
def change
5-
add_column :files, :xml_id_path, :string, null: true, default: nil
5+
add_column :files, :xml_id_path, :string, array: true, default: [], null: true
66
end
77
end

db/schema.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@
315315
t.string "path"
316316
t.integer "file_template_id"
317317
t.boolean "hidden_feedback", default: false, null: false
318-
t.string "xml_id_path"
318+
t.string "xml_id_path", default: [], array: true
319319
t.index ["context_id", "context_type"], name: "index_files_on_context_id_and_context_type"
320320
end
321321

spec/services/proforma_service/convert_exercise_to_task_spec.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,11 @@
111111
end
112112

113113
it 'sets xml_id_path to default' do
114-
expect { convert_to_task.execute }.to change(file.reload, :xml_id_path).from(nil).to(file.id.to_s)
114+
expect { convert_to_task.execute }.to change(file.reload, :xml_id_path).from([]).to([file.id.to_s])
115115
end
116116

117117
context 'when xml_id_path is set for file' do
118-
let(:file) { create(:file, xml_id_path: 'foobar') }
118+
let(:file) { create(:file, xml_id_path: ['foobar']) }
119119

120120
it 'does not change xml_path_id' do
121121
expect { convert_to_task.execute }.not_to change(file.reload, :xml_id_path)
@@ -204,7 +204,7 @@
204204

205205
it 'creates a model-solution with one file with correct attributes' do
206206
expect(task.model_solutions.first.files.first).to have_attributes(
207-
id: file.id.to_s,
207+
id: file.id,
208208
content: file.content,
209209
filename: file.name_with_extension,
210210
used_by_grader: false,
@@ -224,7 +224,7 @@
224224
end
225225

226226
context 'when reference_implementations belong to the same proforma model_solution' do
227-
let(:files) { build_list(:file, 2, role: 'reference_implementation') {|file, i| file.xml_id_path = "proforma_ms/xml_id_#{i}" } }
227+
let(:files) { build_list(:file, 2, role: 'reference_implementation') {|file, i| file.xml_id_path = ['proforma_ms', "xml_id_#{i}"] } }
228228

229229
it 'creates a task with one model-solution' do
230230
expect(task.model_solutions).to have(1).items
@@ -267,7 +267,7 @@
267267

268268
it 'creates a test with one file with correct attributes' do
269269
expect(task.tests.first.files.first).to have_attributes(
270-
id: test_file.id.to_s,
270+
id: test_file.id,
271271
content: test_file.content,
272272
filename: test_file.name_with_extension,
273273
used_by_grader: true,
@@ -294,7 +294,7 @@
294294
end
295295

296296
context 'when test_files belong to the same proforma test' do
297-
let(:tests) { build_list(:test_file, 2) {|file, i| file.xml_id_path = "proforma_test/xml_id_#{i}" } }
297+
let(:tests) { build_list(:test_file, 2) {|file, i| file.xml_id_path = ['proforma_test', "xml_id_#{i}"] } }
298298

299299
it 'creates a test with two file' do
300300
expect(task.tests.first).to have_attributes(

spec/services/proforma_service/convert_task_to_exercise_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -563,9 +563,9 @@
563563
description: 'exercise-description')
564564
end
565565
let(:co_files) do
566-
[create(:file, xml_id_path: 'id', role: 'regular_file'),
567-
create(:file, xml_id_path: 'ms-id/ms-file-id', role: 'reference_implementation'),
568-
create(:test_file, xml_id_path: 'test-id/test-file-id')]
566+
[create(:file, xml_id_path: ['id'], role: 'regular_file'),
567+
create(:file, xml_id_path: %w[ms-id ms-file-id], role: 'reference_implementation'),
568+
create(:test_file, xml_id_path: %w[test-id test-file-id])]
569569
end
570570

571571
it 'reuses existing file' do

0 commit comments

Comments
 (0)