Sha256: b4f9693e4d42fe5ec357d1fd7db73acc1fe08bc58e897b54e425f91aeb76f408

Contents?: true

Size: 1.82 KB

Versions: 17

Compression:

Stored size: 1.82 KB

Contents

module Piggybak
  class ShippingMethod < ActiveRecord::Base
    has_many :shipping_method_values, :dependent => :destroy
    alias :metadata :shipping_method_values

    validates_presence_of :description
    validates_presence_of :klass

    accepts_nested_attributes_for :shipping_method_values, :allow_destroy => true

    attr_accessible :active, :shipping_method_values_attributes, :description,
                    :klass

    validates_each :shipping_method_values do |record, attr, value|
      if record.klass.present?
        calculator = record.klass.constantize
        metadata_keys = value.collect { |v| v.key }.sort
        if calculator::KEYS.sort != metadata_keys
          if calculator::KEYS.empty?
            record.errors.add attr, "You don't need any metadata for this method."
          else
            record.errors.add attr, "You must define key values for #{calculator::KEYS.join(', ')} for this shipping method."
          end
        end
      end
    end

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

    def self.available_methods(cart)
      active_methods = ShippingMethod.find_all_by_active(true)

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

    def self.lookup_methods(cart)
      active_methods = ShippingMethod.find_all_by_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 }
		end
        arr
      end

      methods.sort_by { |b| b[:rate] }
    end
    def admin_label
      self.description
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
piggybak-0.6.27 app/models/piggybak/shipping_method.rb
piggybak-0.6.26 app/models/piggybak/shipping_method.rb
piggybak-0.6.25 app/models/piggybak/shipping_method.rb
piggybak-0.6.24 app/models/piggybak/shipping_method.rb
piggybak-0.6.23 app/models/piggybak/shipping_method.rb
piggybak-0.6.22 app/models/piggybak/shipping_method.rb
piggybak-0.6.21 app/models/piggybak/shipping_method.rb
piggybak-0.6.20 app/models/piggybak/shipping_method.rb
piggybak-0.6.19 app/models/piggybak/shipping_method.rb
piggybak-0.6.18 app/models/piggybak/shipping_method.rb
piggybak-0.6.17 app/models/piggybak/shipping_method.rb
piggybak-0.6.16 app/models/piggybak/shipping_method.rb
piggybak-0.6.15 app/models/piggybak/shipping_method.rb
piggybak-0.6.13 app/models/piggybak/shipping_method.rb
piggybak-0.6.12 app/models/piggybak/shipping_method.rb
piggybak-0.6.11 app/models/piggybak/shipping_method.rb
piggybak-0.6.10 app/models/piggybak/shipping_method.rb