Module: Spree::CalculatedAdjustments::ClassMethods

Defined in:
lib/spree/calculated_adjustments.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.calculatorsObject



8
9
10
# File 'lib/spree/calculated_adjustments.rb', line 8

def self.calculators
  Rails.application.config.spree.calculators.send(self.to_s.tableize.gsub("/", "_"))
end

.register(*args) ⇒ Object

Remove in 0.80.0



29
30
31
# File 'lib/spree/calculated_adjustments.rb', line 29

def self.register(*args)
  ActiveSupport::Deprecation.warn("Calculator registration has changed, add your calculator to the relevant Rails.application.config.spree.calculators collection.", caller)
end

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
# 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]

  def self.calculators
    Rails.application.config.spree.calculators.send(self.to_s.tableize.gsub("/", "_"))
  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

  #Remove in 0.80.0
  def self.register(*args)
    ActiveSupport::Deprecation.warn("Calculator registration has changed, add your calculator to the relevant Rails.application.config.spree.calculators collection.", caller)
  end

  include InstanceMethods
end