diff --git a/app/controllers/exercises_controller.rb b/app/controllers/exercises_controller.rb index 5ad20de3..11b0668b 100644 --- a/app/controllers/exercises_controller.rb +++ b/app/controllers/exercises_controller.rb @@ -26,6 +26,17 @@ class ExercisesController < ApplicationController end end + def collect_paths(files) + unique_paths = files.map(&:path).reject(&:blank?).uniq + subpaths = unique_paths.map do |path| + (path.split('/').length + 1).times.map do |n| + path.split('/').shift(n).join('/') + end + end + subpaths.flatten.uniq + end + private :collect_paths + def create @exercise = Exercise.new(exercise_params) authorize! @@ -62,6 +73,7 @@ class ExercisesController < ApplicationController def implement @submission = Submission.where(exercise_id: @exercise.id, user_id: current_user.id).order('created_at DESC').first @files = (@submission ? @submission.collect_files : @exercise.files).select(&:visible).sort_by(&:name_with_extension) + @paths = collect_paths(@files) end def index diff --git a/app/views/code_ocean/files/_form.html.slim b/app/views/code_ocean/files/_form.html.slim index dab380e3..fafe9529 100644 --- a/app/views/code_ocean/files/_form.html.slim +++ b/app/views/code_ocean/files/_form.html.slim @@ -2,6 +2,9 @@ .form-group = f.label(:name, t('activerecord.attributes.file.name')) = f.text_field(:name, class: 'form-control', required: true) + .form-group + = f.label(:path, t('activerecord.attributes.file.path')) + = f.select(:path, @paths, {}, class: 'form-control') .form-group = f.label(:file_type_id, t('activerecord.attributes.file.file_type_id')) = f.collection_select(:file_type_id, FileType.where(binary: false).order(:name), :id, :name, {}, class: 'form-control') diff --git a/spec/views/exercises/implement.html.slim_spec.rb b/spec/views/exercises/implement.html.slim_spec.rb index 2a80fe83..2eff97a7 100644 --- a/spec/views/exercises/implement.html.slim_spec.rb +++ b/spec/views/exercises/implement.html.slim_spec.rb @@ -8,6 +8,7 @@ describe 'exercises/implement.html.slim' do before(:each) do assign(:exercise, exercise) assign(:files, files) + assign(:paths, []) render end