Class: Spree::ShippingMethod

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

Constant Summary collapse

DISPLAY =
[:both, :front_end, :back_end]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.all_available(order, display_on = nil) ⇒ Object



50
51
52
# File 'app/models/spree/shipping_method.rb', line 50

def self.all_available(order, display_on = nil)
  all.select { |method| method.available_to_order?(order,display_on) }
end

Instance Method Details

#available?(order, display_on = nil) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'app/models/spree/shipping_method.rb', line 18

def available?(order, display_on = nil)
  displayable?(display_on) && calculator.available?(order)
end

#available_to_order?(order, display_on = nil) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
# File 'app/models/spree/shipping_method.rb', line 30

def available_to_order?(order, display_on= nil)
  available?(order, display_on) &&
  within_zone?(order) &&
  category_match?(order)
end

#category_match?(order) ⇒ Boolean

Indicates whether or not the category rules for this shipping method are satisfied (if applicable)

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
46
47
48
# File 'app/models/spree/shipping_method.rb', line 38

def category_match?(order)
  return true if shipping_category.nil?

  if match_all
    order.products.all? { |p| p.shipping_category == shipping_category }
  elsif match_one
    order.products.any? { |p| p.shipping_category == shipping_category }
  elsif match_none
    order.products.all? { |p| p.shipping_category != shipping_category }
  end
end

#displayable?(display_on) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'app/models/spree/shipping_method.rb', line 22

def displayable?(display_on)
  (self.display_on == display_on.to_s || self.display_on.blank?)
end

#within_zone?(order) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'app/models/spree/shipping_method.rb', line 26

def within_zone?(order)
  zone && zone.include?(order.ship_address)
end