Module: ShopBunny::CartModule

Included in:
Cart
Defined in:
lib/shop_bunny/cart_module.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(clazz) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/shop_bunny/cart_module.rb', line 4

def self.included(clazz)
  clazz.send(:attr_accessor, :coupon_code)
  clazz.send(:has_many, :cart_items, :dependent => :destroy, :class_name => 'ShopBunny::CartItem')
  clazz.send(:has_many, :coupon_uses, :dependent => :destroy, :class_name => 'ShopBunny::CouponUse')
  clazz.send(:has_many, :coupons, :through => :coupon_uses)
  clazz.send(:accepts_nested_attributes_for, :cart_items, :allow_destroy => true )
  clazz.send(:before_save, :update_coupons)
  clazz.send(:after_save, :free_coupon_code)
  clazz.send(:attr_accessible, :coupon_code, :cart_items_attributes)
  clazz.send(:validate, :coupon_code_must_be_valid)
end

Instance Method Details

#add_item(item, options = {}) ⇒ Object

Adds one or options number of items to the cart or increases it’s quantity.



61
62
63
64
65
66
67
# File 'lib/shop_bunny/cart_module.rb', line 61

def add_item(item,options = {})
  cart_item = find_cart_item(item)
  cart_item ||= self.cart_items.build(:item => item)
  cart_item.quantity += options[:quantity] || 1
  cart_item.save!
  self.reload
end

#as_json(options = {}) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/shop_bunny/cart_module.rb', line 112

def as_json(options={})
  { 
    :cart => attributes.
    merge(
      :cart_items => cart_items.as_json,
      :item_count => item_count || 0,
      :coupons => coupons.as_json,
      :item_sum => item_sum,
      :shipping_costs => shipping_costs,
      :total => total,
      :items_with_coupons => items_with_coupons
    )
  }
end

#bonus_itemsObject



20
21
22
# File 'lib/shop_bunny/cart_module.rb', line 20

def bonus_items
  coupons.map(&:bonus_article).compact
end

#clear!Object

Remove all items and coupons from the cart



107
108
109
110
# File 'lib/shop_bunny/cart_module.rb', line 107

def clear!
  cart_items.clear
  coupons.clear
end

#empty?Boolean

Check if the cart is empty

Returns:

  • (Boolean)


102
103
104
# File 'lib/shop_bunny/cart_module.rb', line 102

def empty?
  cart_items.empty? && bonus_items.empty?
end

#item_countObject



24
25
26
# File 'lib/shop_bunny/cart_module.rb', line 24

def item_count
  self.cart_items.inject(0) {|sum,e| sum += e.quantity}
end

#item_modelObject

Returns the class/model of the items you can buy. (Products)



97
98
99
# File 'lib/shop_bunny/cart_module.rb', line 97

def item_model
  ShopBunny.item_model_class_name.constantize
end

#item_sumObject

Calculates the sum of all cart_items, excluding the coupons discount!



34
35
36
37
38
39
40
41
42
43
# File 'lib/shop_bunny/cart_module.rb', line 34

def item_sum
  begin
    self.cart_items.inject(0) do |sum,e|
      sum += e.quantity*e.item.price unless e.item.nil?
    end
  rescue Exception => err
    logger.warn "item_sum could not be calculated: #{err}"
    "error: item not processible"
  end
end

#itemsObject



16
17
18
# File 'lib/shop_bunny/cart_module.rb', line 16

def items
  self.cart_items
end

#items_with_couponsObject



45
46
47
48
49
50
51
52
53
# File 'lib/shop_bunny/cart_module.rb', line 45

def items_with_coupons
  sum = item_sum

  absolute_discount = coupons.sum(:discount_credit)
  relative_discount = coupons.inject(1) {|s,coupon| s * coupon.discount_percentage }

  sum -= absolute_discount
  sum *= relative_discount
end

#remove_item(item, options = {}) ⇒ Object

Decreases the quantity of an item in the cart by 1 or options



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/shop_bunny/cart_module.rb', line 70

def remove_item(item,options = {})
  cart_item = find_cart_item(item)
  if cart_item
    options[:quantity] ||= cart_item.quantity
    if cart_item
      cart_item.quantity -= options[:quantity]
      cart_item.save!
      self.reload
    end
  end
end

#shipping_cost_calculatorObject



92
93
94
# File 'lib/shop_bunny/cart_module.rb', line 92

def shipping_cost_calculator
  ShopBunny.shipping_cost_calculator
end

#shipping_costs(options = {}) ⇒ Object



28
29
30
31
# File 'lib/shop_bunny/cart_module.rb', line 28

def shipping_costs(options = {})
  return 0 if coupons.any?(&:removes_shipping_cost)
  shipping_cost_calculator.costs_for(self, options)
end

#totalObject

Calculates the total sum and applies the coupons discount!



56
57
58
# File 'lib/shop_bunny/cart_module.rb', line 56

def total
  [0, items_with_coupons + shipping_costs].max
end

#update_automatic_coupons!Object



127
128
129
130
131
132
# File 'lib/shop_bunny/cart_module.rb', line 127

def update_automatic_coupons!
  coupon_uses.joins(:coupon).where('coupons.value_of_automatic_add IS NOT NULL').destroy_all
  Coupon.valid.automatically_added_over(item_sum).each do |coupon|
    coupons << coupon
  end
end

#update_item(item, options) ⇒ Object

Sets the quantity of the item to options



83
84
85
86
87
88
89
90
# File 'lib/shop_bunny/cart_module.rb', line 83

def update_item(item,options)
  cart_item = find_cart_item(item)
  if cart_item
    cart_item.quantity = options[:quantity]
    cart_item.save!
    self.reload
  end
end