Class: Spree::ShippingMethod

Inherits:
Object
  • Object
show all
Extended by:
DisplayMoney
Includes:
CalculatedAdjustments, DisplayOn, MemoizedData, Metadata, Metafields, VendorConcern, Webhooks::HasWebhooks
Defined in:
app/models/spree/shipping_method.rb

Constant Summary collapse

MEMOIZED_METHODS =
%w[display_estimated_price digital?]
DISPLAY_ON_FRONT_END =

Used for #refresh_rates

1
DISPLAY_ON_BACK_END =
2

Constants included from DisplayOn

DisplayOn::DISPLAY

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DisplayMoney

money_methods

Class Method Details

.calculatorsObject



83
84
85
86
# File 'app/models/spree/shipping_method.rb', line 83

def self.calculators
  spree_calculators.send(model_name_without_spree_namespace).
    select { |c| c.to_s.constantize < Spree::ShippingCalculator }
end

Instance Method Details

#available_to_display?(display_filter) ⇒ Boolean

Returns:



88
89
90
91
# File 'app/models/spree/shipping_method.rb', line 88

def available_to_display?(display_filter)
  (frontend? && display_filter == DISPLAY_ON_FRONT_END) ||
    (backend? && display_filter == DISPLAY_ON_BACK_END)
end

#build_tracking_url(tracking) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/models/spree/shipping_method.rb', line 61

def build_tracking_url(tracking)
  return if tracking.blank?

  tracking = tracking.upcase

  # build tracking url automatically
  if tracking_url.blank?
    # use tracking number gem to build tracking url
    # we need to upcase the tracking number
    # https://github.com/jkeen/tracking_number/pull/85
    tracking_number_service(tracking).tracking_url if tracking_number_service(tracking).valid?
  else
    # build tracking url manually
    tracking_url.gsub(/:tracking/, ERB::Util.url_encode(tracking)) # :url_encode exists in 1.8.7 through 2.1.0
  end
end

#delivery_rangeObject



93
94
95
96
97
98
99
100
101
# File 'app/models/spree/shipping_method.rb', line 93

def delivery_range
  return unless estimated_transit_business_days_min || estimated_transit_business_days_max

  if estimated_transit_business_days_min == estimated_transit_business_days_max
    estimated_transit_business_days_min.to_s
  else
    [estimated_transit_business_days_min, estimated_transit_business_days_max].compact.join("-")
  end
end

#digital?Boolean

Returns true if the shipping method is digital

Returns:



128
129
130
# File 'app/models/spree/shipping_method.rb', line 128

def digital?
  @digital ||= calculator.is_a?(Spree::Calculator::Shipping::DigitalDelivery)
end

#display_estimated_priceObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'app/models/spree/shipping_method.rb', line 103

def display_estimated_price
  return unless calculator

  @display_estimated_price ||= begin
    calculator.description + ': ' +

    if calculator.is_a?(Spree::Calculator::Shipping::FlatRate)
      if calculator.preferred_amount == 0
        Spree.t(:free)
      else
        Spree::Money.new(calculator.preferred_amount, { currency: calculator.preferred_currency }).to_s
      end
    elsif calculator.is_a?(Spree::Calculator::Shipping::FlexiRate)
      Spree::Money.new(calculator.preferred_first_item, { currency: calculator.preferred_currency }).to_s
    elsif calculator.is_a?(Spree::Calculator::Shipping::FlatPercentItemTotal)
      ActionController::Base.helpers.number_to_percentage(calculator.preferred_flat_percent, precision: 2)
    else
      ''
    end
  end
end

#include?(address) ⇒ Boolean

Returns:



48
49
50
51
52
53
54
55
# File 'app/models/spree/shipping_method.rb', line 48

def include?(address)
  return true unless requires_zone_check?
  return false unless address

  zones.includes(:zone_members).any? do |zone|
    zone.include?(address)
  end
end

#requires_zone_check?Boolean

Returns:



57
58
59
# File 'app/models/spree/shipping_method.rb', line 57

def requires_zone_check?
  !calculator.is_a?(Spree::Calculator::Shipping::DigitalDelivery)
end

#tracking_number_service(tracking) ⇒ Object

your shipping method subclasses can override this method to provide a custom tracking number service



79
80
81
# File 'app/models/spree/shipping_method.rb', line 79

def tracking_number_service(tracking)
  @tracking_number_service ||= Spree::Dependencies.tracking_number_service.constantize.new(tracking)
end