Skip to content

Commit

Permalink
Merge pull request #781 from TGAC/existing-plant-lines-upload
Browse files Browse the repository at this point in the history
Existing plant lines upload
  • Loading branch information
kammerer authored Feb 28, 2018
2 parents a25ea9a + f014dbe commit a9990f5
Show file tree
Hide file tree
Showing 13 changed files with 364 additions and 162 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ window.PlantPopulationSubmission = class PlantPopulationSubmission extends Submi
@$('.add-new-plant-line-for-list').on 'click', (event) =>
@validateNewPlantLineForList((plantLineData) =>
@appendToSelectedPlantLineLists(plantLineData)
@appendNewPlantLineData(plantLineData)
@dirtyTracker.resetContext("new-plant-line")
)

Expand Down Expand Up @@ -88,21 +89,26 @@ window.PlantPopulationSubmission = class PlantPopulationSubmission extends Submi

hasNewPlantLine: (data) =>
fieldName = "submission[content][new_plant_lines][][plant_line_name]"
$("[name='#{fieldName}'][value=#{data.plant_line_name}]").length > 0
selector = "[name='#{fieldName}'][value='#{data.plant_line_name}']"
$(selector).length > 0

hasExistingPlantLine: (data) =>
$("#submission_content_plant_line_list option[value='#{data.id}']").length > 0

appendToSelectedPlantLineLists: (data) =>
$select = @$('.plant-line-list')
selectedValues = $select.val() || []

$option = $('<option></option>').attr(value: data.plant_line_name).text(data.plant_line_name)
$option = $('<option></option>').attr(value: data.id || data.plant_line_name).text(data.plant_line_name)
$select.append($option)

selectedValues.push(data.plant_line_name)
selectedValues.push(data.id || data.plant_line_name)
$select.val(selectedValues)
$select.trigger('change') # required to notify select2 about changes, see https://github.com/select2/select2/issues/3057

appendNewPlantLineData: (data) =>
# add all PL attributes to DOM so it can be sent with form
$form = @$el.find('form')
$form = @$el.find('form.edit-submission')
$container = $('<div></div>').attr(id: @newPlantLineForListContainerId(data.plant_line_name), class: "new-plant-line-attrs")
$container.appendTo($form)

Expand All @@ -116,13 +122,13 @@ window.PlantPopulationSubmission = class PlantPopulationSubmission extends Submi
$("##{@newPlantLineForListContainerId(plant_line_name)}").remove()

$select = @$('.plant-line-list')
$option = $select.find("option[value=#{plant_line_name}]")
$option = $select.find("option[value='#{plant_line_name}']")
$option.remove()

this.resetNewPlantLines() if $select.find("option").length == 0

resetNewPlantLines: =>
$form = @$el.find('form')
$form = @$el.find('form.edit-submission')
$form.find(".new-plant-line-attrs").remove()

$container = $('<div></div>').attr(id: @newPlantLineForListContainerId(""), class: "new-plant-line-attrs")
Expand Down Expand Up @@ -158,8 +164,16 @@ window.PlantPopulationSubmission = class PlantPopulationSubmission extends Submi
@$('.uploaded-plant-lines .parser-errors').addClass('hidden')
@$('.uploaded-plant-lines .parser-errors').text('')

$.each(data.result.uploaded_plant_lines, (_, uploadedPlantLine) =>
@appendToSelectedPlantLineLists(uploadedPlantLine) unless @hasNewPlantLine(uploadedPlantLine)

$.each(data.result.uploaded_new_plant_lines, (_, plantLineData) =>
@removeNewPlantLineFromList(plantLineData.plant_line_name) if @hasNewPlantLine(plantLineData)
@appendToSelectedPlantLineLists(plantLineData)
@appendNewPlantLineData(plantLineData)
)

$.each(data.result.uploaded_existing_plant_lines, (_, plantLineData) =>
@removeNewPlantLineFromList(plantLineData.plant_line_name) if @hasNewPlantLine(plantLineData)
@appendToSelectedPlantLineLists(plantLineData) unless @hasExistingPlantLine(plantLineData)
)

fail: (event, data) =>
Expand All @@ -173,6 +187,11 @@ window.PlantPopulationSubmission = class PlantPopulationSubmission extends Submi
$li = $("<li></li>").text(error)
$errors.find("ul").append($li)
)
else
@$('.fileinput-button').removeClass('disabled')

