Class: SolidusConfigurableKits::OrdersController

Inherits:
Spree::StoreController
  • Object
show all
Defined in:
app/controllers/solidus_configurable_kits/orders_controller.rb

Instance Method Summary collapse

Instance Method Details

#populate_kitObject

Adds a new Kit to the order (creating a new order if none already exists) Mostly mirrors to Spree::OrdersController#populate action!



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/controllers/solidus_configurable_kits/orders_controller.rb', line 13

def populate_kit
  @order = current_order(create_order_if_necessary: true)
  authorize! :update, @order, cookies.signed[:guest_token]

  variant  = ::Spree::Variant.find(params[:variant_id])
  quantity = params[:quantity].present? ? params[:quantity].to_i : 1
  options = { kit_variant_ids: params[:kit_variant_ids] }

  # 2,147,483,647 is crazy. See issue https://github.com/spree/spree/issues/2695.
  if !quantity.between?(1, 2_147_483_647)
    @order.errors.add(:base, t('spree.please_enter_reasonable_quantity'))
  else
    begin
      @line_item = @order.contents.add(variant, quantity, options)
      @order.line_items.reload
      @order.recalculate
    rescue ActiveRecord::RecordInvalid => e
      @order.errors.add(:base, e.record.errors.full_messages.join(", "))
    end
  end

  respond_with(@order) do |format|
    format.html do
      if @order.errors.any?
        flash[:error] = @order.errors.full_messages.join(", ")
        redirect_back_or_default(spree.root_path)
        return
      else
        redirect_to spree.cart_path
      end
    end
  end
end