Skip to content

Commit 9e1a0f9

Browse files
committed
More adjustments for validations of task arrays.
1 parent 2f57f7c commit 9e1a0f9

File tree

3 files changed

+37
-21
lines changed

3 files changed

+37
-21
lines changed

BrainPortal/app/controllers/tasks_controller.rb

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -405,23 +405,28 @@ def create #:nodoc:
405405
return
406406
end
407407

408-
# Create a bunch of tasks and launch them, either in background or in foreground
409-
tasklist,tl_messages = create_tasklist_from_initial_task(@task) # 'tasklist' messages
408+
# Prepare final list of tasks; from the one maintask object we have,
409+
# we get a full array of clones of that task in tasklist
410+
tasklist,ftl_message = @task.wrapper_final_task_list
410411

411412
# Revalidate everything for all the tasks in the new tasklist
412413
# This is kind of costly, but we have no choices if special validation
413414
# modules have been loaded for after_form().
414415
# As soon as any task in the task list is not valid, we stop checking and
415416
# return to the form.
416417
af_messages = "" # 'after_form' messages
417-
tasklist.each do |task|
418+
tasklist.each_with_index do |task,idx|
418419
af_messages += task.wrapper_after_form
419420
af_messages = af_messages.split("\n").uniq.join("\n") # remove duplicate messages, if any
420421
next if task.errors.empty? && task.valid? # move on to next task if everything ok
421422

422-
# Found a faulty task, so inform user and return to launch form.
423-
@task.errors.add(:base, "While creating a set of tasks, one of them was found to be invalid, probably because a file selected as input was not appropriate.")
423+
# Found at least one faulty task, so inform user
424+
task.errors.each { |key,message| @task.errors.add(key,message) } # copy error messages, if any
425+
@task.errors.add(:base, "While creating a set of tasks, at least one of them was found to be invalid, probably because a file selected as input was not appropriate.")
424426
flash.now[:error] += af_messages
427+
428+
# Re-render the form
429+
initialize_common_form_values
425430
respond_to do |format|
426431
format.html { render :action => 'new' }
427432
format.xml { render :xml => @task.errors, :status => :unprocessable_entity }
@@ -430,13 +435,17 @@ def create #:nodoc:
430435
return
431436
end
432437

438+
# Create a bunch of tasks and launch them, either in background or in foreground
439+
tl_messages = create_tasklist_from_initial_task(@task,tasklist)
440+
433441
if tasklist.size == 1
434442
flash[:notice] += "Launching a #{@task.pretty_name} task in background."
435443
else
436444
flash[:notice] += "Launching #{tasklist.size} #{@task.pretty_name} tasks in background."
437445
end
438-
flash[:notice] += "\n#{tk_messages.strip}" if tl_messages.present?
439446
flash[:notice] += "\n#{af_messages.strip}" if af_messages.present?
447+
flash[:notice] += "\n#{ftl_message.strip}" if ftl_message.present?
448+
flash[:notice] += "\n#{tl_messages.strip}" if tl_messages.present?
440449

441450
# Increment the number of times the user has launched this particular tool
442451
tool_id = @task.tool.id
@@ -1417,7 +1426,7 @@ def create_initial_task_from_form(new_task_info, tool_id = nil) #:nodoc:
14171426
# Part of the create() process for a task
14181427
#
14191428
# Code extracted from the old monolithic 'create'
1420-
def create_tasklist_from_initial_task(maintask) #:nodoc:
1429+
def create_tasklist_from_initial_task(maintask,tasklist) #:nodoc:
14211430

14221431
messages = ""
14231432

@@ -1443,14 +1452,6 @@ def create_tasklist_from_initial_task(maintask) #:nodoc:
14431452
messages += "Warning: parallelization cannot be performed until the admin configures a Tool for it.\n"
14441453
end
14451454

1446-
# Prepare final list of tasks; from the one maintask object we have,
1447-
# we get a full array of clones of that task in tasklist
1448-
tasklist,task_list_message = maintask.wrapper_final_task_list
1449-
if task_list_message.present?
1450-
messages += "\n" if messages.present?
1451-
messages += task_list_message
1452-
end
1453-
14541455
# Spawn a background process to launch the tasks.
14551456
# In case of API requests, we don't spawn.
14561457
CBRAIN.spawn_with_active_records_if(! api_request?, :admin, "Spawn Tasks") do
@@ -1524,7 +1525,7 @@ def create_tasklist_from_initial_task(maintask) #:nodoc:
15241525

15251526
end # CBRAIN spawn_if block
15261527

1527-
return tasklist,messages
1528+
return messages
15281529
end
15291530

15301531
# Some useful variables for the views for 'new' and 'edit'

BrainPortal/app/models/boutiques_portal_task.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,10 @@ def final_task_list #:nodoc:
342342
end
343343
end
344344

345+
# Re-introduce the file IDs of the task list, in case the main form
346+
# needs to be re-rendered.
347+
self.params[:interface_userfile_ids] |= task_array_userfiles_ids
348+
345349
return tasklist.flatten
346350
end # When only one file input
347351

@@ -505,8 +509,8 @@ def validateCols(cbcsv,id)
505509
# Ensure that the +input+ parameter is not null and matches a generic tool
506510
# parameter type (:file, :numeric, :string or :flag) before converting the
507511
# parameter's value to the corresponding Ruby type (if appropriate).
508-
# For example, sanitize_param(someinput) where someinput's name is 'deviation'
509-
# and someinput's type is 'numeric' would validate that
512+
# For example, sanitize_param(someinput) where someinput's name is 'deviation'
513+
# and someinput's type is 'numeric' would validate that
510514
# self.params['invoke']['deviation'] is a number and then convert it to a Ruby Float or
511515
# Integer.
512516
#
@@ -548,7 +552,7 @@ def sanitize_param(input)
548552

549553
# Taken userfile names. An error will be raised if two input files have the
550554
# same name.
551-
@taken_files ||= Set.new
555+
@taken_files ||= {}
552556

553557
# Fetch the parameter and convert to an Enumerable if required
554558
values = invoke_params[name]
@@ -608,10 +612,10 @@ def sanitize_param(input)
608612
next nil # remove bad value
609613
end
610614

611-
if @taken_files.include?(file.id)
615+
if @taken_files[file.id].present? && @taken_files[file.id] != input.id
612616
params_errors.add(invokename, ": file name already in use (#{file.name})")
613617
else
614-
@taken_files.add(file.id)
618+
@taken_files[file.id] = input.id
615619
end
616620

617621
end

BrainPortal/app/models/portal_task.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,17 @@ def params_errors
743743
@params_errors_cache
744744
end
745745

746+
# Needed in case of a dup()
747+
def params_errors_clear #:nodoc:
748+
@params_errors_cache = nil
749+
end
750+
751+
def dup #:nodoc:
752+
obj = super
753+
obj.params_errors_clear
754+
obj
755+
end
756+
746757
# This method returns a 'pretty' name for a params attributes.
747758
# This implementation will try to look up a hash table returned
748759
# by the class method pretty_params_names() first, so an

0 commit comments

Comments
 (0)