diff --git a/app/assets/javascripts/exercises.js.erb b/app/assets/javascripts/exercises.js.erb index 905c281d..6468de13 100644 --- a/app/assets/javascripts/exercises.js.erb +++ b/app/assets/javascripts/exercises.js.erb @@ -1,5 +1,6 @@ $(function() { var ACE_FILES_PATH = '/assets/ace/'; + var EXECUTION_ENVIRONMENTS = <%= ExecutionEnvironment.where('file_type_id IS NOT NULL').select(:file_type_id, :id).to_json %>; var FILE_TYPES = <%= FileType.where('file_extension IS NOT NULL').map { |file_type| [file_type.file_extension, file_type.id] }.to_h.to_json %>; var TAB_KEY_CODE = 9; @@ -12,6 +13,7 @@ $(function() { disable_search_threshold: 5, search_contains: true }); + $('#files li:last select[name*="file_type_id"]').val(getSelectedExecutionEnvironment().file_type_id); $('body, html').scrollTo('#add-file'); }; @@ -28,6 +30,12 @@ $(function() { }); }; + var getSelectedExecutionEnvironment = function() { + return _.find(EXECUTION_ENVIRONMENTS, function(execution_environment) { + return execution_environment.id === parseInt($('#exercise_execution_environment_id').val()); + }, this); + }; + var highlightCode = function() { $('pre code').each(function(index, element) { hljs.highlightBlock(element); @@ -40,8 +48,7 @@ $(function() { var file_type_id = FILE_TYPES['.' + filename.split('.')[1]]; var parent = $(this).parents('li'); parent.find('input[name*="name"]').val(filename.split('.')[0]); - parent.find('select[name*="file_type_id"]').val(file_type_id); - parent.find('select[name*="file_type_id"]').trigger('chosen:updated'); + parent.find('select[name*="file_type_id"]').val(file_type_id).trigger('chosen:updated'); }); }; diff --git a/app/controllers/execution_environments_controller.rb b/app/controllers/execution_environments_controller.rb index bd103a17..23e73768 100644 --- a/app/controllers/execution_environments_controller.rb +++ b/app/controllers/execution_environments_controller.rb @@ -29,7 +29,7 @@ class ExecutionEnvironmentsController < ApplicationController end def execution_environment_params - params[:execution_environment].permit(:docker_image, :exposed_ports, :editor_mode, :file_extension, :help, :indent_size, :name, :permitted_execution_time, :pool_size, :run_command, :test_command, :testing_framework).merge(user_id: current_user.id, user_type: current_user.class.name) + params[:execution_environment].permit(:docker_image, :exposed_ports, :editor_mode, :file_extension, :file_type_id, :help, :indent_size, :name, :permitted_execution_time, :pool_size, :run_command, :test_command, :testing_framework).merge(user_id: current_user.id, user_type: current_user.class.name) end private :execution_environment_params diff --git a/app/models/execution_environment.rb b/app/models/execution_environment.rb index 650a2965..dc244386 100644 --- a/app/models/execution_environment.rb +++ b/app/models/execution_environment.rb @@ -6,6 +6,7 @@ class ExecutionEnvironment < ActiveRecord::Base after_initialize :set_default_values has_many :exercises + belongs_to :file_type has_many :hints scope :with_exercises, -> { where('id IN (SELECT execution_environment_id FROM exercises)') } diff --git a/app/models/file_type.rb b/app/models/file_type.rb index 831d8aba..87c34087 100644 --- a/app/models/file_type.rb +++ b/app/models/file_type.rb @@ -7,6 +7,7 @@ class FileType < ActiveRecord::Base after_initialize :set_default_values + has_many :execution_environments has_many :files validates :binary, inclusion: {in: [true, false]} diff --git a/app/views/code_ocean/files/_form.html.slim b/app/views/code_ocean/files/_form.html.slim index fafe9529..07dd3355 100644 --- a/app/views/code_ocean/files/_form.html.slim +++ b/app/views/code_ocean/files/_form.html.slim @@ -7,6 +7,6 @@ = 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') + = f.collection_select(:file_type_id, FileType.where(binary: false).order(:name), :id, :name, {selected: @exercise.execution_environment.file_type.try(:id)}, class: 'form-control') = f.hidden_field(:context_id) .actions = render('shared/submit_button', f: f, object: CodeOcean::File.new) diff --git a/app/views/execution_environments/_form.html.slim b/app/views/execution_environments/_form.html.slim index 126fa1b4..8c17343b 100644 --- a/app/views/execution_environments/_form.html.slim +++ b/app/views/execution_environments/_form.html.slim @@ -3,6 +3,9 @@ .form-group = f.label(:name) = f.text_field(:name, class: 'form-control', required: true) + .form-group + = f.label(:file_type_id) + = f.collection_select(:file_type_id, FileType.all.order(:name), :id, :name, {include_blank: true}, class: 'form-control') .form-group = f.label(:docker_image) |   diff --git a/app/views/execution_environments/show.html.slim b/app/views/execution_environments/show.html.slim index 9c4f0652..60c01364 100644 --- a/app/views/execution_environments/show.html.slim +++ b/app/views/execution_environments/show.html.slim @@ -4,6 +4,7 @@ h1 = row(label: 'execution_environment.name', value: @execution_environment.name) = row(label: 'execution_environment.user', value: link_to(@execution_environment.author, @execution_environment.author)) += row(label: 'execution_environment.file_type', value: @execution_environment.file_type.present? ? link_to(@execution_environment.file_type, @execution_environment.file_type) : nil) - [:docker_image, :exposed_ports, :permitted_execution_time, :pool_size, :run_command, :test_command].each do |attribute| = row(label: "execution_environment.#{attribute}", value: @execution_environment.send(attribute)) = row(label: 'execution_environment.testing_framework', value: @testing_framework_adapter.try(:framework_name)) diff --git a/config/locales/de.yml b/config/locales/de.yml index 2fe0b704..b395f15b 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -10,6 +10,8 @@ de: execution_environment: docker_image: Docker-Image exposed_ports: Zugängliche Ports + file_type: Standard-Dateityp + file_type_id: Standard-Dateityp help: Hilfetext name: Name permitted_execution_time: Erlaubte Ausführungszeit (in Sekunden) diff --git a/config/locales/en.yml b/config/locales/en.yml index 761505e3..f932f033 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -10,6 +10,8 @@ en: execution_environment: docker_image: Docker Image exposed_ports: Exposed Ports + file_type: Default File Type + file_type_id: Default File Type help: Help Text name: Name permitted_execution_time: Permitted Execution Time (in Seconds) diff --git a/db/migrate/20150310150712_add_file_type_id_to_execution_environments.rb b/db/migrate/20150310150712_add_file_type_id_to_execution_environments.rb new file mode 100644 index 00000000..d57907c2 --- /dev/null +++ b/db/migrate/20150310150712_add_file_type_id_to_execution_environments.rb @@ -0,0 +1,5 @@ +class AddFileTypeIdToExecutionEnvironments < ActiveRecord::Migration + def change + add_reference :execution_environments, :file_type + end +end diff --git a/db/schema.rb b/db/schema.rb index 9d0c1bf2..94538e85 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20150204080832) do +ActiveRecord::Schema.define(version: 20150310150712) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -45,6 +45,7 @@ ActiveRecord::Schema.define(version: 20150204080832) do t.integer "user_id" t.string "user_type" t.integer "pool_size" + t.integer "file_type_id" end create_table "exercises", force: true do |t| diff --git a/spec/factories/execution_environment.rb b/spec/factories/execution_environment.rb index b47e2e03..5ac07497 100644 --- a/spec/factories/execution_environment.rb +++ b/spec/factories/execution_environment.rb @@ -2,6 +2,7 @@ FactoryGirl.define do factory :coffee_script, class: ExecutionEnvironment do created_by_teacher docker_image 'hklement/ubuntu-coffee:latest' + association :file_type, factory: :dot_coffee help name 'CoffeeScript' permitted_execution_time 10.seconds @@ -13,6 +14,7 @@ FactoryGirl.define do factory :html, class: ExecutionEnvironment do created_by_teacher docker_image 'hklement/ubuntu-html:latest' + association :file_type, factory: :dot_html help name 'HTML5' permitted_execution_time 10.seconds @@ -26,6 +28,7 @@ FactoryGirl.define do factory :java, class: ExecutionEnvironment do created_by_teacher docker_image 'hklement/ubuntu-java:latest' + association :file_type, factory: :dot_java help name 'Java 8' permitted_execution_time 10.seconds @@ -39,6 +42,7 @@ FactoryGirl.define do factory :jruby, class: ExecutionEnvironment do created_by_teacher docker_image 'hklement/ubuntu-jruby:latest' + association :file_type, factory: :dot_rb help name 'JRuby 1.7' permitted_execution_time 10.seconds @@ -52,6 +56,7 @@ FactoryGirl.define do factory :node_js, class: ExecutionEnvironment do created_by_teacher docker_image 'hklement/ubuntu-node:latest' + association :file_type, factory: :dot_js help name 'Node.js' permitted_execution_time 10.seconds @@ -63,6 +68,7 @@ FactoryGirl.define do factory :python, class: ExecutionEnvironment do created_by_teacher docker_image 'hklement/ubuntu-python:latest' + association :file_type, factory: :dot_py help name 'Python 3.4' permitted_execution_time 10.seconds @@ -76,6 +82,7 @@ FactoryGirl.define do factory :ruby, class: ExecutionEnvironment do created_by_teacher docker_image 'hklement/ubuntu-ruby:latest' + association :file_type, factory: :dot_rb help name 'Ruby 2.2' permitted_execution_time 10.seconds @@ -89,6 +96,7 @@ FactoryGirl.define do factory :sinatra, class: ExecutionEnvironment do created_by_teacher docker_image 'hklement/ubuntu-sinatra:latest' + association :file_type, factory: :dot_rb exposed_ports '4567' help name 'Sinatra' @@ -103,6 +111,7 @@ FactoryGirl.define do factory :sqlite, class: ExecutionEnvironment do created_by_teacher docker_image 'hklement/ubuntu-sqlite:latest' + association :file_type, factory: :dot_sql help name 'SQLite' permitted_execution_time 1.minute