ronzalo/spree_sales

View on GitHub
app/models/spree/calculator/percent_off_sale_price_calculator.rb

Summary

Maintainability
A
0 mins
Test Coverage
module Spree
  class Calculator::PercentOffSalePriceCalculator < Spree::Calculator
    # TODO: validate that the sale price is between 0 and 1
    def self.description
      "Calculates the sale price for a Variant by taking off a percentage of the original price"
    end

    def self.title
      Spree.t("sale_calculators.#{self.name.demodulize.underscore}.name")
    end

    def compute(sale_price)
      (1.0 - sale_price.value.to_f) * sale_price.price.variant.original_price.to_f
    end
  end
end