Skip to content

Commit efeb1db

Browse files
refactor: move duplicate methods into concern
1 parent f3afa60 commit efeb1db

File tree

5 files changed

+28
-20
lines changed

5 files changed

+28
-20
lines changed

app/controllers/admin/invoices/mark_as_paids_controller.rb

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ class Invoices::MarkAsPaidsController < BaseController
33
rescue_from Errors::InvoiceAlreadyPaid, with: :invoice_already_paid
44

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

910
def edit; end
1011

@@ -30,14 +31,6 @@ def invoice_already_paid
3031
redirect_to admin_invoice_path(@invoice), status: :see_other
3132
end
3233

33-
def authorize_user
34-
authorize! :read, Invoice
35-
end
36-
37-
def authorize_for_update
38-
authorize! :update, @invoice
39-
end
40-
4134
def invoice_params
4235
params.require(:invoice).permit(:notes, :paid_at)
4336
end
Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
module Admin
22
class Invoices::TogglePartialPaymentsController < BaseController
33
before_action :set_invoice
4-
before_action :authorize_user
5-
before_action :authorize_for_update
4+
5+
# order is important! before set invoice, otherwise @invoice wont be set
6+
include ::Invoices::UpdateAuthorizable
67

78
def update
89
if @invoice.toggle(:partial_payments).save
@@ -19,13 +20,5 @@ def update
1920
def set_invoice
2021
@invoice = Invoice.find(params[:invoice_id])
2122
end
22-
23-
def authorize_user
24-
authorize! :read, Invoice
25-
end
26-
27-
def authorize_for_update
28-
authorize! :update, @invoice
29-
end
3023
end
3124
end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module Invoices
2+
module UpdateAuthorizable
3+
extend ActiveSupport::Concern
4+
5+
included do
6+
before_action :authorize_user
7+
before_action :authorize_for_update
8+
end
9+
10+
private
11+
12+
def authorize_user
13+
authorize! :read, Invoice
14+
end
15+
16+
def authorize_for_update
17+
authorize! :update, @invoice
18+
end
19+
end
20+
end

config/locales/invoices.en.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ en:
4545
paid_deposit_title: "Paid deposits"
4646
pay_all: Pay all invoices
4747
partial_payments_activated: "Allowed clients to make partial payments for this invoice"
48+
partial_payments_deactivated: "Disallowed clients to make partial payments for this invoice"
4849
allow_partial_payments: "Allow partial payments"
4950
disallow_partial_payments: "Disallow partial payments"
5051
amount_must_be_positive: "Amount must be greater than 0"

config/locales/invoices.et.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ et:
4646
paid_deposit_title: Deposiidimaksed
4747
pay_all: "Maksa kõik arved"
4848
partial_payments_activated: "Lubatud klientidel teha selle arve osalisi makseid"
49+
partial_payments_deactivated: "Keelatud klientidel teha selle arve osalisi makseid"
4950
allow_partial_payments: "Luba osalisi makseid"
5051
disallow_partial_payments: "Keela osalised maksed"
5152
amount_must_be_positive: "Summa peab olema suurem kui 0"

0 commit comments

Comments
 (0)