Permalink
Browse files

Let user set encoding of incoming file. Issue #10

  • Loading branch information...
1 parent 2ad7a16 commit ac01ca9abfa265ffb0679f4d4b6e4949c49d9869 @vijendra vijendra committed Feb 14, 2012
@@ -7,7 +7,8 @@ jQuery ->
data:
'_method': 'put',
'attachment[col_sep]': $('#attachment_col_sep').val(),
- 'attachment[quote_char]': $("#attachment_quote_char").val()
+ 'attachment[quote_char]': $("#attachment_quote_char").val(),
+ 'attachment[encoding]': $("#attachment_encoding").val()
,
success: (data) -> insertFields(data)
false
@@ -24,6 +25,7 @@ jQuery ->
authenticity_token: $("input[name='authenticity_token']").val()
col_sep: $("#attachment_col_sep").val()
quote_char: $("#attachment_quote_char").val()
+ encoding: $("#attachment_encoding").val()
,
onComplete: (id, fileName, data) ->
insertFields(data)
@@ -14,3 +14,4 @@ ul.top_menu li:first-child a{margin-left:0}
ul.top_menu li:last-child{border-right:none}
hr.seperator{border-top:1px solid #ccc;background:#ccc;margin:5px 0}
.clear{clear:both}
+form label span.required{color: red; font-size: 110%; margin-left: 4px;}
@@ -2,7 +2,7 @@ class AttachmentsController < ApplicationController
load_and_authorize_resource
def new
- @attachment = Attachment.new(col_sep: ',', quote_char: '"')
+ @attachment = Attachment.new(col_sep: ',', quote_char: '"', encoding: 'utf-8')
end
# TODO:
@@ -11,7 +11,7 @@ def new
# - find or construct a row with all data set, so we can show examples
def create
# puts params[:file].tempfile.external_encoding.name
- @attachment = Attachment.new(uploaded_data: params[:file], col_sep: params[:col_sep], quote_char: params[:quote_char])
+ @attachment = Attachment.new(uploaded_data: params[:file], col_sep: params[:col_sep], quote_char: params[:quote_char], encoding: params[:encoding])
@attachment.user = current_user
@attachment.save!
render json: {success: true, id: @attachment.id, rows: @attachment.rows(4)}, status: :ok
View
@@ -20,9 +20,9 @@ class Attachment < ActiveRecord::Base
after_destroy :delete_file
validates :filename, :disk_filename, presence: true
- validates :col_sep, :quote_char, presence: true
+ validates :col_sep, :quote_char, :encoding, presence: true
- attr_accessible :col_sep, :quote_char, :uploaded_data
+ attr_accessible :col_sep, :quote_char, :uploaded_data, :encoding
attr_reader :error_rows
# Any upload file gets passed in as uploaded_data attribute
@@ -53,8 +53,8 @@ def rows(size = 0)
# When parsing data, we expect our file to be saved as valid utf-8
def parsed_data
@parsed_data ||= begin
- CSV.read(full_filename, col_sep: col_sep, quote_char: quote_char)
- rescue CSV::MalformedCSVError => er
+ CSV.read(full_filename, col_sep: col_sep, quote_char: quote_char, encoding: encoding)
+ rescue #CSV::MalformedCSVError => er
rows = []
#one more attempt. If BOM is present in the file.
begin
@@ -10,13 +10,17 @@
= form_for @attachment, method: 'put' do |f|
.clearfix
- = label_tag :col_sep, Attachment.human_attribute_name(:col_sep)
+ = label_tag :col_sep, "#{Attachment.human_attribute_name(:col_sep)} <span class='required'> *</span>".html_safe
.input
= f.text_field :col_sep, class: 'minimi'
.clearfix
- = label_tag :quote_char, Attachment.human_attribute_name(:quote_char)
+ = label_tag :quote_char, "#{Attachment.human_attribute_name(:quote_charp)} <span class='required'> *</span>".html_safe
.input
= f.text_field :quote_char, class: 'minimi'
+ .clearfix
+ = label_tag :encoding, "#{Attachment.human_attribute_name(:encoding)} <span class='required'> *</span>".html_safe
+ .input
+ = f.text_field :encoding
- if @attachment.new_record?
.clearfix
%label=t(:'attachments.upload')
@@ -29,4 +33,4 @@
.actions
= f.submit t('attachments.proceed_to_mapping'), class: "btn primary large"
- = link_to t('link.back_to_list'), attachments_path, class: "btn secondary"
+ = link_to t('link.back_to_list'), attachments_path, class: "btn secondary"
@@ -0,0 +1,5 @@
+class AddEncodingToAttachments < ActiveRecord::Migration
+ def change
+ add_column :attachments, :encoding, :string
+ end
+end
@@ -15,6 +15,9 @@
it "triggers access_denied" do
controller.should_receive(:access_denied)
get :show, id: Factory(:attachment).id
+ endit "sets attachment company_id" do
+ post :create, file: file_upload('test1.csv'), col_sep: ';', quote_char: '"', encoding: 'utf-8'
+ assigns[:attachment].company_id.should == @company_id
end
end
@@ -106,27 +109,34 @@
describe "POST #create" do
it "creates new attachment" do
lambda {
- post :create, file: file_upload('test1.csv'), col_sep: ';', quote_char: '"'
+ post :create, file: file_upload('test1.csv'), col_sep: ';', quote_char: '"', encoding: 'utf-8'
}.should change(Attachment, :count).by(1)
end
it "reveals new attachment" do
- post :create, file: file_upload('test1.csv'), col_sep: ';', quote_char: '"'
+ post :create, file: file_upload('test1.csv'), col_sep: ';', quote_char: '"', encoding: 'utf-8'
assigns[:attachment].should_not be_nil
end
it "sets attachment user_id" do
- post :create, file: file_upload('test1.csv'), col_sep: ';', quote_char: '"'
+ post :create, file: file_upload('test1.csv'), col_sep: ';', quote_char: '"', encoding: 'utf-8'
assigns[:attachment].user_id.should == @user_id
end
it "sets attachment company_id" do
- post :create, file: file_upload('test1.csv'), col_sep: ';', quote_char: '"'
+ post :create, file: file_upload('test1.csv'), col_sep: ';', quote_char: '"', encoding: 'utf-8'
assigns[:attachment].company_id.should == @company_id
end
+ it "sets col_sep, quote_char and encoding " do
+ post :create, file: file_upload('test1.csv'), col_sep: ';', quote_char: '"', encoding: 'utf-8'
+ assigns[:col_sep].should == ';'
+ assigns[:quote_char].should == '"'
+ assigns[:encoding].should == 'utf-8'
+ end
+
it "renders successful json response" do
- post :create, file: file_upload('test1.csv'), col_sep: ';', quote_char: '"'
+ post :create, file: file_upload('test1.csv'), col_sep: ';', quote_char: '"', encoding: 'utf-8'
response.content_type.should == "application/json"
response.code.should == "200"
end
View
@@ -3,6 +3,7 @@
uploaded_data { file_upload('test1.csv') }
col_sep ';'
quote_char '"'
+ encoding 'utf-8'
association :mapping
end

0 comments on commit ac01ca9

Please sign in to comment.