Class: Sprangular::CartsController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/sprangular/carts_controller.rb

Instance Method Summary collapse

Methods inherited from BaseController

#invalid_resource!, #not_found, #unauthorized

Instance Method Details

#add_variantObject

Adds a new item to the order (creating a new order if none already exists)



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/controllers/sprangular/carts_controller.rb', line 13

def add_variant
  order = current_order(create_order_if_necessary: true)
  variant  = Spree::Variant.find(params[:variant_id])
  quantity = params[:quantity].to_i

  if quantity.between?(1, 2_147_483_647)
    begin
      order.contents.add(variant, quantity)
      order.ensure_updated_shipments
      @order = order.reload
      render 'spree/api/orders/show'
    rescue ActiveRecord::RecordInvalid => e
      invalid_resource!(e.record)
    end
  else
    invalid_resource!(order)
  end
end

#change_variantObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/controllers/sprangular/carts_controller.rb', line 48

def change_variant
  old_variant_id = params[:old_variant_id].to_i
  new_variant_id = params[:new_variant_id].to_i

  @order = current_order(create_order_if_necessary: true)

  existing = @order.line_items.detect { |li| li.variant_id == new_variant_id }
  old = @order.line_items.detect { |li| li.variant_id == old_variant_id }

  if existing
    existing.update(variant_id: new_variant_id, quantity: existing.quantity + old.quantity)
    old.destroy
  else
    old.update(variant_id: new_variant_id)
  end

  @order.reload

  render 'spree/api/orders/show'
end

#destroyObject



81
82
83
84
85
86
87
88
89
90
# File 'app/controllers/sprangular/carts_controller.rb', line 81

def destroy
  if @order = current_order
    @order.empty!
    @order.state ='cart'
    @order.save!
    render 'spree/api/orders/show'
  else
    not_found
  end
end

#remove_adjustmentObject



69
70
71
72
73
74
75
76
77
78
79
# File 'app/controllers/sprangular/carts_controller.rb', line 69

def remove_adjustment
  @order = current_order(create_order_if_necessary: true)
  adjustment = @order.adjustments.where(id: params[:adjustment_id]).first!
  promotion = @order.promotions.where('spree_promotions.id' => adjustment.source.promotion_id).first!
  @order.promotions.delete(promotion)
  adjustment.destroy

  @order.update_totals

  render 'spree/api/orders/show'
end

#showObject



3
4
5
6
7
8
9
10
# File 'app/controllers/sprangular/carts_controller.rb', line 3

def show
  @order = current_order
  if @order
    render 'spree/api/orders/show'
  else
    not_found
  end
end

#update_variantObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/controllers/sprangular/carts_controller.rb', line 32

def update_variant
  variant_id = params[:variant_id].to_i

  @order = current_order(create_order_if_necessary: true)

  line_item = @order.line_items.detect { |li| li.variant_id == variant_id }
  data = {
    variant_id: variant_id,
    quantity: params[:quantity]
  }
  data.merge!(id: line_item.id) if line_item
  @order.contents.update_cart(line_items_attributes: { '0' => data })

  render 'spree/api/orders/show'
end