CSVã¢ãããã¼ã
ã¦ã¼ã¶ã¼ãCSVã¢ãããã¼ãã§ç»é²ããæ©è½ãå®è£
ãã¦ããã®ã§ã¡ã¢ãã³ã³ããã¼ã©ã®ã³ã¼ããé·ãã£ãããã¡ã½ããã®ååããã¾ãè¯ããªãã®ã§ããã£ã¨ããæ¹æ³ãè¦ã¤ããã追è¨ãã¦ãããã(*´è¸ï½)
View
ã¾ãã¯ç°¡åãªãã¥ã¼ãã
- csv_upload.rhtml
<% @title="ã¦ã¼ã¶ã¼ã®CSVä¸æ¬ç»é²" -%> ã¢ãããã¼ãã®éå½¢ã使ãå ´åãäºåã«ãã«ããåç §ãã¦ä¸ããã<br> <% form_tag( {:action => "upload"},{ :multipart => true }) do -%> <p>ã¢ãããã¼ããã¡ã¤ã«<span style="color:red;">(å¿ é )</span><br> <%= file_field_tag(:file) %> </p> <%= submit_tag("ã¢ãããã¼ã") %> <% end -%> <br> <%= link_to "ã¢ãããã¼ããã¡ã¤ã«ã®éå½¢","../../user/csv_up_format.csv" %>ã <%= link_to "ä¸æ¬ç»é²ã®ãã«ã",:action => "csv_upload_help" %>
ãã¡ã¤ã«ãéãã®ã§ã{:multipart => true}ãæå®ãããã¾ããéå½¢ãpublic/user/ãã£ã¬ã¯ããªã«ããã¦ç½®ãããã®ãã¡ã¤ã«ãã¦ã¼ã¶ã¼ããã¦ã³ãã¼ãã§ããããã«ããã
Controller
次ã«ã³ã³ããã¼ã©ãããããã¾ã綺éºãããªãã¾(ï½Ð´Â´)ï¾ï½¼
- user_controller.rb
require 'csv' def upload @users = [] file = params[:file] if CSVUtil.valid_data_from_file?(file) CSV::Reader.parse(file) do |row| @users << User.new_by_array(row.to_a) end invalid_rows = [] @users.each_with_index do |user, index| invalid_rows << i if user.invalid? end if invalid_rows.empty? @users.each{|user| user.save} flash[:notice] = "#{user.size}件ã®ãã¼ã¿ãç»é²ããã¾ãã" redirect_to :action => "list" else render_with_flash("csv_upload", <<-MSG.chomp) #{invalid_rows.join(", ")}è¡ç®ã®ãã¼ã¿ãä¸æ£ã§ããæåããããç´ãã¦ä¸ããã MSG end else render_with_flash("csv_upload","CSVãã¡ã¤ã«ã空ããæå®ããããã¡ã¤ã«ãåå¨ãã¾ãã") end end
ãã©ã³ã¶ã¯ã·ã§ã³ã使ã£ã¦ä»¥ä¸ã®ããã«æ¸ãã¦ãããã£ãã®ã§ãããä¸æ£ãªè¡ãä¸åã§ãã¹ã¦è¡¨ç¤ºãããã£ãã®ã§ãã®ããã«ãã¾ããã
- upload(transactionã使ã£ãç)
def upload @users = [] line_count = 0 file = params[:file] begin if CSVUtil.valid_data_from_file?(file) Interview.transaction do CSV::Reader.parse(file) do |row| user = User.new_by_array(row.to_a) raise unless user.save @users << user line_count += 1 end end flash[:notice] = "#{line_count}件ã®ãã¼ã¿ãç»é²ããã¾ãã" redirect_to :action => "list" else render_with_flash("csv_upload","CSVãã¡ã¤ã«ã空ããæå®ããããã¡ã¤ã«ãåå¨ãã¾ãã") end rescue => e render_with_flash("csv_upload", <<-MSG.chomp) #{line_count + 1}è¡ç®ã®ãã¼ã¿ãä¸æ£ã§ããæåããããç´ãã¦ä¸ããã MSG end end
model
ããã¦ã¢ãã«
- user.rb
class << self def new_by_array(arr) arr.map! do |elem| NKF::nkf('-S -w',elem) if elem end self.new( :family_name => arr[0], :first_name => arr[1], :phone => arr[2], :address => arr[3] ) end end
ã©ã¤ãã©ãª
CSVãæ±ãããã®ã©ã¤ãã©ãªãlibãã£ã¬ã¯ããªä¸ã«ç½®ãã
- csv_util.rb
class CSVUtil class << self def valid_data_from_file?(stream_data) if stream_data.respond_to?(:original_filename) (!stream_data.eof?) && (File.extname(stream_data.original_filename) == ".csv") else false end end end end
ã¢ãããã¼ãããããã¡ã¤ã«ï¼ã®ä¸èº«ï¼ãåå¨ãããããã®ãã¡ã¤ã«ã¯CSVãã¡ã¤ã«ãã調ã¹ã¦ããã
ãã¹ã
- user_controller_test.rb
def test_upload previous_count = User.count post :upload,:file => upload_file(File.dirname(__FILE__) + '/../../upload_files/test_upload.csv') assert_redirected_to :action => "list" assert_equal 2,assigns(:users).size assert_equal previous_count + 2,User.count satou = assigns(:users)[0] suzuki = assigns(:users)[1] assert_equal "ä½è¤", satou.family_name assert_equal "太é", satou.first_name assert_equal "09012345678", satou.phone assert_equal "[email protected]", satou.address assert_equal "é´æ¨", suzuki.family_name assert_equal "æ å", suzuki.first_name assert_equal "08011111111", suzuki.phone assert_equal "[email protected]", suzuki.address end private def upload_file(filename) if File.exist?(filename) f = File.open(filename,"r") (class << f; self; end).class_eval do define_method(:original_filename){File.basename(filename)} end f else StringIO.new end end
- test_upload.csv
ä½è¤,太é,09012345678,[email protected] é´æ¨,æ å,08011111111,[email protected]
test/upload_filesãã£ã¬ã¯ããªä¸ã«ãã¹ãç¨ã®CSVãã¡ã¤ã«ãç½®ãããããããã¼ã¿ãèªã¿è¾¼ãã§ãã¹ããè¡ãã
upload_fileã¡ã½ããã¯ããã¡ã¤ã«ãopenãããã®ãã¡ã¤ã«ã®ãã¼ã¿ãè¿ããå®éã«ãã¢ãããã¼ãããããã¡ã¤ã«ã¯ããã¼ã¿ã¨ãã¦params[:file]ã«å
¥ããã³ã³ããã¼ã©ã«æ¸¡ãããã®ã§ããã¹ãã§ããããã·ãã¥ã¬ã¼ããã¦ããã
ãã¡ã¤ã«ãåå¨ããªãå ´åã«ãStringIOãªãã¸ã§ã¯ããè¿ãã¦ããã®ããã®ãããå®éã«ãfile_fieldã«åå¨ããªããã¡ã¤ã«ã®ãã¹ãå
¥ããã¨ã空ã®StringIOãªãã¸ã§ã¯ããã³ã³ããã¼ã©ã«æ¸¡ããããï¼å°ããã¹ã§ãªããåãªãæååãå
¥ããå ´åã¯æååï¼Stringãªãã¸ã§ã¯ãï¼ã渡ãããï¼