Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Existing plant lines upload #781

Merged
merged 5 commits into from
Feb 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}"))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works - I was able to see a 500 error when I uploaded a wrong file. What is wrong, however, that I got that 500 in the first place. I tried to upload a completely wrong file and I caught BIP trying to select on nil:

NoMethodError - private method `select' called for nil:NilClass:
  app/decorators/submission_plant_lines_upload_decorator.rb:22:in `uploaded_new_plant_lines'
  app/decorators/submission_plant_lines_upload_decorator.rb:4:in `as_json'

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you remember what kind of file you uploaded? I only managed to get this 500 response using a badly encoded file, otherwise I get regular error message such as "File content type is invalid".

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did it with two files, one was even a CSV, only a completely different one (than expected). I also remember I selected nothing on the 2nd step. The 3rd step was also empty when I uploaded the file.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, fixed.


@$('.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