OSDN Git Service

テスト用CSVファイルの位置を変更
authorTaro Matsuzawa <tmatsuzawa@kbmj.com>
Wed, 31 Mar 2010 08:46:00 +0000 (17:46 +0900)
committerTaro Matsuzawa <tmatsuzawa@kbmj.com>
Wed, 31 Mar 2010 08:46:00 +0000 (17:46 +0900)
商品マスタ登録のCSV関係の実装

16 files changed:
app/controllers/admin/products_controller.rb
app/models/product.rb
spec/controllers/admin/customers_controller_spec.rb
spec/controllers/admin/order_statuses_controller_spec.rb
spec/controllers/admin/products_controller_spec.rb
spec/csv/authorities_functions.csv [moved from spec/authorities_functions.csv with 100% similarity]
spec/csv/customer_csv_upload_for_spec.csv [moved from spec/customer_csv_upload_for_spec.csv with 100% similarity]
spec/csv/customer_upload.csv [moved from spec/customer_upload.csv with 100% similarity]
spec/csv/order_status_update.csv [moved from spec/order_status_update.csv with 100% similarity]
spec/csv/product_csv_upload_for_spec.csv [moved from spec/product_csv_upload_for_spec.csv with 100% similarity]
spec/csv/product_csv_upload_image_for_spec.csv [moved from spec/product_csv_upload_image_for_spec.csv with 100% similarity]
spec/csv/product_sample.csv [moved from spec/product_sample.csv with 100% similarity]
spec/csv/product_sample_other_shop.csv [new file with mode: 0644]
spec/fixtures/products.yml
spec/models/customer_spec.rb
spec/models/product_spec.rb

index 086dfbe..ce0d847 100644 (file)
@@ -122,7 +122,7 @@ class Admin::ProductsController < Admin::BaseController
 
     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}行目のデータが不正です。最初からやり直して下さい。"
index fcb2835..652ed1a 100644 (file)
@@ -335,12 +335,15 @@ class Product < ActiveRecord::Base
   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
@@ -353,21 +356,26 @@ class Product < ActiveRecord::Base
 
     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]
@@ -395,7 +403,11 @@ class Product < ActiveRecord::Base
       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)
@@ -422,9 +434,11 @@ class Product < ActiveRecord::Base
       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?
index 8fe12fb..2bb3c8e 100644 (file)
@@ -170,7 +170,7 @@ describe Admin::CustomersController do
   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
index 15d3a62..a99df3e 100644 (file)
@@ -67,7 +67,7 @@ describe Admin::OrderStatusesController do
 
   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
index 6ee3433..5833422 100644 (file)
@@ -181,12 +181,33 @@ describe Admin::ProductsController do
   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)
diff --git a/spec/csv/product_sample_other_shop.csv b/spec/csv/product_sample_other_shop.csv
new file mode 100644 (file)
index 0000000..1c4db23
--- /dev/null
@@ -0,0 +1,3 @@
+\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
index d00572f..1de4827 100644 (file)
@@ -334,4 +334,28 @@ product_style_test:
   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
 
index 5d1ebcd..3095438 100644 (file)
@@ -512,7 +512,7 @@ describe Customer do
   #現状だと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
 
index f02fecf..3f4d978 100644 (file)
@@ -175,7 +175,7 @@ describe Product do
     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)
       
       #=========================================
@@ -193,7 +193,7 @@ describe Product do
       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件増加、