Skip to content

Commit

Permalink
Allow to freely override PLs via file upload
Browse files Browse the repository at this point in the history
  • Loading branch information
kammerer committed Feb 27, 2018
1 parent e2ced0f commit 68dc259
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ 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
Expand All @@ -107,7 +108,7 @@ window.PlantPopulationSubmission = class PlantPopulationSubmission extends Submi

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 @@ -127,7 +128,7 @@ window.PlantPopulationSubmission = class PlantPopulationSubmission extends Submi
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 @@ -163,13 +164,15 @@ window.PlantPopulationSubmission = class PlantPopulationSubmission extends Submi
@$('.uploaded-plant-lines .parser-errors').addClass('hidden')
@$('.uploaded-plant-lines .parser-errors').text('')


$.each(data.result.uploaded_new_plant_lines, (_, plantLineData) =>
unless @hasNewPlantLine(plantLineData)
@appendToSelectedPlantLineLists(plantLineData)
@appendNewPlantLineData(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)
)

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()

47 changes: 31 additions & 16 deletions app/services/submission/plant_line_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,7 @@ def call
@upload.log "Detected wrong file format - please make sure the file is not encoded (e.g. you are uploading an xls, instead of a text file)."
end

if @upload.errors.empty?
@upload.submission.content.append(:step03,
plant_line_list: @plant_line_names,
new_plant_lines: @new_plant_lines,
new_plant_varieties: @new_plant_varieties,
new_plant_accessions: @new_plant_accessions)
@upload.submission.content.update(:step03, uploaded_plant_lines: @plant_line_names)
@upload.submission.save!
@upload.submission.content.save!
end
update_submission_content if @upload.errors.empty?
end

private
Expand Down Expand Up @@ -79,6 +70,36 @@ def parse_plant_lines
@upload.log "Detected #{@new_plant_varieties.size} plant line(s) of new (i.e. not existing in BIP) plant variety(ies)."
end

def update_submission_content
remove_overriden_content
append_uploaded_content

@upload.submission.save!
end

def remove_overriden_content
current_new_plant_lines = @upload.submission.content.new_plant_lines || []
current_new_plant_varieties = @upload.submission.content.new_plant_varieties || {}
current_new_plant_accessions = @upload.submission.content.new_plant_accessions || {}

overriden_new_plant_lines =
current_new_plant_lines.select { |npl| @plant_line_names.include?(npl.fetch("plant_line_name")) }

@upload.submission.content.update(:step03,
new_plant_lines: current_new_plant_lines - overriden_new_plant_lines,
new_plant_varieties: current_new_plant_varieties.except(*@plant_line_names),
new_plant_accessions: current_new_plant_accessions.except(*@plant_line_names))
end

def append_uploaded_content
@upload.submission.content.append(:step03,
plant_line_list: @plant_line_names,
new_plant_lines: @new_plant_lines,
new_plant_varieties: @new_plant_varieties,
new_plant_accessions: @new_plant_accessions)
@upload.submission.content.update(:step03, uploaded_plant_lines: @plant_line_names)
end

def csv
@csv ||= CSV.new(input)
end
Expand All @@ -97,8 +118,6 @@ def correct_input?(row)

if reused_plant_line?(plant_line_name)
@upload.log "Ignored row for #{plant_line_name} since a plant line with that name is already defined in the uploaded file."
elsif current_new_plant_lines.include?(plant_line_name)
@upload.log "Ignored row for #{plant_line_name} since a plant line with that name is already defined. Please clear the 'Plant line list' field before re-uploading a CSV file."
elsif existing_plant_line?(plant_line_name) && !existing_plant_line_match?(pl_attrs, pv_attrs)
@upload.log "Ignored row for #{plant_line_name} since a plant line with that name is already present in BIP "\
"but uploaded data does not match existing record."
Expand Down Expand Up @@ -192,10 +211,6 @@ def current_plant_lines
@current_plant_lines ||= @upload.submission.content.plant_line_list || []
end

def current_new_plant_lines
@current_new_plant_lines ||= (@upload.submission.content.new_plant_lines || []).map { |pl| pl["plant_line_name"] }
end

