Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please make a pull #20

Merged
merged 4 commits into from
Feb 24, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion app/assets/javascripts/attachments.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions app/assets/stylesheets/application.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;}
7 changes: 4 additions & 3 deletions app/controllers/attachments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -20,7 +20,8 @@ def create
def update
if @attachment.update_attributes(params[:attachment])
respond_to do |format|
format.html { redirect_to new_attachment_mapping_url(@attachment) }
format.html { redirect_to (@attachment.mapping.blank? ? new_attachment_mapping_url(@attachment) : new_attachment_import_url(@attachment))
}
format.js { render json: {rows: @attachment.rows(4)}, status: :ok }
end
else
Expand Down
5 changes: 5 additions & 0 deletions app/helpers/mappings_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module MappingsHelper
def mapping_options
Mapping.by_company(session['company_id']).with_fields.map{|m| [m.title, m.id]}
end
end
8 changes: 4 additions & 4 deletions app/models/attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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, :mapping_id
attr_reader :error_rows

# Any upload file gets passed in as uploaded_data attribute
Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion app/models/mapping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ class Mapping < ActiveRecord::Base
default_scope order('mappings.id desc')

accepts_nested_attributes_for :mapping_elements

scope :by_company, lambda{|company_id| where(:company_id => company_id)}
scope :with_fields, joins(:mapping_elements).
select('mappings.id, count(mapping_elements.id) as element_count').
group('mappings.id').
having('element_count > 0')

def title
I18n.t('mappings.title', count: mapping_elements.count, fields: mapping_elements.collect(&:target).to_sentence)
end
Expand Down
10 changes: 7 additions & 3 deletions app/views/attachments/new.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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"
8 changes: 7 additions & 1 deletion app/views/mappings/new.html.haml
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
%h2
= t('link.step', count: 2, total: 3)
= t('mappings.new')

= form_for @attachment do |f|
%p= t('mappings.reuse')
= f.select 'mapping_id', options_for_select(mapping_options)
= f.submit t('mappings.proceed_to_import'), :class => "btn primary large"
%hr

= form_for [@attachment, @mapping] do |f|
.clearfix
Expand All @@ -22,4 +28,4 @@
.target{attrs.merge('data-target' => name)}= name
.actions
= f.submit t('mappings.proceed_to_import'), :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"
4 changes: 3 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ en:
new: Upload new CSV file
upload: Upload
proceed_to_mapping: Proceed to mapping
delete_confirm: "Please note that, this is irreversible process. This will delete associated imports. Are you sure?"
delete_confirm: "Please note that, this is irreversible process. This will delete associated imports. Are you sure?"
csv_import_error: "Try with different 'Column Separator' and/or 'Quote character'."
mappings:
title:
one: "%{count} field: %{fields}"
Expand All @@ -44,6 +45,7 @@ en:
proceed_to_import: Proceed to import
source_fields: Source fields
target_fields: Target fields
reuse: Select an existing mapping from the list to reuse that or create a new mapping in the below section.
dnd_info: "Please drag&drop fields above onto their targets below. Drop multiple fields on a target to join values (space-delimited) e.g for the tag list."
imports:
title_success:
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20120213113143_add_encoding_to_attachments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddEncodingToAttachments < ActiveRecord::Migration
def change
add_column :attachments, :encoding, :string
end
end
19 changes: 12 additions & 7 deletions spec/controllers/attachments_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,27 +106,27 @@
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 "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
Expand All @@ -152,11 +152,16 @@
assigns[:attachment].quote_char.should == '^'
end

it "redirects to new attachment mapping on html request" do
put :update, id: @authorized_attachment, attachment: {col_sep: ';'}
it "redirects to new attachment mapping on html request if mapping is not set" do
put :update, id: @authorized_attachment, attachment: {col_sep: ';', mapping_id: ''}
response.should redirect_to(new_attachment_mapping_url(@authorized_attachment))
end

it "redirects to new attachment import on html request if mapping is set" do
mapping = Factory(:mapping)
put :update, id: @authorized_attachment, attachment: {col_sep: ';', mapping_id: mapping.id}
response.should redirect_to(new_attachment_import_url(@authorized_attachment))
end
it "reneders successful json response on js request" do
put :update, id: @authorized_attachment, attachment: {col_sep: ';'}, format: 'js'
response.code.should == "200"
Expand Down
1 change: 1 addition & 0 deletions spec/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
uploaded_data { file_upload('test1.csv') }
col_sep ';'
quote_char '"'
encoding 'utf-8'
association :mapping
end

Expand Down
10 changes: 9 additions & 1 deletion spec/models/attachment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@
describe Attachment do
it { should belong_to(:mapping) }
it { should have_many(:imports).dependent(:destroy) }


['filename', 'col_sep', 'quote_char', 'encoding', 'disk_filename'].each do |attribute|
it { should validate_presence_of(attribute)}
end

[:col_sep, :quote_char, :uploaded_data, :encoding, :mapping_id].each do |attribute|
it { should allow_mass_assignment_of(attribute) }
end

before :each do
@attachment = Factory(:attachment)
end
Expand Down
35 changes: 35 additions & 0 deletions spec/models/mapping_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,41 @@
require 'spec_helper'

describe Mapping do
before :each do
@mapping = Factory(:mapping, :company_id => 'a company')
Factory(:mapping_element, mapping: @mapping)
Factory(:gender_mapping_element, mapping: @mapping)
Factory(:birthday_mapping_element, mapping: @mapping)
end

it { should have_many(:mapping_elements).dependent(:destroy) }
it { should have_many(:attachments).dependent(:nullify) }

describe ".by_company" do
it "includes mappings belongs to company" do
Mapping.by_company('a company').should include(@mapping)
end

it "excludes mappings belongs to other company" do
another_mapping = Factory(:mapping, :company_id => 'another company')
Mapping.by_company('a company').should_not include(another_mapping)
end
end

describe ".with_fields" do
it "includes mappings with more than one mapping element defined" do
Mapping.with_fields.should include(@mapping)
end

it "excludes mappings belongs to other company" do
another_mapping = Factory(:mapping, :company_id => 'another company')
Mapping.with_fields.should_not include(another_mapping)
end
end

describe '#title' do
it 'should return mapping details' do
@mapping.title.should == '3 fields: organization, gender, and birthday'
end
end
end