Sha256: 9823ec324799133ac8057be98449a6da8fdaefe3d22a9452782077c4bfeb686c

Contents?: true

Size: 1.31 KB

Versions: 4

Compression:

Stored size: 1.31 KB

Contents

module Spree
  # Records the costs of different shipping methods for a shipment and which
  # method has been selected to deliver the shipment.
  #
  class ShippingRate < Spree::Base
    belongs_to :shipment, class_name: 'Spree::Shipment'
    belongs_to :shipping_method, -> { with_deleted }, class_name: 'Spree::ShippingMethod', inverse_of: :shipping_rates

    has_many :taxes,
             class_name: "Spree::ShippingRateTax",
             foreign_key: "shipping_rate_id",
             dependent: :destroy

    delegate :order, :currency, to: :shipment
    delegate :name, :tax_category, to: :shipping_method
    delegate :code, to: :shipping_method, prefix: true
    alias_attribute :amount, :cost

    alias_method :discounted_amount, :amount

    extend DisplayMoney
    money_methods :amount

    def display_price
      price = display_amount.to_s

      return price if taxes.empty? || amount == 0

      tax_explanations = taxes.map(&:label).join(tax_label_separator)

      Spree.t :display_price_with_explanations,
               scope: 'shipping_rate.display_price',
               price: price,
               explanations: tax_explanations
    end
    alias_method :display_cost, :display_price

    private

    def tax_label_separator
      Spree.t :tax_label_separator, scope: 'shipping_rate.display_price'
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
solidus_core-2.1.1 app/models/spree/shipping_rate.rb
solidus_core-2.1.0 app/models/spree/shipping_rate.rb
solidus_core-2.1.0.rc1 app/models/spree/shipping_rate.rb
solidus_core-2.1.0.beta1 app/models/spree/shipping_rate.rb