def header_columns
[
"Species",
Expand Down
2 changes: 1 addition & 1 deletion app/views/submissions/steps/population/_step03.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,6 @@
%button.btn.btn-primary.add-new-plant-line-for-list{type: 'button'} Confirm
- @content.new_plant_lines.each do |plant_line|
%div{ id: "new-plant-line-#{plant_line.plant_line_name.split.join('-').downcase}" }
%div{ id: "new-plant-line-#{plant_line.plant_line_name.split.join('-').downcase}", class: "new-plant-line-attrs" }
- @content.class.new_plant_line_properties.each do |attr|
= hidden_field_tag "submission[content][new_plant_lines][][#{attr}]", plant_line.send(attr)
57 changes: 32 additions & 25 deletions spec/services/submission/plant_line_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,52 +140,59 @@
end

context "override of previously defined content" do
before { allow(subject).to receive(:parse_header) }

it 'allows override of existing plant line with existing plant line' do
pl = create(:plant_line)

submission.content.update(:step03, plant_line_list: pl.plant_line_name)
submission.content.update(:step03, plant_line_list: [pl.plant_line_name])
submission.save!

input_is "#{pl.taxonomy_term.name},,,#{pl.plant_line_name}"
subject.send(:parse_plant_lines)
expect(subject.plant_line_names).to eq [pl.plant_line_name]
expect(subject.new_plant_lines).to eq []
subject.call
expect(submission.reload.content.plant_line_list).to eq [pl.plant_line_name]
expect(submission.content.new_plant_varieties).to be_blank
expect(submission.content.new_plant_accessions).to be_blank
end

it 'blocks override of new plant line with new plant line' do
submission.content.update(:step03, plant_line_list: ["new-pl"],
new_plant_lines: [{ plant_line_name: "new-pl",
plant_variety_name: "new-pv",
taxonomy_term: "Brassica napus" }])
it 'allows override of new plant line with new plant line' do
submission.content.update(:step03,
plant_line_list: ["new-pl"],
new_plant_lines: [{ plant_line_name: "new-pl", plant_variety_name: "new-pv", taxonomy_term: "Brassica napus" }],
new_plant_varieties: { "new-pl" => { "plant_variety_name" => "new-pv" } },
new_plant_accessions: { "new-pl" => { "plant_accession" => "new-pa" } }
)
submission.save!

input_is "Brassica napus,,,new-pl"
subject.send(:parse_plant_lines)
input_is "Brassica napus,new-pv-2,,new-pl"
subject.call
expect(upload.errors).to be_blank
expect(submission.reload.content.plant_line_list).to eq ["new-pl"]
expect(submission.content.new_plant_lines).
to eq [{ "plant_line_name" => "new-pl", "plant_variety_name" => "new-pv", "taxonomy_term" => "Brassica napus" }]
to eq [{ "plant_line_name" => "new-pl", "plant_variety_name" => "new-pv-2", "taxonomy_term" => "Brassica napus" }]

expect(upload.logs).
to include "Ignored row for new-pl since a plant line with that name is already defined. "\
"Please clear the 'Plant line list' field before re-uploading a CSV file."
expect(submission.content.new_plant_varieties).to eq("new-pl" => { "plant_variety_name" => "new-pv-2" })
expect(submission.content.new_plant_accessions).to be_blank
end

it 'blocks override of new plant line by existing plant line' do
it 'allows override of new plant line by existing plant line' do
pl = create(:plant_line)

submission.content.update(:step03, plant_line_list: [pl.plant_line_name],
new_plant_lines: [{ plant_line_name: pl.plant_line_name,
taxonomy_term: "Brassica napus" }])
submission.content.update(:step03,
plant_line_list: [pl.plant_line_name],
new_plant_lines: [{ plant_line_name: pl.plant_line_name, plant_variety_name: "new-pv", taxonomy_term: "Brassica napus" }],
new_plant_varieties: { pl.plant_line_name => { "plant_variety_name" => "new-pv" } },
new_plant_accessions: { pl.plant_line_name => { "plant_accession" => "new-pa" } }
)
submission.save!

input_is "#{pl.taxonomy_term.name},,,#{pl.plant_line_name}"
subject.send(:parse_plant_lines)
subject.call
expect(upload.errors).to be_blank
expect(submission.reload.content.plant_line_list).to eq [pl.plant_line_name]
expect(submission.content.new_plant_lines).to eq [{ "plant_line_name" => pl.plant_line_name,
"taxonomy_term" => "Brassica napus" }]
expect(upload.logs).
to include "Ignored row for #{pl.plant_line_name} since a plant line with that name is already defined. "\
"Please clear the 'Plant line list' field before re-uploading a CSV file."
expect(submission.content.new_plant_lines).to be_blank
expect(submission.content.new_plant_varieties).to be_blank
expect(submission.content.new_plant_accessions).to be_blank
end
end

Expand Down

0 comments on commit 68dc259

Please sign in to comment.