4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/spree/core/calculated_adjustments.rb', line 4
def self.included(klass)
klass.class_eval do
has_one :calculator, :class_name => "Spree::Calculator", :as => :calculable, :dependent => :destroy
accepts_nested_attributes_for :calculator
attr_accessible :calculator_type, :calculator_attributes
validates :calculator, :presence => true
def self.calculators
spree_calculators.send model_name_without_spree_namespace
end
def calculator_type
calculator.class.to_s if calculator
end
def calculator_type=(calculator_type)
klass = calculator_type.constantize if calculator_type
self.calculator = klass.new if klass && !self.calculator.is_a?(klass)
end
def create_adjustment(label, target, calculable, mandatory=false, state="closed")
old_calculable = calculable
calculable = calculable.to_package if calculable.is_a?(Spree::Shipment)
amount = compute_amount(calculable)
return if amount == 0 && !mandatory
target.adjustments.create({ :amount => amount,
:source => old_calculable,
:originator => self,
:label => label,
:mandatory => mandatory,
:state => state }, :without_protection => true)
end
def update_adjustment(adjustment, calculable)
calculable = calculable.to_package if calculable.is_a?(Spree::Shipment)
adjustment.update_attribute_without_callbacks(:amount, compute_amount(calculable))
end
def compute_amount(calculable)
self.calculator.compute(calculable)
end
private
def self.model_name_without_spree_namespace
self.to_s.tableize.gsub('/', '_').sub('spree_', '')
end
def self.spree_calculators
Rails.application.config.spree.calculators
end
end
end
|