Sha256: 96c8e903909b0e41b9954de03be9e89910d9edac1596212a48aa411e45c453f2

Contents?: true

Size: 1.55 KB

Versions: 13

Compression:

Stored size: 1.55 KB

Contents

module Spree
  class Calculator < Spree::Base
    # Conditional check for backwards compatibilty since acts as paranoid was added late https://github.com/spree/spree/issues/5858
    if connection.table_exists?(:spree_calculators) && connection.column_exists?(:spree_calculators, :deleted_at)
      acts_as_paranoid
    end

    belongs_to :calculable, polymorphic: true

    # This method calls a compute_<computable> method. must be overriden in concrete calculator.
    #
    # It should return amount computed based on #calculable and the computable parameter
    def compute(computable)
      # Spree::LineItem -> :compute_line_item
      computable_name = computable.class.name.demodulize.underscore
      method = "compute_#{computable_name}".to_sym
      calculator_class = self.class
      if respond_to?(method)
        self.send(method, computable)
      else
        raise NotImplementedError, "Please implement '#{method}(#{computable_name})' in your calculator: #{calculator_class.name}"
      end
    end

    # overwrite to provide description for your calculators
    def self.description
      'Base Calculator'
    end

    ###################################################################

    def self.register(*klasses)
    end

    # Returns all calculators applicable for kind of work
    def self.calculators
      Rails.application.config.spree.calculators
    end

    def to_s
      self.class.name.titleize.gsub("Calculator\/", "")
    end

    def description
      self.class.description
    end

    def available?(object)
      true
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
spree_core-3.0.10 app/models/spree/calculator.rb
spree_core-3.0.9 app/models/spree/calculator.rb
spree_core-3.0.8 app/models/spree/calculator.rb
spree_core-3.0.7 app/models/spree/calculator.rb
spree_core-3.0.6.1 app/models/spree/calculator.rb
spree_core-3.0.6 app/models/spree/calculator.rb
spree_core-3.0.5 app/models/spree/calculator.rb
spree_core-3.0.4 app/models/spree/calculator.rb
spree_core-2.4.10 app/models/spree/calculator.rb
spree_core-3.0.3 app/models/spree/calculator.rb
spree_core-2.4.9 app/models/spree/calculator.rb
spree_core-3.0.2 app/models/spree/calculator.rb
spree_core-2.4.8 app/models/spree/calculator.rb