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
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