Module: Spree::Core::CalculatedAdjustments

Included in:
Promotion::Actions::CreateAdjustment, Promotion::Actions::CreateItemAdjustments, ShippingMethod, TaxRate
Defined in:
lib/spree/core/calculated_adjustments.rb

Class Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



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

    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