Class: Spree::OrderPopulator

Inherits:
Object
  • Object
show all
Defined in:
app/models/spree/order_populator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(order, currency) ⇒ OrderPopulator



6
7
8
9
10
# File 'app/models/spree/order_populator.rb', line 6

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

Instance Attribute Details

#currencyObject

Returns the value of attribute currency



3
4
5
# File 'app/models/spree/order_populator.rb', line 3

def currency
  @currency
end

#errorsObject (readonly)

Returns the value of attribute errors



4
5
6
# File 'app/models/spree/order_populator.rb', line 4

def errors
  @errors
end

#orderObject

Returns the value of attribute order



3
4
5
# File 'app/models/spree/order_populator.rb', line 3

def order
  @order
end

Instance Method Details

#populate(from_hash) ⇒ Object

Parameters can be passed using the following possible parameter configurations:

  • Single variant/quantity pairing

:variants => { variant_id => quantity }

  • Multiple products at once

+:products => { product_id => variant_id, product_id => variant_id }, :quantity => quantity+



20
21
22
23
24
25
26
27
28
29
30
# File 'app/models/spree/order_populator.rb', line 20

def populate(from_hash)
  from_hash[:products].each do |product_id,variant_id|
    attempt_cart_add(variant_id, from_hash[:quantity])
  end if from_hash[:products]

  from_hash[:variants].each do |variant_id, quantity|
    attempt_cart_add(variant_id, quantity)
  end if from_hash[:variants]

  valid?
end

#valid?Boolean



32
33
34
# File 'app/models/spree/order_populator.rb', line 32

def valid?
  errors.empty?
end