Class: Spree::Adjustment

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/spree/adjustment.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.chargeObject



103
104
105
# File 'app/models/spree/adjustment.rb', line 103

def charge
  where('amount >= 0')
end

.creditObject



107
108
109
# File 'app/models/spree/adjustment.rb', line 107

def credit
  where('amount < 0')
end

.eligibleObject



99
100
101
# File 'app/models/spree/adjustment.rb', line 99

def eligible
  where(:eligible => true)
end

.optionalObject



95
96
97
# File 'app/models/spree/adjustment.rb', line 95

def optional
  where(:mandatory => false)
end

.priceObject



87
88
89
# File 'app/models/spree/adjustment.rb', line 87

def price
  where(:adjustable_type => 'Spree::LineItem')
end

.shippingObject



91
92
93
# File 'app/models/spree/adjustment.rb', line 91

def shipping
  where(:originator_type => 'Spree::ShippingMethod')
end

.taxObject



83
84
85
# File 'app/models/spree/adjustment.rb', line 83

def tax
  where(:originator_type => 'Spree::TaxRate', :adjustable_type => 'Spree::Order')
end

Instance Method Details

#currencyObject



68
69
70
# File 'app/models/spree/adjustment.rb', line 68

def currency
  adjustable.nil? ? Spree::Config[:currency] : adjustable.currency
end

#display_amountObject



72
73
74
# File 'app/models/spree/adjustment.rb', line 72

def display_amount
  Spree::Money.new(amount, { :currency => currency }).to_s
end

#eligible_for_originator?Boolean

Allow originator of the adjustment to perform an additional eligibility of the adjustment Should return true if originator is absent or doesn’t implement eligible?

Returns:

  • (Boolean)


47
48
49
50
# File 'app/models/spree/adjustment.rb', line 47

def eligible_for_originator?
  return true if originator.nil?
  !originator.respond_to?(:eligible?) || originator.eligible?(source)
end

#set_eligibilityObject

Update the boolean eligible attribute which deterimes which adjustments count towards the order’s adjustment_total.



39
40
41
42
43
# File 'app/models/spree/adjustment.rb', line 39

def set_eligibility
  update_attribute_without_callbacks(:eligible,
                                     mandatory ||
                                     (amount != 0 && eligible_for_originator?))
end

#update!(src = nil) ⇒ Object

Update both the eligibility and amount of the adjustment. Adjustments delegate updating of amount to their Originator when present, but only if locked is false. Adjustments that are locked will never change their amount. The new adjustment amount will be set by by the originator and is not automatically saved. This makes it save to use this method in an after_save hook for other models without causing an infinite recursion problem.

order#update_adjustments passes self as the src, this is so calculations can be performed on the current values. If we used source it would load the old record from db for the association



59
60
61
62
63
64
65
66
# File 'app/models/spree/adjustment.rb', line 59

def update!(src = nil)
  src ||= source
  return if locked?
  if originator.present?
    originator.update_adjustment(self, src)
  end
  set_eligibility
end