我在想,我怎麼能重構這個代碼,因爲它沒有看起來不錯的閱讀和理解重構軌方法
def next_payment_price
price = self.plan_price
price = discounted_price if self.coupon && self.coupon_duration.nil? && self.coupon_discount != 100
price = discounted_price if self.coupon && self.coupon_duration.present? && self.coupon_discount != 100 && ((self.created_at + 14.days + self.coupon_duration.month) > Time.now)
price
end
def discounted_price
self.plan_price - ((self.plan_price * self.coupon_discount)/100)
end
提取這優惠券類 – apneadiving
無法實現,因爲優惠券是可以改變的,而在這個類中,我有靜態數據,這將用於計算 –
是的...但是你可以用你的細節初始化一個優惠券對象,提醒:解耦你的lpgic,用它們創建對象自己的責任。 (基本上,人們鼓勵你保持模型中的邏輯不要遵守這些準則) – apneadiving