$errors = $(".errors").text("").removeClass('hidden').append("<ul></ul>")
$errors.find("ul").append($("<li></li>").text("Unexpected server response: #{data.jqXHR.status} #{data.jqXHR.statusText}"))

@$('.delete-plant-lines-upload').on 'ajax:success', (data, status, xhr) =>
@$('.fileinput').removeClass('hidden')
Expand Down
10 changes: 10 additions & 0 deletions app/assets/javascripts/components/plant_trial_submission.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ window.PlantTrialSubmission = class PlantTrialSubmission extends Submission
$li = $("<li></li>").text(error)
$errors.find("ul").append($li)
)
else
@$('.fileinput-button').removeClass('disabled')

$errors = $(".errors").text("").removeClass('hidden').append("<ul></ul>")
$errors.find("ul").append($("<li></li>").text("Unexpected server response: #{data.jqXHR.status} #{data.jqXHR.statusText}"))

@$('.delete-trait-scores-upload').on 'ajax:success', (data, status, xhr) =>
@$('.fileinput').removeClass('hidden')
Expand Down Expand Up @@ -128,6 +133,11 @@ window.PlantTrialSubmission = class PlantTrialSubmission extends Submission
$li = $("<li></li>").text(error)
$errors.find("ul").append($li)
)
else
@$('.fileinput-button').removeClass('disabled')

$errors = $(".errors").text("").removeClass('hidden').append("<ul></ul>")
$errors.find("ul").append($("<li></li>").text("Unexpected server response: #{data.jqXHR.status} #{data.jqXHR.statusText}"))

@$('.delete-layout-upload').on 'ajax:success', (data, status, xhr) =>
@$('.fileinput').removeClass('hidden')
Expand Down
3 changes: 2 additions & 1 deletion app/assets/javascripts/submissions.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
$ ->
new PlantPopulationSubmission('.edit-population-submission').init()
window.pp = new PlantPopulationSubmission('.edit-population-submission')
window.pp.init()
new PlantTrialSubmission('.edit-trial-submission').init()

23 changes: 21 additions & 2 deletions app/decorators/submission_plant_lines_upload_decorator.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
class SubmissionPlantLinesUploadDecorator < SubmissionUploadDecorator
def as_json(*)
super.merge(uploaded_plant_lines: uploaded_plant_lines)
super.merge(uploaded_existing_plant_lines: uploaded_existing_plant_lines,
uploaded_new_plant_lines: uploaded_new_plant_lines)
end

private

def uploaded_plant_lines
submission.content.uploaded_plant_lines
submission.content.uploaded_plant_lines || []
end

def new_plant_lines
submission.content.new_plant_lines || []
end

def uploaded_existing_plant_lines
PlantLine.
visible(submission.user_id).
where(plant_line_name: uploaded_plant_lines).
pluck(:id, :plant_line_name).
map { |id, plant_line_name| { id: id, plant_line_name: plant_line_name } }
end

def uploaded_new_plant_lines
new_plant_lines.select do |plant_line_attrs|
uploaded_plant_lines.include?(plant_line_attrs.fetch("plant_line_name"))
end
end
end
2 changes: 1 addition & 1 deletion app/forms/submissions/population/step03_content_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Step03ContentForm < PlantPopulationForm
# Do not allow :new_plant_lines to include existing :plant_line_name
validate do
new_plant_lines.each do |new_plant_line|
if plant_line_exists?("plant_line_name ILIKE ?", new_plant_line.plant_line_name)
if plant_line_exists?(plant_line_name: new_plant_line.plant_line_name)
errors.add(:new_plant_lines, :taken, name: new_plant_line.plant_line_name)
end
end
Expand Down
Loading

0 comments on commit a9990f5

Please sign in to comment.