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 |method|
    (method.subtotal_min.nil? || method.subtotal_min <= price) &&
      (method.subtotal_max.nil? || method.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

.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)
  method = find_by(carrier: carrier, name: name) rescue nil
  method.try(:tax_code)
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



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

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)


62
63
64
# File 'app/models/workarea/shipping/service.rb', line 62

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

#to_option(subtotal) ⇒ Object



66
67
68
69
# File 'app/models/workarea/shipping/service.rb', line 66

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