Commit 2346980

mo khan <mo@mokhan.ca>
2015-02-28 19:33:24
implement the items#destroy action.
1 parent afa0265
Changed files (2)
app
spec
app/controllers/items_controller.rb
@@ -25,10 +25,12 @@ class ItemsController < ApplicationController
   def update
     item = current_user.items.find(params[:id])
     item.update!(secure_params)
-    render nothing: true
+    redirect_to dashboard_path
   end
 
   def destroy
+    current_user.items.destroy(params[:id])
+    redirect_to dashboard_path
   end
 
   private
spec/controllers/items_controller_spec.rb
@@ -93,7 +93,7 @@ RSpec.describe ItemsController, type: :controller do
       let(:my_item) { create(:item, user: user) }
       let(:item_params) { build(:item).attributes }
 
-      it 'updates the item' do
+      it "updates the item" do
         patch :update, id: my_item.id, item: item_params
 
         my_item.reload
@@ -103,6 +103,36 @@ RSpec.describe ItemsController, type: :controller do
         expect(my_item.serial_number).to eql(item_params["serial_number"])
         expect(my_item.purchase_price).to eql(item_params["purchase_price"])
       end
+
+      it "redirects to the dashboard" do
+        patch :update, id: my_item.id, item: item_params
+
+        expect(response).to redirect_to(dashboard_path)
+      end
+    end
+
+    describe "#destroy" do
+      let(:my_item) { create(:item, user: user) }
+      let(:other_item) { create(:item, user: other_user) }
+      let(:other_user) { create(:user) }
+
+      it "deletes the item" do
+        delete :destroy, id: my_item.id
+
+        expect(Item.count).to eql(0)
+      end
+
+      it "cannot delete another persons item" do
+        delete :destroy, id: other_item.id
+
+        expect(response.status).to eql(404)
+      end
+
+      it "redirects to the dashboard" do
+        delete :destroy, id: my_item.id
+
+        expect(response).to redirect_to(dashboard_path)
+      end
     end
   end
 end