Only show file templates which are available for the selected file type

This commit is contained in:
Maximilian Grundke
2016-06-10 17:48:04 +02:00
parent c10b07690a
commit 4d2676fea7
7 changed files with 42 additions and 1 deletions

View File

@ -148,6 +148,22 @@ $(function() {
});
};
var updateFileTemplates = function(fileType) {
var jqxhr = $.ajax({
url: '/file_templates/by_file_type/' + fileType + '.json',
dataType: 'json'
});
jqxhr.done(function(response) {
var noTemplateLabel = $('#noTemplateLabel').data('text');
var options = "<option value>" + noTemplateLabel + "</option>";
for (var i = 0; i < response.length; i++) {
options += "<option value='" + response[i].id + "'>" + response[i].name + "</option>"
}
$("#code_ocean_file_file_template_id").find('option').remove().end().append($(options));
});
jqxhr.fail(ajaxError);
}
if ($.isController('exercises')) {
if ($('table').isPresent()) {
enableBatchUpdate();
@ -162,6 +178,10 @@ $(function() {
inferFileAttributes();
observeFileRoleChanges();
overrideTextareaTabBehavior();
} else if ($('#files.jstree').isPresent()) {
var fileTypeSelect = $('#code_ocean_file_file_type_id');
fileTypeSelect.on("change", function() {updateFileTemplates(fileTypeSelect.val())});
updateFileTemplates(fileTypeSelect.val());
}
toggleCodeHeight();
if (window.hljs) {

View File

@ -6,6 +6,14 @@ class FileTemplatesController < ApplicationController
end
private :authorize!
def by_file_type
@file_templates = FileTemplate.where(:file_type_id => params[:file_type_id])
authorize!
respond_to do |format|
format.json { render :show, status: :ok, json: @file_templates.to_json }
end
end
# GET /file_templates
# GET /file_templates.json
def index

View File

@ -4,4 +4,8 @@ class FileTemplatePolicy < AdminOnlyPolicy
everyone
end
def by_file_type?
everyone
end
end

View File

@ -12,4 +12,5 @@
= f.label(:file_template_id, t('activerecord.attributes.file.file_template_id'))
= f.collection_select(:file_template_id, FileTemplate.all.order(:name), :id, :name, {:include_blank => true}, class: 'form-control')
= f.hidden_field(:context_id)
.hidden#noTemplateLabel data-text=t('file_template.no_template_label')
.actions = render('shared/submit_button', f: f, object: CodeOcean::File.new)

View File

@ -432,3 +432,5 @@ de:
next_label: 'Nächste Seite &#8594;'
page_gap: '&hellip;'
previous_label: '&#8592; Vorherige Seite'
file_template:
no_template_label: "Leere Datei"

View File

@ -432,3 +432,5 @@ en:
next_label: 'Next Page &#8594;'
page_gap: '&hellip;'
previous_label: '&#8592; Previous Page'
file_template:
no_template_label: "Empty File"

View File

@ -1,7 +1,11 @@
FILENAME_REGEXP = /[\w\.]+/ unless Kernel.const_defined?(:FILENAME_REGEXP)
Rails.application.routes.draw do
resources :file_templates
resources :file_templates do
collection do
get 'by_file_type/:file_type_id', as: :by_file_type, to: :by_file_type
end
end
resources :code_harbor_links
resources :request_for_comments
get '/my_request_for_comments', as: 'my_request_for_comments', to: 'request_for_comments#get_my_comment_requests'