這是處理匯款的活動記錄類。如何擺脫所有功能中的類似說法
class Wallet < ActiveRecord::Base
belongs_to :user
has_many :deposits, dependent: :destroy
has_many :deposit_requests, dependent: :destroy
has_many :withdrawals, dependent: :destroy
has_many :withdrawal_requests, dependent: :destroy
def deposit_with_tax_deduction! amount
deposit! amount
deduct_tax(amount)
end
def deposit! amount
deposits.create! amount: amount - tax_for(amount)
end
def withdraw! amount
if can_spend? amount
withdrawals.create! amount: amount
else
raise "Недостаточно средств"
end
end
def total
deposits.sum(:amount) - withdrawals.sum(:amount) + deposit_requests.success.sum(:amount) - withdrawal_requests.success.sum(:amount)
end
def can_spend? amount
total - amount > 0
end
def tax_for(amount)
amount.to_f * Option.current.tax/100
end
private
def deduct_tax(amount)
AdminWallet.deposit! amount
end
end
我幾乎在課堂上的每種方法都有相同的論點(amount
)。它是一種代碼味道,可能會導致多年後的不良後果?我應該遵循什麼模式?