Class: Workarea::Shipping::Service

Inherits:
Object
  • Object
show all
Includes:
ApplicationDocument
Defined in:
app/models/workarea/shipping/service.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ApplicationDocument

#releasable?

Methods included from Sidekiq::Callbacks

add_worker, assert_valid_config!, async, caching_classes?, disable, enable, inline, #run_callbacks, workers, workers_list

Methods included from Mongoid::Document

#embedded_children

Class Method Details

.by_price(price) ⇒ Object



39
40
41
42
43
44
# File 'app/models/workarea/shipping/service.rb', line 39

def self.by_price(price)
  cache.select do |service|
    (service.subtotal_min.nil? || service.subtotal_min <= price) &&
      (service.subtotal_max.nil? || service.subtotal_max >= price)
  end
end

.cacheObject



29
30
31
32
33
# File 'app/models/workarea/shipping/service.rb', line 29

def self.cache
  Rails.cache.fetch('shipping_services_cache', expires_in: Workarea.config.cache_expirations.shipping_services) do
    Shipping::Service.all.to_a
  end
end

.default_tax_code(carrier, name) ⇒ Object



51
52
53
54
55
56
# File 'app/models/workarea/shipping/service.rb', line 51

def self.default_tax_code(carrier, name)
  default = Workarea.config.default_shipping_service_tax_code
  return default unless default.respond_to?(:call)

  default.call(carrier, name)
end

.find_tax_code(carrier, name) ⇒ Object



46
47
48
49
# File 'app/models/workarea/shipping/service.rb', line 46

def self.find_tax_code(carrier, name)
  service = find_by(carrier: carrier, name: name) rescue nil
  service.present? ? service.tax_code : default_tax_code(carrier, name)
end

.for_location(country, region) ⇒ Object



35
36
37
# File 'app/models/workarea/shipping/service.rb', line 35

def self.for_location(country, region)
  Shipping::LocationQuery.new(cache, country, region).location_services
end

Instance Method Details

#find_rate(price = 0.to_m) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'app/models/workarea/shipping/service.rb', line 58

def find_rate(price = 0.to_m)
  if tiered?
    rates.detect do |rate|
      (rate.tier_min.nil? || rate.tier_min <= price) &&
        (rate.tier_max.nil? || rate.tier_max >= price)
    end
  else
    rates.first
  end
end

#tiered?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'app/models/workarea/shipping/service.rb', line 69

def tiered?
  rates.any?(&:tiered?)
end

#to_option(subtotal) ⇒ Object



73
74
75
76
# File 'app/models/workarea/shipping/service.rb', line 73

def to_option(subtotal)
  price = find_rate(subtotal).price
  ShippingOption.new(attributes.merge(name: name, price: price))
end