hide fields from the file form if not matching the file's role

This commit is contained in:
Hauke Klement
2015-03-12 17:04:47 +01:00
parent a62d4ad789
commit cd09e19d24
3 changed files with 23 additions and 10 deletions

View File

@ -103,8 +103,14 @@ $(function() {
$(document).on('change', 'select[name$="[role]"]', function() { $(document).on('change', 'select[name$="[role]"]', function() {
var is_test_file = $(this).val() === 'teacher_defined_test'; var is_test_file = $(this).val() === 'teacher_defined_test';
var parent = $(this).parents('.panel'); var parent = $(this).parents('.panel');
parent.find('[name$="[feedback_message]"]').attr('disabled', !is_test_file); var fields = parent.find('.test-related-fields');
parent.find('[name$="[weight]"]').attr('disabled', !is_test_file); if (is_test_file) {
fields.slideDown();
} else {
fields.slideUp();
parent.find('[name$="[feedback_message]"]').val('');
parent.find('[name$="[weight]"]').val(1);
}
}); });
}; };

View File

@ -9,8 +9,9 @@ module CodeOcean
TEACHER_DEFINED_ROLES = ROLES - %w(user_defined_file) TEACHER_DEFINED_ROLES = ROLES - %w(user_defined_file)
after_initialize :set_default_values after_initialize :set_default_values
before_validation :set_ancestor_values, if: :incomplete_descendent? before_validation :clear_weight, unless: :teacher_defined_test?
before_validation :hash_content, if: :content_present? before_validation :hash_content, if: :content_present?
before_validation :set_ancestor_values, if: :incomplete_descendent?
belongs_to :context, polymorphic: true belongs_to :context, polymorphic: true
belongs_to :execution_environment belongs_to :execution_environment
@ -49,6 +50,11 @@ module CodeOcean
file_id || id file_id || id
end end
def clear_weight
self.weight = nil
end
private :clear_weight
def content_present? def content_present?
content? || native_file? content? || native_file?
end end

View File

@ -22,11 +22,12 @@ li.panel.panel-default
label label
= f.check_box(:read_only) = f.check_box(:read_only)
= t('activerecord.attributes.file.read_only') = t('activerecord.attributes.file.read_only')
.form-group .test-related-fields style="display: #{f.object.teacher_defined_test? ? 'initial' : 'none'};"
= f.label(:name, t('activerecord.attributes.file.feedback_message')) .form-group
= f.text_area(:feedback_message, class: 'form-control', disabled: !f.object.teacher_defined_test?, maxlength: 255) = f.label(:name, t('activerecord.attributes.file.feedback_message'))
.help-block = t('.hints.feedback_message') = f.text_area(:feedback_message, class: 'form-control', maxlength: 255)
.form-group .help-block = t('.hints.feedback_message')
= f.label(:role, t('activerecord.attributes.file.weight')) .form-group
= f.number_field(:weight, class: 'form-control', disabled: !f.object.teacher_defined_test?, min: 1, step: 'any') = f.label(:role, t('activerecord.attributes.file.weight'))
= f.number_field(:weight, class: 'form-control', min: 1, step: 'any')
= render('code_field', attribute: :content, form: f, label: t('activerecord.attributes.file.content')) = render('code_field', attribute: :content, form: f, label: t('activerecord.attributes.file.content'))