Sha256: d479bdf37b6cec0b384475c3941133262ea78fe946937512c3cf13dcf99d4ba9

Contents?: true

Size: 1009 Bytes

Versions: 7

Compression:

Stored size: 1009 Bytes

Contents

module Spree
  class OrderPopulator
    attr_accessor :order, :currency
    attr_reader :errors

    def initialize(order, currency)
      @order = order
      @currency = currency
      @errors = ActiveModel::Errors.new(self)
    end

    
    def populate(variant_id, quantity)
      attempt_cart_add(variant_id, quantity)
      valid?
    end

    def valid?
      errors.empty?
    end

    private

    def attempt_cart_add(variant_id, quantity)
      quantity = quantity.to_i
      # 2,147,483,647 is crazy.
      # See issue #2695.
      if quantity > 2_147_483_647
        errors.add(:base, Spree.t(:please_enter_reasonable_quantity, :scope => :order_populator))
        return false
      end

      variant = Spree::Variant.find(variant_id)
      if quantity > 0
        line_item = @order.contents.add(variant, quantity, currency)
        unless line_item.valid?
          errors.add(:base, line_item.errors.messages.values.join(" "))
          return false
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
spree_core-2.3.1 app/models/spree/order_populator.rb
spree_core-2.2.4 app/models/spree/order_populator.rb
spree_core-2.2.3 app/models/spree/order_populator.rb
spree_core-2.3.0 app/models/spree/order_populator.rb
spree_core-2.2.2 app/models/spree/order_populator.rb
spree_core-2.2.1 app/models/spree/order_populator.rb
spree_core-2.2.0 app/models/spree/order_populator.rb