Skip to content

Commit 97f22e2

Browse files
authored
Shallow Route receipts v4 (#10828)
## Summary of the problem Can't add receipt bin receipts due to requiring to pass in transaction ID in routes. ## Describe your changes Added shallow routing <!-- If there are any visual changes, please attach images, videos, or gifs. -->
1 parent b018e70 commit 97f22e2

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

app/controllers/api/v4/receipts_controller.rb

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,23 @@ module Api
44
module V4
55
class ReceiptsController < ApplicationController
66
def index
7-
@hcb_code = authorize HcbCode.find_by_public_id(params[:transaction_id]), :show?
8-
@receipts = @hcb_code.receipts.includes(:user)
9-
end
10-
11-
def receipt_bin
12-
skip_authorization
13-
@receipts = Receipt.in_receipt_bin.includes(:user).where(user: current_user)
14-
render "index"
7+
if params[:transaction_id].present?
8+
@hcb_code = HcbCode.find_by_public_id(params[:transaction_id])
9+
authorize @hcb_code, :show?
10+
@receipts = @hcb_code.receipts.includes(:user)
11+
else
12+
skip_authorization
13+
@receipts = Receipt.in_receipt_bin.includes(:user).where(user: current_user)
14+
end
1515
end
1616

1717
def create
18-
@hcb_code = HcbCode.find_by_public_id(params[:transaction_id])
19-
authorize @hcb_code, :upload?, policy_class: ReceiptablePolicy
20-
18+
if params[:transaction_id].present?
19+
@hcb_code = HcbCode.find_by_public_id(params[:transaction_id])
20+
authorize @hcb_code, :upload?, policy_class: ReceiptablePolicy
21+
else
22+
skip_authorization
23+
end
2124
@receipt = Receipt.create!(file: params[:file], receiptable: @hcb_code, user: current_user, upload_method: :api)
2225

2326
render "show"

config/routes.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -575,15 +575,14 @@
575575
end
576576

577577
get "transactions/missing_receipt", to: "transactions#missing_receipt"
578-
get "receipt_bin", to: "receipts#receipt_bin"
579578
get :available_icons
580579
end
581580

582581
resources :events, path: "organizations", only: [:show] do
583582
resources :stripe_cards, path: "cards", only: [:index]
584583
resources :card_grants, only: [:index, :create]
585584
resources :transactions, only: [:show, :update] do
586-
resources :receipts, only: [:create, :index, :destroy]
585+
resources :receipts, only: [:index]
587586
resources :comments, only: [:index, :create]
588587

589588
member do
@@ -601,6 +600,7 @@
601600
end
602601

603602
resources :transactions, only: [:show]
603+
resources :receipts, only: [:create, :index, :destroy]
604604

605605
resources :stripe_cards, path: "cards", only: [:show, :update, :create] do
606606
member do

0 commit comments

Comments
 (0)