Module: Spree::CalculatedAdjustments::ClassMethods

Defined in:
lib/spree/calculated_adjustments.rb

Instance Method Summary collapse

Instance Method Details

#calculated_adjustments(options = {}) ⇒ Object



3
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
# File 'lib/spree/calculated_adjustments.rb', line 3

def calculated_adjustments(options = {})
  has_one   :calculator, :as => :calculable, :dependent => :destroy
  accepts_nested_attributes_for :calculator
  validates :calculator, :presence => true if options[:require]

  class_attribute :calculators
  self.calculators = Set.new
  # @available_calculators = []
  def register_calculator(calculator)
    self.calculators.add(calculator)
  end
  # def calculators
  #   @available_calculators
  # end

  if options[:default]
    default_calculator_class = options[:default]
    #if default_calculator_class.available?(self.new)
      before_create :default_calculator
      define_method(:default_calculator) do
        self.calculator ||= default_calculator_class.new
      end
    # else
    #   raise(ArgumentError, "calculator #{default_calculator_class} can't be used with #{self}")
    # end
  else
    define_method(:default_calculator) do
      nil
    end
  end

  include InstanceMethods
end

#register_calculator(calculator) ⇒ Object



11
12
13
# File 'lib/spree/calculated_adjustments.rb', line 11

def register_calculator(calculator)
  self.calculators.add(calculator)
end