Module: RailsTrade::Amount

Extended by:
ActiveSupport::Concern
Included in:
Cart, Order
Defined in:
app/models/rails_trade/amount.rb

Instance Method Summary collapse

Instance Method Details

#compute_amountObject



16
17
18
19
20
21
22
# File 'app/models/rails_trade/amount.rb', line 16

def compute_amount
  self.item_amount = trade_items.sum(&:amount)
  self.overall_additional_amount = trade_promotes.select(&->(o){ o.amount >= 0 }).sum(&:amount)
  self.overall_reduced_amount = trade_promotes.select(&->(o){ o.amount < 0 }).sum(&:amount)
  self.amount = item_amount + overall_additional_amount + overall_reduced_amount
  self
end

#compute_saved_amountObject



24
25
26
27
28
# File 'app/models/rails_trade/amount.rb', line 24

def compute_saved_amount
  _item_amount = trade_items.sum(:amount)
  _promote_amount = trade_promotes.sum(:amount)
  _item_amount + _promote_amount
end

#reset_amountObject



30
31
32
33
34
35
36
37
# File 'app/models/rails_trade/amount.rb', line 30

def reset_amount
  self.item_amount = trade_items.sum(:amount)
  self.overall_additional_amount = trade_promotes.default_where('amount-gte': 0).sum(:amount)
  self.overall_reduced_amount = trade_promotes.default_where('amount-lt': 0).sum(:amount)
  self.amount = item_amount + overall_additional_amount + overall_reduced_amount
  self.valid?
  self.changes
end

#reset_amount!(*args) ⇒ Object



39
40
41
42
# File 'app/models/rails_trade/amount.rb', line 39

def reset_amount!(*args)
  self.reset_amount
  self.save(*args)
end