【发布时间】:2015-10-11 14:31:36
【问题描述】:
这是处理汇款的活动记录类。
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)。它是一种代码气味,多年后会导致不良后果吗?我应该遵循什么模式?
【问题讨论】:
标签: ruby-on-rails ruby oop refactoring