| commands | |
|---|---|
| pundit | |
| decorators/presenter | |
| DelegateClass |
| command | operation |
|---|---|
| pundit | policy |
| decorators/presenter | cells |
| DelegateClass | operation |
class PaymentController < ApplicationController
def show
@payment = Payment.build(params[:payment])
end
def create
@payment = Payment.create!(params.require(:payment).permit(:amount, :patient_id, :visit_id)
CreditCardProcessor.new(payment: @payment).run! if @payment.patient_portion
end
endclass Payment < ActiveRecord::Base
belongs_to :patient
belongs_to :visit
validates :amount, numericality: { less_than: 5_000 }
before_create :handle_insurance, if: ->(payment) { payment.patient.insurance? }
def patient_portion?; ...; end
def insurance_portion?; ...; end
private
def handle_insurance
ins_pay = InsuranceBill.create!(patient: patient, visit: visit) if insurance_portion?
InsuranceNotification.create!(payment: ins_pay) if insurance_portion?
end
endclass PaymentController < ApplicationController
def show
@payment = Payment.build
end
def create
@payment = Payment.build(params[:payment])
CreditCardProcessor.new(payment: @payment).run! if @payment.patient_portion
patient = @payment.patient
if patient.insurance?
ins_pay = InsuranceBill.create!(patient: patient, visit: visit) if @payment.insurance_portion
InsuranceNotification.create!(payment: ins_pay) if @payment.insurance_portion
end
end
endclass Payment::Create < Trailblazer::Operation
def process(params)
...
end
endclass Payment::Create < Trailblazer::Operation
include Model
model Payment, :create
def process(params)
...
end
endclass Payment::Create < Trailblazer::Operation
include Model
model Payment, :create
def process(params)
validate(params[:payment]) do
contract.save
end
end
endclass Payment::Create < Trailblazer::Operation
include Model
model Payment, :create
contract do
property :patient_id, validates: { presence: true }
property :visit_id, validates: { presence: true }
property :amount, validates: { numericality: { less_than: 5_000 } }
end
def process(params)
validate(params[:payment]) do
contract.save
end
end
end
class Payment::Create < Trailblazer::Operation
include Model
model Payment, :create
contract do
property :patient_id, validates: { presence: true }
property :visit_id, validates: { presence: true }
property :amount, validates: { numericality: { less_than: 5_000 } }
end
def process(params)
validate(params[:payment]) do
contract.save
CreditCardProcessor.new(payment: model).run! if model.patient_portion
end
end
endclass PaymentController < ApplicationController
def show
@payment = Payment.build
end
def create
@payment = Payment.build(params[:payment])
patient = @payment.patient
CreditCardProcessor.new(payment: @payment).run! if @payment.patient_portion
if patient.insurance?
ins_pay = InsuranceBill.create!(patient: patient, visit: visit) if @payment.insurance_portion
InsuranceNotification.create!(payment: ins_pay) if @payment.insurance_portion
end
end
end
class Payment::Create < Trailblazer::Operation
include Model
model Payment, :create
contract do
property :patient_id, validates: { presence: true }
property :visit_id, validates: { presence: true }
property :amount, validates: { numericality: { less_than: 5_000 } }
end
def process(params)
validate(params[:payment]) do
contract.save
CreditCardProcessor.new(payment: @payment).run! if @payment.patient_portion
if model.patient.insurance?
ins_pay = InsuranceBill.create!(patient: patient, visit: visit) if model.insurance_portion
InsuranceNotification.create!(payment: ins_pay) if model.insurance_portion
end
end
end
endclass Payment::Create < Trailblazer::Operation
include Model
model Payment, :create
contract do
property :patient_id, validates: { presence: true }
property :visit_id, validates: { presence: true }
property :amount, validates: { numericality: { less_than: 5_000 } }
end
builds -> (params) do
patient = Patient.find(params[:payment][:patient_id])
return Insured if patient.insurance?
return self
end
def process(params)
validate(params[:payment]) do
contract.save
CreditCardProcessor.new(payment: @payment).run! if @payment.patient_portion
end
end
class Insured < self
def process(params)
validate(params[:payment]) do
super(params)
ins_pay = InsuranceBill.create!(patient: patient, visit: visit) if model.insurance_portion
InsuranceNotification.create!(payment: ins_pay) if model.insurance_portion
end
end
end
endclass PaymentController < ApplicationController
def show
form Payment::Create
end
def update
run Payment::Create do
redirect_to home_path
end
render :show
end
end
class Payment < ActiveRecord::Base
belongs_to :patient
def patient_portion?; ...; end
def insurance_portion?; ...; end
end
class Payment
class Cell < Cell::Concept
include TimeAgoHelper
property :amount
property :created_at
def show
render
end
private
def user
options[:user]
end
def receipt_image
img_tag model.images.resize(:small), alt: "Payment Receipt"
end
end
end<div>
<div><%= receipt_image %></div>
<span><%= number_to_currency amount %></span>
<% if user.admin? %><%= link_to Edit, edit_path %><% end %>
<span>Created: <%= time_ago(created_at) %></span>
</div>
<%= concept("payment/cell, payment, user: acting_user) %>class Patient
class Cell < Cell::Concept
def show
latest_payment = concept("payment/cell", patient.payments.last)
'<span class="special">' + render + '</span>' + latest_payment
end
end
endclass Payment
class Cell < Cell::Concept
def show
render
end
private
def late?
created_at < 1.month.ago
end
def background_color
late? ? "red" : "green"
end
end
end<div class="background-color: <%= background_color %>">
<%= number_to_currency amount %>
</div>| command | operation |
|---|---|
| pundit | policy |
| decorators/presenter | cells |
| DelegateClass | operation |
or
source: https://8thlight.com/blog/uncle-bob/2012/08/13/the-clean-architecture.html