-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathshipping_rate.rb
48 lines (37 loc) · 1.52 KB
/
shipping_rate.rb
1
2
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# frozen_string_literal: true
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', touch: true
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",
inverse_of: :shipping_rate,
dependent: :destroy
delegate :order, :currency, to: :shipment
delegate :name, :tax_category, :tax_category_id, to: :shipping_method
delegate :code, to: :shipping_method, prefix: true
alias_attribute :amount, :cost
alias_method :discounted_amount, :amount
deprecate discounted_amount: :total_before_tax, deprecator: Spree::Deprecation
alias_method :total_before_tax, :amount
extend DisplayMoney
money_methods :amount
def display_price
price = display_amount.to_s
return price if taxes.to_a.empty? || amount == 0
tax_explanations = taxes.map(&:label).join(tax_label_separator)
I18n.t 'spree.shipping_rate.display_price.display_price_with_explanations',
price: price,
explanations: tax_explanations
end
alias_method :display_cost, :display_price
private
def tax_label_separator
I18n.t 'spree.shipping_rate.display_price.tax_label_separator'
end
end
end