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. #19

Merged
merged 3 commits into from
Feb 6, 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
Binary file added app/assets/images/destroy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions app/controllers/attachments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,9 @@ def update
end
end
end

def destroy
@attachment.destroy
redirect_to attachments_path
end
end
4 changes: 2 additions & 2 deletions app/models/attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def full_filename
def rows(size = 0)
parsed_data[0..(size - 1)]
end

private

# When parsing data, we expect our file to be saved as valid utf-8
Expand Down Expand Up @@ -81,7 +81,7 @@ def store_file
end

def delete_file
File.delete(full_filename)
File.delete(full_filename) rescue true #catch Errno::ENOENT exception for deleted files
end


Expand Down
3 changes: 3 additions & 0 deletions app/views/attachments/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
%th= Attachment.model_name.human(count: 1)
%th= Mapping.model_name.human(count: 1)
%th= Import.model_name.human(count: 1)
%th
%tbody
- @attachments.each do |attachment|
%tr
Expand All @@ -29,3 +30,5 @@
.created= import.created_at
- elsif attachment.mapping
= link_to(t('imports.new'), new_attachment_import_path(attachment))
%td
= link_to(image_tag('destroy.png'), attachment, :confirm => t('attachments.delete_confirm'), :method => :delete)
3 changes: 2 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ 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?"
mappings:
title:
one: "%{count} field: %{fields}"
Expand Down Expand Up @@ -69,4 +70,4 @@ en:
confirm_destroy: Really delete this record?
step: "Step %{count} from %{total}:"
errors:
sk_login_required: "Please login to SalesKing and navigate to this app from inside there."
sk_login_required: "Please login to SalesKing and navigate to this app from inside there."
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CsvImporter::Application.routes.draw do

resources :attachments, except: [:edit, :destroy] do
resources :attachments, except: [:edit] do
resources :mappings, only: [:new, :create]
resources :imports, only: [:new, :create]
end
Expand Down
21 changes: 21 additions & 0 deletions spec/controllers/attachments_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@
put :update, id: Factory(:attachment).id, attachment: {}
end
end

describe "DELETE #destroy" do
it "triggers access_denied" do
controller.should_receive(:access_denied)
delete :destroy, id: Factory(:attachment).id
end
end
end

context "for authenticaned user" do
Expand Down Expand Up @@ -173,5 +180,19 @@
end
end
end

describe "DELETE destroy" do
it "destroys the requested attachment" do
expect {
delete :destroy, :id => @authorized_attachment.id
}.to change(Attachment, :count).by(-1)
end

it "redirects to the attachments list after destroying the requested attachment" do
delete :destroy, :id => @authorized_attachment.id
response.should redirect_to(attachments_path)
end
end

end
end
36 changes: 35 additions & 1 deletion spec/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,49 @@
end

factory :mapping do

end

#data rwo will be like this ["Test organization", "Test", "male_user", "[email protected]", "Herr", "1980-01-10"]
factory :mapping_element do
association :mapping
mapping
source 0
target 'organization'
end

factory :fname_mapping_element, :class => MappingElement do
mapping
source '1,2'
target 'first_name'
conv_type 'join'
end

factory :email_mapping_element, :class => MappingElement do
mapping
source 3
target 'email'
end

factory :gender_mapping_element, :class => MappingElement do
mapping
source 4
target 'gender'
conv_type 'enum'
conv_opts "{\"male\":\"Herr\",\" female\":\"Frau\"}"
end

factory :birthday_mapping_element, :class => MappingElement do
mapping
source 5
target 'birthday'
conv_type 'date'
conv_opts "{\"date\":\"%Y-%M-%d\"}"
end



factory :import do
association :attachment
end

end
8 changes: 7 additions & 1 deletion spec/models/attachment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
File.exist?(file_path).should be_false
end

it "should silently ignore missing files on destroy" do
file_path = @attachment.full_filename
File.delete(file_path)
lambda {@attachment.destroy}.should_not raise_error(Errno::ENOENT)
end

it "parses csv data" do
@attachment.rows.size.should == 2
@attachment.rows.first.size.should be > 1
Expand All @@ -28,7 +34,7 @@
@attachment.rows(1).size.should == 1
end

describe "different csv formats" do
describe "formats" do
{'google_native_test_.csv' => 3, 'google_outlook_test.csv' => 3, 'test1.csv' => 2}.each do |csv_file, count|
it "should able to read #{csv_file}" do
attachment = Factory(:attachment, :uploaded_data => file_upload(csv_file))
Expand Down
82 changes: 82 additions & 0 deletions spec/models/data_row_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,86 @@

describe DataRow do
it { should belong_to(:import) }

describe "#populate_client" do
before :each do
@mapping = Factory(:mapping)
@attachment = Factory(:attachment, mapping: @mapping)

@import = Factory.build(:import, attachment: @attachment)
@import.should_receive(:populate_data_rows).and_return(true) #To avoid firing data_row create from after_save
@import.save
@client = stub_sk_client
end

context 'creating sk client record' do
context 'successfully' do
before :each do
#TODO try moving these to factoris file. defining association with mapping in factory is not working at the moment.
Factory(:mapping_element, mapping: @mapping)
Factory(:gender_mapping_element, mapping: @mapping)
Factory(:birthday_mapping_element, mapping: @mapping)
Factory(:fname_mapping_element, mapping: @mapping )
Factory(:mapping_element, mapping: @mapping, source: 6, target: 'address.zip')
Factory(:mapping_element, mapping: @mapping, source: 7, target: 'address.address1')
Factory(:mapping_element, mapping: @mapping, source: 8, target: 'address.city')
@csv_row = ["Test org", "T", "Male_user", "[email protected]", "Herr", "1980-01-10", '83620', 'Hubertstr. 205', 'Feldkirchen']
@client.should_receive(:save).and_return(true)
@client.should_receive(:id).and_return("some_uuid")
@data_row = @import.data_rows.new(data: @csv_row)
@data_row.save!
end

it "should save Client ID in corresponding data row" do
@data_row.sk_id.should == 'some_uuid'
end

it "should have organization field value" do
@client.organization.should == 'Test org'
end

it "should have converted enum field value" do
@client.gender.should == 'male'
end

it "should have formatted date field value" do
@client.birthday.should == '1980.01.10'
end

it "should join initial and first_name" do
@client.first_name.should == 'T Male_user'
end

it "should create an address" do
@client.addresses[0].zip.should == '83620'
@client.addresses[0].address1.should == 'Hubertstr. 205'
@client.addresses[0].city.should == 'Feldkirchen'
end
end #successfully

context 'fails' do
before :each do
@csv_row = ["", "T", "Male_user", "[email protected]", "Herr", "1980-01-10", '83620', 'Hubertstr. 205', 'Feldkirchen']
@client.should_receive(:save).and_return(false)
@client.errors[:base] = 'Organisation or lastname must be present.'
@data_row = @import.data_rows.new(data: @csv_row)
@data_row.save!
end

it "should not save Client ID in corresponding data row" do
@data_row.sk_id.should be_nil
end

it "should save failed row as source" do
@data_row.source.should == @csv_row.to_csv(col_sep: @attachment.col_sep, quote_char: @attachment.quote_char)
end

it "should save error log returned from client" do
@data_row.log.should == 'Organisation or lastname must be present.'
end
end #Fails

end #creating sk client record

end
end
2 changes: 1 addition & 1 deletion spec/models/import_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@
@import.should be_success
end
end
end
end