begin
if CSVUtil.valid_data_from_file?(file)
- line, result = Product.add_by_csv(file)
+ line, result = Product.add_by_csv(file, session[:admin_user].retailer_id)
unless result
line = line + 1
flash[:product_csv_upload_e] = "#{line}行目のデータが不正です。最初からやり直して下さい。"
end
class << self
- def add_by_csv(file)
+ def add_by_csv(file, retailer_id)
line = 0
Product.transaction do
CSV::Reader.parse(file) do |row|
if line != 0
- product = new_by_array(row)
+ product = new_by_array(row, retailer_id)
+ if product.nil?
+ return [line-1, false]
+ end
unless product.save!
return [line-1, false]
end
private
- def new_by_array(arr)
+ def new_by_array(arr, retailer_id)
arr.map! do | val |
Iconv.conv('UTF-8', 'cp932', val)
end
+ # retailer_idの確認
+ product = Product.find_by_id(arr[0].to_i) unless arr[0].blank?
+ if retailer_id != Retailer::DEFAULT_ID && product && product.retailer_id != retailer_id
+ return nil
+ end
#arr[0]が対応しているデータ存在する時、更新、存在しない時、新規作成
- unless !arr[0].blank? && product = Product.find_by_id(arr[0].to_i)
+ unless product
product = Product.new
end
#CSVデータ設定
- set_data(product,arr)
+ set_data(product,arr,retailer_id)
product
end
#CSVデータ設定
- def set_data(product,arr)
+ def set_data(product,arr,retailer_id)
setPermit(product, arr[1])
product.name = arr[2]
product.url = arr[3]
setDelivery_dates(product,arr[30])
setSupplierId(product,arr[31])
# todo: csvupload retailer対応
- setRetailerId(product,arr[32])
+ if retailer_id == Retailer::DEFAULT_ID
+ setRetailerId(product,arr[32])
+ else
+ setRetailerId(product,arr[32],retailer_id)
+ end
end
def setPermit(product, permit)
end
end
- def setRetailerId(product, name)
- if name.blank?
+ def setRetailerId(product, name, retailer_id = nil)
+ if name.blank? && retailer_id.nil?
product.retailer_id = Retailer::DEFAULT_ID
+ elsif retailer_id
+ product.retailer_id = retailer_id
else
r = Retailer.find_by_name(name)
if !r.blank?
describe "POST 'csv_upload'" do
it "should be successful" do
last_customer = Customer.find(:last)
- csv = uploaded_file(File.dirname(__FILE__) + "/../../customer_upload.csv", "text", "customer_upload.csv")
+ csv = uploaded_file(File.dirname(__FILE__) + "/../../csv/customer_upload.csv", "text", "customer_upload.csv")
post 'csv_upload', :upload_file => csv
Customer.find(:last).should_not == last_customer
end
describe "POST 'csv_upload'" do
it "should be successful" do
- csv = uploaded_file(File.dirname(__FILE__) + "/../../order_status_update.csv", "text", "order_status_update.csv")
+ csv = uploaded_file(File.dirname(__FILE__) + "/../../csv/order_status_update.csv", "text", "order_status_update.csv")
post 'csv_upload', :upload_file => csv
flash[:status].should_not be_nil
end
describe "POST 'csv_upload'" do
it "should be successful" do
last_product = Product.find(:last)
- csv = uploaded_file(File.dirname(__FILE__) + "/../../product_sample.csv", "text", "product_sample.csv")
+ csv = uploaded_file(File.dirname(__FILE__) + "/../../csv/product_sample.csv", "text", "product_sample.csv")
post 'csv_upload', :upload_file => csv
Product.find(:last).should_not == last_product
end
+
+ it "other shop should not be successful" do
+ session[:admin_user] = admin_users(:admin18_retailer_id_is_another_shop)
+ last_product = Product.find(:last)
+ csv = uploaded_file(File.dirname(__FILE__) + "/../../csv/product_sample.csv", "text", "product_sample.csv")
+ post 'csv_upload', :upload_file => csv
+ p flash[:product_csv_upload_e]
+ Product.find(:last).should == last_product
+ end
+
+ it "違うショップでもデータが正しければ登録できる" do
+ session[:admin_user] = admin_users(:admin18_retailer_id_is_another_shop)
+ last_product = Product.find(:last)
+ csv = uploaded_file(File.dirname(__FILE__) + "/../../csv/product_sample_other_shop.csv", "text", "product_sample_other_shop.csv")
+ post 'csv_upload', :upload_file => csv
+ p flash[:product_csv_upload_e]
+ Product.find(:last).should_not == last_product
+ end
+
+
end
+
describe "GET 'search' from retailer_fail" do
before(:each) do
session[:admin_user] = admin_users(:admin17_retailer_id_is_fails)
--- /dev/null
+\8f¤\95iID,\8cö\8aJ\90Ý\92è,\96¼\91O,\8eQ\8fÆURL,\88ê\97\97\83R\83\81\83\93\83g,\8fÚ\8d×\83R\83\81\83\93\83g,\83L\81[\83\8f\81[\83h,\8eQ\8dl\8es\8fê\89¿\8ai,\88ê\97\97\81E\83\81\83C\83\93\89æ\91\9cID,\88ê\97\97\81E\83\81\83C\83\93\89æ\91\9c\83R\83\81\83\93\83g,\88ê\97\97\81E\83\81\83C\83\93\89æ\91\9c\83p\83X,\8fÚ\8d×\81E\83\81\83C\83\93\89æ\91\9cID,\8fÚ\8d×\81E\83\81\83C\83\93\89æ\91\9c\83R\83\81\83\93\83g,\8fÚ\8d×\81E\83\81\83C\83\93\89æ\91\9c\83p\83X,\8fÚ\8d×\81E\83\81\83C\83\93\8ag\91å\89æ\91\9cID,\8fÚ\8d×\81E\83\81\83C\83\93\8ag\91å\89æ\91\9c\83R\83\81\83\93\83g,\8fÚ\8d×\81E\83\81\83C\83\93\8ag\91å\89æ\91\9c\83p\83X,\8dw\93ü\90§\8cÀ,\83|\83C\83\93\83g\95t\97^\97¦,\94Ì\94\84\8aJ\8en\93ú,\94Ì\94\84\8fI\97¹\93ú,\8f¤\95i\83J\83e\83S\83\8a,\93ü\89×\97\\92è\93ú,\83T\83C\83Y,\91f\8dÞ,\8c´\8eY\8d\91,\8fd\82³,\93ü\89×\93ú,\82»\82Ì\91¼\8ed\97l,\83t\83\8a\81[\93ü\97Í,\94z\91\97\93ú,\8ed\93ü\90æ\96¼,\94Ì\94\84\8c³\96¼,\93o\98^\93ú,\8dX\90V\93ú\r
+24,\8cö\8aJ,\83X\83J\81[\83g,,\83e\83X\83g,\83e\83X\83g,,1000,70,,,69,,,,,,,,2009/10/1 9:00,2012/10/1 9:00,\91å\83J\83e\83S\83\8a,2009/10/1 9:00,,,,,,,,,,,\83T\83u\94Ì\94\84\8c³,2009/12/8 19:01,2009/12/8 19:01\r
+,\8cö\8aJ,\83X\83J\81[\83g,,\83e\83X\83g,\83e\83X\83g,,1000,70,,,69,,,,,,,,2009/10/1 9:00,2012/10/1 9:00,\91å\83J\83e\83S\83\8a,2009/10/1 9:00,,,,,,,,,,,2009/12/8 19:01,2009/12/8 19:01\r
public_start_at: 2009-10-01 00:00:00
public_end_at: 2012-10-30 00:00:00
arrival_expected_date: 2009-10-01 00:00:00
+#通常商品
+valid_product_other_shop:
+ id: 24
+ name: コート
+ permit: true
+ no_limit_flag: false
+ introduction: テスト
+ description: テスト
+ price: 1000
+ small_resource_id: 70
+ medium_resource_id: 69
+ category_id: 16
+ delivery_dates: 3
+ have_product_style: true
+ sale_start_at: 2009-10-01 00:00:00
+ sale_end_at: 2012-10-01 00:00:00
+ public_start_at: 2009-10-01 00:00:00
+ public_end_at: 2012-10-01 00:00:00
+ arrival_expected_date: 2009-10-01 00:00:00
+ style_id: 7
+ created_at: 2009-10-01 00:00:00
+ updated_at: 2009-10-01 00:00:00
+ supplier_id: 2
+ retailer_id: 2
#現状だとraw_password,email_confirm,password_confirmがblankだとエラー
it "CSVアップロード" do
max_id = Customer.maximum(:id)
- Customer.add_by_csv(File.read("#{RAILS_ROOT}/spec/customer_csv_upload_for_spec.csv"))
+ Customer.add_by_csv(File.read("#{RAILS_ROOT}/spec/csv/customer_csv_upload_for_spec.csv"))
max_id.should < Customer.maximum(:id)
end
end
it "CSVアップロード" do
max_id = Product.maximum(:id)
- Product.add_by_csv(File.read("#{RAILS_ROOT}/spec/product_csv_upload_for_spec.csv"))
+ Product.add_by_csv(File.read("#{RAILS_ROOT}/spec/csv/product_csv_upload_for_spec.csv"), 1)
max_id.should < Product.maximum(:id)
#=========================================
cnt_image_data_b = ResourceData.count
cnt_product_b = Product.count
#CSVアップロード
- Product.add_by_csv(File.read("#{RAILS_ROOT}/spec/product_csv_upload_image_for_spec.csv"))
+ Product.add_by_csv(File.read("#{RAILS_ROOT}/spec/csv/product_csv_upload_image_for_spec.csv"), 1)
#期待結果
#商品データが1件更新、1件増加、