Checkbox to remove articles not in upload

This commit is contained in:
wvengen 2015-04-10 19:41:08 +02:00
parent 6ce1b7f928
commit 4d1e102f47
5 changed files with 32 additions and 14 deletions

View File

@ -138,7 +138,8 @@ class ArticlesController < ApplicationController
# Update articles from a spreadsheet
def parse_upload
uploaded_file = params[:articles]['file']
options = {filename: uploaded_file.original_filename}
outlist_absent = (params[:articles]['outlist_absent'] == '1')
options = {filename: uploaded_file.original_filename, outlist_absent: outlist_absent}
@updated_article_pairs, @outlisted_articles, @new_articles = @supplier.sync_from_file uploaded_file.tempfile, options
if @updated_article_pairs.empty? && @outlisted_articles.empty? && @new_articles.empty?
redirect_to supplier_articles_path(@supplier), :notice => I18n.t('articles.controller.parse_upload.notice')

View File

@ -61,6 +61,7 @@ class Supplier < ActiveRecord::Base
# @param options [Hash] Options passed to {FoodsoftFile#parse} except when listed here.
# @option options [Boolean] :outlist_absent Set to +true+ to remove articles not in spreadsheet.
def sync_from_file(file, options={})
all_order_numbers = []
updated_article_pairs, outlisted_articles, new_articles = [], [], []
FoodsoftFile::parse file, options do |status, new_attrs, line|
article = articles.undeleted.where(order_number: new_attrs[:order_number]).first
@ -86,6 +87,11 @@ class Supplier < ActiveRecord::Base
# @todo move I18n key to model
raise I18n.t('articles.model.error_parse', :msg => status, :line => line.to_s)
end
all_order_numbers << article.order_number if article
end
if options[:outlist_absent]
outlisted_articles += articles.undeleted.where.not(id: all_order_numbers+[nil])
end
return [updated_article_pairs, outlisted_articles, new_articles]
end
@ -126,4 +132,3 @@ class Supplier < ActiveRecord::Base
end
end
end

View File

@ -69,9 +69,17 @@
%p= t '.text_2'
= form_for :articles, :url => parse_upload_supplier_articles_path(@supplier),
:html => { :multipart => true } do |f|
%label(for="articles_file")= t '.file_label'
= f.file_field "file"
:html => { multipart: true, class: "form-horizontal" } do |f|
.control-group
%label(for="articles_file")= t '.file_label'
= f.file_field "file"
.control-group
%label(for="articles_outlist_absent")
= f.check_box "outlist_absent"
= t '.outlist_absent'
.form-actions
= submit_tag t('.submit'), class: 'btn btn-primary'
= link_to t('ui.or_cancel'), supplier_articles_path(@supplier)

View File

@ -438,6 +438,7 @@ en:
reserved: "(Reserved)"
status: Status (x=skip)
file_label: Please choose a compatible file
outlist_absent: Delete articles not in uploaded file
sample:
juices: Juices
nuts: Nuts

View File

@ -85,14 +85,17 @@ describe ArticlesController, :type => :feature do
end
end
#describe "can remove an existing article" do
# let!(:article) { create :article, supplier: supplier, name: 'Foobar', order_number: 99999 }
# it do
# find('input[type="submit"]').click
# expect(find("#outlisted_articles_#{article.id}")).to be_present
# find('input[type="submit"]').click
# expect(Article.where(id: article.id)).to be_empty
# end
#end
describe "can remove an existing article" do
let!(:article) { create :article, supplier: supplier, name: 'Foobar', order_number: 99999 }
it do
check('articles_outlist_absent')
find('input[type="submit"]').click
expect(find("#outlisted_articles_#{article.id}", visible: :all)).to be_present
all("tr select > option")[1].select_option
find('input[type="submit"]').click
expect(article.reload.deleted?).to be true
end
end
end
end