Skip to content

Commit 097234e

Browse files
committed
Add ProFormA validity checks and disable exporting not invalid
1 parent a568d40 commit 097234e

File tree

9 files changed

+110
-4
lines changed

9 files changed

+110
-4
lines changed

app/assets/stylesheets/tasks.css.scss

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,16 @@
110110
margin-bottom: 0 !important;
111111
}
112112
}
113+
.button-box{
114+
115+
.btn.disabled{
116+
border: 1px solid lightgray;
117+
}
118+
}
119+
120+
.disabled-btn-wrapper {
121+
width: 100%;
122+
}
113123

114124
.completeness-checklist-container {
115125
background: white;

app/controllers/task_contributions_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def show
4545
@files = @task.files
4646
@tests = @task.tests
4747
@model_solutions = @task.model_solutions
48+
@proforma_valid = [nil, *ProformaXML::SCHEMA_VERSIONS].index_with {|v| @task.proforma_valid?(schema_version: v) }
4849

4950
@user_rating = @task.ratings&.find_by(user: current_user)&.rating
5051
render 'tasks/show'

app/controllers/tasks_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def show
3838
@tests = @task.tests
3939
@model_solutions = @task.model_solutions
4040

41+
@proforma_valid = [nil, *ProformaXML::SCHEMA_VERSIONS].index_with {|v| @task.proforma_valid?(schema_version: v) }
4142
@user_rating = @task.ratings&.find_by(user: current_user) || Rating.new(Rating::CATEGORIES.index_with {|_category| 0 })
4243
end
4344

app/models/task.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,17 @@ def url_for(action)
259259
Rails.application.routes.url_helpers.url_for(controller:, action:, **id_options, only_path: true)
260260
end
261261

262+
def proforma_valid?(schema_version: nil)
263+
check_versions = schema_version.nil? ? ProformaXML::SCHEMA_VERSIONS : [schema_version]
264+
265+
check_versions.all? do |version|
266+
ProformaService::ExportTask.call(task: self, options: {version:})
267+
true
268+
rescue ProformaXML::PostGenerateValidationError
269+
false
270+
end
271+
end
272+
262273
private
263274

264275
def duplicate_tests(set_parent_id: true)

app/views/tasks/show.html.slim

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -519,13 +519,22 @@
519519
.wrapper
520520
- if policy(@task).download?
521521
.dropdown.btn-group
522-
= button_tag class: 'btn btn-light dropdown-toggle', 'data-bs-toggle': 'dropdown' do
523-
= t('common.button.download_zip')
522+
- if @proforma_valid[nil]
523+
= button_tag class: 'btn btn-light dropdown-toggle', 'data-bs-toggle': 'dropdown' do
524+
= t('common.button.download_zip')
525+
- else
526+
.disabled-btn-wrapper data-bs-toggle='tooltip' title=t('.not_proforma_valid', version: '')
527+
= button_tag class: 'btn btn-light dropdown-toggle disabled', 'data-bs-toggle': 'dropdown' do
528+
= t('common.button.download_zip')
524529
ul.scrollable.dropdown-menu role='menu'
525530
li.dropdown-header = "#{t('common.button.available_versions')}: "
526531
- ProformaXML::SCHEMA_VERSIONS.each do |proforma_version|
527532
li
528-
= link_to proforma_version, download_task_path(@task, version: proforma_version), class: 'btn btn-light dropdown-item', target: '_blank', rel: 'noopener noreferrer'
533+
- if @proforma_valid[proforma_version]
534+
= link_to proforma_version, download_task_path(@task, version: proforma_version), class: 'btn btn-light dropdown-item', target: '_blank', rel: 'noopener noreferrer'
535+
- else
536+
.disabled-btn-wrapper data-bs-toggle='tooltip' title=t('.not_proforma_valid', version: " (#{proforma_version})")
537+
= link_to proforma_version, download_task_path(@task, version: proforma_version), class: 'btn btn-light dropdown-item disabled', target: '_blank', rel: 'noopener noreferrer'
529538

530539
- else
531540
div data-bs-toggle='tooltip' title=unavailable_tooltip data-bs-delay=150
@@ -535,6 +544,7 @@
535544
.dropdown.btn-group
536545
= button_tag class: 'btn btn-light dropdown-toggle', 'data-bs-toggle': 'dropdown' do
537546
= t('.button.export')
547+
538548
ul.scrollable.dropdown-menu role='menu'
539549
li.dropdown-header = "#{t('tasks.show.export_to')}: "
540550
- if current_user.available_account_links.empty?
@@ -543,7 +553,12 @@
543553
- else
544554
- current_user.available_account_links.each do |acc_link|
545555
li
546-
= link_to acc_link.name, export_external_start_task_path(account_link: acc_link), method: :post, remote: true, class: 'dropdown-item export-test'
556+
- if @proforma_valid[acc_link.proforma_version || ProformaXML::SCHEMA_VERSION_LATEST]
557+
= link_to acc_link.name, export_external_start_task_path(account_link: acc_link), method: :post, remote: true, class: 'dropdown-item export-test'
558+
- else
559+
.disabled-btn-wrapper data-bs-toggle='tooltip' title=t('.not_proforma_valid', version: " (#{acc_link.proforma_version || ProformaXML::SCHEMA_VERSION_LATEST})")
560+
= link_to acc_link.name, export_external_start_task_path(account_link: acc_link), method: :post, remote: true, class: 'dropdown-item export-test disabled'
561+
547562
- else
548563
div data-bs-toggle='tooltip' title=unavailable_tooltip data-bs-delay=150
549564
= button_tag class: 'btn btn-outline-dark dropdown-toggle disabled', 'data-bs-toggle': 'dropdown' do

config/locales/de/views/tasks.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ de:
111111
no_license: Keine
112112
no_model_solution_present: Keine Musterlösungen vorhanden
113113
no_tests: Keine Tests enthalten
114+
not_proforma_valid: Die Aufgabe ist nicht in ProFormA%{version} exportierbar
114115
owner_required_tooltip: Sie müssen der Autor der Aufgabe sein, um die Funktion zu verwenden.
115116
remove_task_from_collection_warning: Sind Sie sicher, dass Sie diese Aufgabe aus der Sammlung entfernen wollen?
116117
task_contribution:

config/locales/en/views/tasks.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ en:
111111
no_license: None
112112
no_model_solution_present: No Model Solutions present
113113
no_tests: No Tests included
114+
not_proforma_valid: The task is not exportable in ProFormA%{version}.
114115
owner_required_tooltip: This feature can only be used by the owner of the task.
115116
remove_task_from_collection_warning: Are you sure you want to remove this Task from the Collection?
116117
task_contribution:

spec/controllers/tasks_controller_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,29 @@
219219
expect(response).to redirect_to([contribution.base, contribution])
220220
end
221221
end
222+
223+
context 'when checking proforma validity' do
224+
before do
225+
stub_const('ProformaXML::SCHEMA_VERSIONS', ['2.0'])
226+
227+
task_double = task
228+
229+
allow(Task).to receive(:find).with(task.to_param).and_return(task_double)
230+
231+
allow(task_double).to receive(:proforma_valid?).with(schema_version: nil).and_return(true)
232+
allow(task_double).to receive(:proforma_valid?).with(schema_version: '2.0').and_return(false)
233+
end
234+
235+
it 'assigns proforma_valid to instance variable' do
236+
get_request
237+
expect(assigns(:proforma_valid)).to be_a(Hash)
238+
end
239+
240+
it 'includes all values in proforma_valid hash' do
241+
get_request
242+
expect(assigns(:proforma_valid)).to eql({nil => true, '2.0' => false})
243+
end
244+
end
222245
end
223246
end
224247

spec/models/task_spec.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,4 +666,47 @@
666666
end
667667
end
668668
end
669+
670+
describe '#proforma_valid?' do
671+
subject(:proforma_valid) { task.proforma_valid?(schema_version:) }
672+
673+
let(:task) { create(:task) }
674+
let(:schema_version) { nil }
675+
676+
context 'when the task is valid for all schema versions' do
677+
before do
678+
allow(ProformaService::ExportTask).to receive(:call).and_return(true)
679+
end
680+
681+
it { is_expected.to be true }
682+
end
683+
684+
context 'when the task is invalid for a schema version' do
685+
before do
686+
allow(ProformaService::ExportTask).to receive(:call).and_raise(ProformaXML::PostGenerateValidationError, '["version not supported"]')
687+
end
688+
689+
it { is_expected.to be false }
690+
end
691+
692+
context 'when a specific schema version is provided' do
693+
let(:schema_version) { '2.0' }
694+
695+
context 'when the task is valid for the specified schema version' do
696+
before do
697+
allow(ProformaService::ExportTask).to receive(:call).with(task: task, options: {version: schema_version}).and_return(true)
698+
end
699+
700+
it { is_expected.to be true }
701+
end
702+
703+
context 'when the task is invalid for the specified schema version' do
704+
before do
705+
allow(ProformaService::ExportTask).to receive(:call).with(task: task, options: {version: schema_version}).and_raise(ProformaXML::PostGenerateValidationError, '["version not supported"]')
706+
end
707+
708+
it { is_expected.to be false }
709+
end
710+
end
711+
end
669712
end

0 commit comments

Comments
 (0)