Class: Piggybak::ShippingMethod

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.available_methods(cart) ⇒ Object



29
30
31
32
33
# File 'app/models/piggybak/shipping_method.rb', line 29

def self.available_methods(cart)
  active_methods = ShippingMethod.where(active: true)

  active_methods.select { |method| method.klass.constantize.available?(method, cart) }
end

.lookup_methods(cart) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/models/piggybak/shipping_method.rb', line 35

def self.lookup_methods(cart)
  active_methods = ShippingMethod.where(active: true)

  methods = active_methods.inject([]) do |arr, method|
    klass = method.klass.constantize
    if klass.available?(method, cart)
      rate = klass.rate(method, cart)
      arr << {
        :label => "#{method.description} $#{"%.2f" % rate}",
        :id => method.id,
        :rate => rate.to_f }
      end
    arr
  end

  methods.sort_by { |b| b[:rate] }
end

Instance Method Details

#admin_labelObject



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

def admin_label
  self.description
end

#klass_enumObject



25
26
27
# File 'app/models/piggybak/shipping_method.rb', line 25

def klass_enum
  Piggybak.config.shipping_calculators.collect { |b| [ b.constantize.description, b ] }
end