From c9826e73d7f5b44a858d43e865c4b654d527c40c Mon Sep 17 00:00:00 2001 From: vijendra Date: Sat, 4 Feb 2012 12:38:48 +0530 Subject: [PATCH] delete attachment. Issue #12 --- app/assets/images/destroy.png | Bin 0 -> 522 bytes app/controllers/attachments_controller.rb | 5 +++++ app/models/attachment.rb | 4 ++-- app/views/attachments/index.html.haml | 3 +++ config/locales/en.yml | 3 ++- config/routes.rb | 2 +- .../attachments_controller_spec.rb | 21 ++++++++++++++++++ spec/models/attachment_spec.rb | 8 ++++++- 8 files changed, 41 insertions(+), 5 deletions(-) create mode 100755 app/assets/images/destroy.png diff --git a/app/assets/images/destroy.png b/app/assets/images/destroy.png new file mode 100755 index 0000000000000000000000000000000000000000..76a1c72b56d92b29e36fc6c02c56d20f73086694 GIT binary patch literal 522 zcmV+l0`>igP)zhtLp>@2MUfQDiJIt t('attachments.delete_confirm'), :method => :delete) diff --git a/config/locales/en.yml b/config/locales/en.yml index 94fa03f..9b200f8 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -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}" @@ -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." \ No newline at end of file + sk_login_required: "Please login to SalesKing and navigate to this app from inside there." diff --git a/config/routes.rb b/config/routes.rb index a97dc1a..abe2513 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/spec/controllers/attachments_controller_spec.rb b/spec/controllers/attachments_controller_spec.rb index da251ad..4685fff 100644 --- a/spec/controllers/attachments_controller_spec.rb +++ b/spec/controllers/attachments_controller_spec.rb @@ -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 @@ -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 diff --git a/spec/models/attachment_spec.rb b/spec/models/attachment_spec.rb index 95fd006..374732e 100644 --- a/spec/models/attachment_spec.rb +++ b/spec/models/attachment_spec.rb @@ -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 @@ -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))