Class: ShippingMethod

Inherits:
ActiveRecord::Base show all
Defined in:
app/models/shipping_method.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.available_for(amount, address) ⇒ Object

returns shipping methods available for the given address and for the given amount



48
49
50
51
# File 'app/models/shipping_method.rb', line 48

def self.available_for(amount, address)
  a = address.state_code ? in_state(address.state_code, address.country_code) : in_country(address.country_code)
  a.active.in_price_range(amount)
end

.available_for_countries(amount) ⇒ Object



53
54
55
# File 'app/models/shipping_method.rb', line 53

def self.available_for_countries(amount)
  active.in_price_range(amount).includes(:shipping_zone).map { |t| t.shipping_zone.country_code }.uniq
end

.in_state(state_code, country_code) ⇒ Object



33
34
35
36
37
38
# File 'app/models/shipping_method.rb', line 33

def self.in_state(state_code, country_code)
  where({
    shipping_zones: { state_code: state_code },
    country_shipping_zones_shipping_zones: { country_code: country_code }
  }).joins(shipping_zone: :country_shipping_zone)
end

Instance Method Details

#country_level?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'app/models/shipping_method.rb', line 61

def country_level?
  shipping_zone.is_a?(CountryShippingZone)
end

#disable!Object



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

def disable!
  update_attributes(active: false)
end

#enable!Object



75
76
77
# File 'app/models/shipping_method.rb', line 75

def enable!
  update_attributes(active: true)
end

#shipping_priceObject



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

def shipping_price
  country_level? ? base_price : (parent.base_price + offset)
end

#update_offset(value) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'app/models/shipping_method.rb', line 65

def update_offset(value)
  value ||= 0
  value = value.to_f

  unless country_level?
    self.offset += value
    save
  end
end