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 # Update articles from a spreadsheet
def parse_upload def parse_upload
uploaded_file = params[:articles]['file'] 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 @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? 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') 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. # @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. # @option options [Boolean] :outlist_absent Set to +true+ to remove articles not in spreadsheet.
def sync_from_file(file, options={}) def sync_from_file(file, options={})
all_order_numbers = []
updated_article_pairs, outlisted_articles, new_articles = [], [], [] updated_article_pairs, outlisted_articles, new_articles = [], [], []
FoodsoftFile::parse file, options do |status, new_attrs, line| FoodsoftFile::parse file, options do |status, new_attrs, line|
article = articles.undeleted.where(order_number: new_attrs[:order_number]).first 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 # @todo move I18n key to model
raise I18n.t('articles.model.error_parse', :msg => status, :line => line.to_s) raise I18n.t('articles.model.error_parse', :msg => status, :line => line.to_s)
end 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 end
return [updated_article_pairs, outlisted_articles, new_articles] return [updated_article_pairs, outlisted_articles, new_articles]
end end
@ -126,4 +132,3 @@ class Supplier < ActiveRecord::Base
end end
end end
end end

View file

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

View file

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

View file

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