Skip to content

Commit 64a4a6d

Browse files
codeclimate refactor and stub function to the test
1 parent efeb1db commit 64a4a6d

File tree

4 files changed

+49
-44
lines changed

4 files changed

+49
-44
lines changed
Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,40 @@
11
module Admin
2-
class Invoices::MarkAsPaidsController < BaseController
3-
rescue_from Errors::InvoiceAlreadyPaid, with: :invoice_already_paid
2+
module Invoices
3+
class MarkAsPaidsController < BaseController
4+
rescue_from Errors::InvoiceAlreadyPaid, with: :invoice_already_paid
45

5-
before_action :set_invoice
6-
7-
# order is important! before set invoice, otherwise @invoice wont be set
8-
include ::Invoices::UpdateAuthorizable
6+
before_action :set_invoice
97

10-
def edit; end
8+
# order is important! before set invoice, otherwise @invoice wont be set
9+
include ::Invoices::UpdateAuthorizable
1110

12-
def update
13-
raise(Errors::InvoiceAlreadyPaid, @invoice.id) if @invoice.paid?
11+
def edit; end
1412

15-
@invoice.assign_attributes(invoice_params)
16-
@invoice.mark_as_paid_at(invoice_params[:paid_at])
17-
@invoice.save!
13+
def update
14+
raise(Errors::InvoiceAlreadyPaid, @invoice.id) if @invoice.paid?
1815

19-
flash[:notice] = t('invoices.marked_as_paid')
20-
redirect_to admin_invoice_path(@invoice), status: :see_other
21-
end
16+
@invoice.assign_attributes(invoice_params)
17+
@invoice.mark_as_paid_at(invoice_params[:paid_at])
18+
@invoice.save!
2219

23-
private
20+
flash[:notice] = t('invoices.marked_as_paid')
21+
redirect_to admin_invoice_path(@invoice), status: :see_other
22+
end
2423

25-
def set_invoice
26-
@invoice = Invoice.find(params[:invoice_id])
27-
end
24+
private
2825

29-
def invoice_already_paid
30-
flash[:alert] = t('invoices.already_paid')
31-
redirect_to admin_invoice_path(@invoice), status: :see_other
32-
end
26+
def set_invoice
27+
@invoice = Invoice.find(params[:invoice_id])
28+
end
29+
30+
def invoice_already_paid
31+
flash[:alert] = t('invoices.already_paid')
32+
redirect_to admin_invoice_path(@invoice), status: :see_other
33+
end
3334

34-
def invoice_params
35-
params.require(:invoice).permit(:notes, :paid_at)
35+
def invoice_params
36+
params.require(:invoice).permit(:notes, :paid_at)
37+
end
3638
end
3739
end
38-
end
40+
end
Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
module Admin
2-
class Invoices::TogglePartialPaymentsController < BaseController
3-
before_action :set_invoice
2+
module Invoices
3+
class TogglePartialPaymentsController < BaseController
4+
before_action :set_invoice
45

5-
# order is important! before set invoice, otherwise @invoice wont be set
6-
include ::Invoices::UpdateAuthorizable
6+
# order is important! before set invoice, otherwise @invoice wont be set
7+
include ::Invoices::UpdateAuthorizable
78

8-
def update
9-
if @invoice.toggle(:partial_payments).save
10-
action = @invoice.partial_payments? ? 'activated' : 'deactivated'
11-
redirect_to admin_invoice_path(@invoice), notice: t("invoices.partial_payments_#{action}"), status: :see_other
12-
else
13-
redirect_to admin_invoice_path(@invoice), alert: t(:something_went_wrong), status: :see_other
9+
def update
10+
if @invoice.toggle(:partial_payments).save
11+
12+
action = @invoice.partial_payments? ? 'activated' : 'deactivated'
13+
redirect_to admin_invoice_path(@invoice), notice: t("invoices.partial_payments_#{action}"), status: :see_other
14+
else
15+
redirect_to admin_invoice_path(@invoice), alert: t(:something_went_wrong), status: :see_other
16+
end
1417
end
15-
rescue StandardError => e
16-
Rails.logger.error "Error toggling partial payments: #{e.message}"
17-
redirect_to admin_invoice_path(@invoice), alert: t(:something_went_wrong), status: :see_other
18-
end
1918

20-
def set_invoice
21-
@invoice = Invoice.find(params[:invoice_id])
19+
def set_invoice
20+
@invoice = Invoice.find(params[:invoice_id])
21+
end
2222
end
2323
end
24-
end
24+
end

app/controllers/concerns/invoices/update_authorizable.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ def authorize_for_update
1717
authorize! :update, @invoice
1818
end
1919
end
20-
end
20+
end

test/controllers/admin/invoices/mark_as_paids_controller_test.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ class Invoices::MarkAsPaidControllerTest < ActionDispatch::IntegrationTest
66
@user = users(:administrator)
77
sign_in @user
88
@invoice = invoices(:payable)
9+
10+
stub_request(:patch, 'http://eis_billing_system:3000/api/v1/invoice/update_invoice_data')
11+
.to_return(status: 200, body: @message.to_json, headers: {})
912
end
1013

1114
test 'should mark invoice as paid' do

0 commit comments

Comments
 (0)