Skip to content

Commit

Permalink
Merge pull request #75 from syncrou/add_hard_deletes
Browse files Browse the repository at this point in the history
Added Portfolio DELETE method
  • Loading branch information
syncrou authored Jan 4, 2019
2 parents 6858ed1 + 628a11b commit 1acb490
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 4 deletions.
5 changes: 5 additions & 0 deletions app/controllers/api/v0/admins_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ def edit_portfolio
render :json => portfolio
end

def destroy_portfolio
Portfolio.find(params.require(:portfolio_id)).destroy
head :no_content
end

def add_portfolio_item_to_portfolio
portfolio = Portfolio.find(params.require(:portfolio_id))
portfolio_item = PortfolioItem.find(params.require(:portfolio_item_id))
Expand Down
2 changes: 1 addition & 1 deletion app/models/portfolio.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Portfolio < ApplicationRecord

validates_presence_of :name
validates_presence_of :description
has_many :portfolio_items
has_many :portfolio_items, :dependent => :destroy

def add_portfolio_item(portfolio_item_id)
portfolio_item = PortfolioItem.find_by(id: portfolio_item_id)
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def add_swagger_route(http_method, path, opts = {})
add_swagger_route 'GET', '/portfolios/{portfolio_id}/portfolio_items', :controller_name => 'admins', :action_name => 'fetch_portfolio_items_with_portfolio'
add_swagger_route 'GET', '/portfolios/{portfolio_id}', :controller_name => 'admins', :action_name => 'fetch_portfolio_with_id'
add_swagger_route 'PATCH', '/portfolios/{portfolio_id}', :controller_name => 'admins', :action_name => 'edit_portfolio'
add_swagger_route 'DELETE', '/portfolios/{portfolio_id}', :controller_name => 'admins', :action_name => 'destroy_portfolio'
add_swagger_route 'GET', '/orders/{order_id}/items/{order_item_id}', :controller_name => 'admins', :action_name => 'list_order_item'
add_swagger_route 'GET', '/orders/{order_id}/items', :controller_name => 'admins', :action_name => 'list_order_items'
add_swagger_route 'GET', '/orders', :controller_name => 'admins', :action_name => 'list_orders'
Expand Down
22 changes: 22 additions & 0 deletions public/doc/swagger-2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,28 @@ paths:
$ref: '#/responses/Forbidden'
'422':
$ref: '#/responses/InvalidEntity'
delete:
tags:
- admins
summary: Delete an existing portfolio
operationId: destroyPortfolio
description: |
Deletes the portfolio id passed in as the param.
produces:
- application/json
parameters:
- $ref: '#/parameters/PortfolioID'
responses:
'200':
description: Edited portfolio object
schema:
$ref: '#/definitions/Portfolio'
'401':
$ref: '#/responses/Unauthorized'
'403':
$ref: '#/responses/Forbidden'
'422':
$ref: '#/responses/InvalidEntity'
'/portfolios/{portfolio_id}/portfolio_items':
get:
tags:
Expand Down
21 changes: 18 additions & 3 deletions spec/models/portfolio_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
describe Portfolio do
let(:portfolio) { create(:portfolio) }
let(:portfolio_item) { create(:portfolio_item) }
let(:tenant) { create(:tenant) }
let(:portfolio) { create(:portfolio) }
let(:portfolio_id) { portfolio.id }
let(:portfolio_item) { create(:portfolio_item) }
let(:portfolio_item_id) { portfolio_item.id }
let(:tenant) { create(:tenant) }

context "destroy portfolio cascading portfolio_items" do
before do
ActsAsTenant.current_tenant = nil
portfolio.add_portfolio_item(portfolio_item)
end

it "destroys portfolio_items only associated with the current portfolio" do
portfolio.destroy
expect(Portfolio.find_by(:id => portfolio_id)).to be_nil
expect(PortfolioItem.find_by(:id => portfolio_item_id)).to be_nil
end
end

context "without current_tenant" do

Expand Down
14 changes: 14 additions & 0 deletions spec/requests/portfolios_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,20 @@
end
end

describe 'DELETE admin tagged /portfolios/:portfolio_id' do
let(:valid_attributes) { { :name => 'PatchPortfolio', :description => 'description for patched portfolio' } }

context 'when :portfolio_id is valid' do
before do
delete "#{api}/portfolios/#{portfolio_id}", :headers => admin_headers, :params => valid_attributes
end

it 'deletes the record' do
expect(response).to have_http_status(204)
end
end
end

describe 'PATCH admin tagged /portfolios/:portfolio_id' do
let(:valid_attributes) { { :name => 'PatchPortfolio', :description => 'description for patched portfolio' } }
let(:invalid_attributes) { { :fred => 'nope', :bob => 'bob portfolio' } }
Expand Down

0 comments on commit 1acb490

Please sign in to comment.