OSDN Git Service

refs #1587 TSVダウンロード: リファクタリング
authorYOSHIDA Hiroki <hyoshida@appirits.com>
Thu, 16 May 2013 03:18:48 +0000 (12:18 +0900)
committerYOSHIDA Hiroki <hyoshida@appirits.com>
Thu, 16 May 2013 03:20:42 +0000 (12:20 +0900)
app/controllers/application_controller.rb
app/controllers/recommend_controller.rb
lib/csv_util.rb

index fbf05f5..11b7956 100644 (file)
@@ -51,6 +51,9 @@ class ApplicationController < ActionController::Base
       filename: filename
     )
   end
+
+  alias send_tsv send_csv
+
   private
   #sslの有効無効をuse_sslで決定する
   def ensure_proper_protocol
index e3b5047..007177d 100644 (file)
@@ -2,23 +2,33 @@ class RecommendController < ApplicationController
   def tsv
     conditions = Product.default_condition
     @products = Product.find(:all, :conditions => flatten_conditions(conditions))
-    csv_data = CSV.generate("",:col_sep => "\t",:row_sep => "\r\n") do | writer |
-      writer << [:item_code, :name, :url, :stock_flg, :comment, :category, :area, :price, :img_url]
-      @products.each do | product |
-        columns = []
-        columns << product.id 
-        columns << product.name 
-        columns << url_for(:controller => "/products", :action => "show", :id => product.id )
-        columns << (product.have_zaiko? ? 2 : 0)
-        columns << product.introduction.gsub(/(\r\n|[\r\n])/, " ")
-        columns << nil
-        columns << nil
-        columns << product.price_label
-        columns << url_for(:controller => "/image_resource", :action => "show", :id => product.small_resource_id) 
-        writer << columns 
-      end
+    tsv_text = CSVUtil::make_tsv_string(tsv_rows(@products), tsv_header)
+    send_tsv(tsv_text, tsv_filename)
+  end
+
+  private
+
+  def tsv_filename
+    'tsv.tsv'
+  end
+
+  def tsv_header
+    [ :item_code, :name, :url, :stock_flg, :comment, :category, :area, :price, :img_url ]
+  end
+
+  def tsv_rows(products)
+    products.map do |product|
+      columns = []
+      columns << product.id 
+      columns << product.name 
+      columns << url_for(controller: :products, action: :show, id: product.id)
+      columns << (product.have_zaiko? ? 2 : 0)
+      columns << product.introduction.gsub(/(\r\n|[\r\n])/, " ")
+      columns << nil
+      columns << nil
+      columns << product.price_label
+      columns << url_for(controller: :image_resource, action: :show, id: product.small_resource_id) 
+      columns 
     end
-    filename = "tsv.tsv"
-    send_csv(csv_data,filename)
   end
 end
index 05fd143..7882ec8 100644 (file)
@@ -34,11 +34,21 @@ class CSVUtil
       return pairs
     end
     
-    def make_csv_string(rows, header)
-      CSV.generate do |writer|
+    def make_csv_string(rows, header, options={})
+      CSV.generate("", options) do |writer|
         writer << header
         rows.each {|row| writer << row }
       end
     end
+
+    def make_tsv_string(rows, header, options={})
+      make_csv_string(rows, header, default_tsv_options.merge(options))
+    end
+
+    private
+
+    def default_tsv_options
+      { col_sep: "\t", row_sep: "\r\n" }
+    end
   end
 end