Module: ShopBunny::CartModule
- Included in:
- Cart
- Defined in:
- lib/shop_bunny/cart_module.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#add_item(item, options = {}) ⇒ Object
increases the quantity of an article.
-
#as_json(options = {}) ⇒ Object
Make.
-
#clear! ⇒ Object
Remove all items from the cart.
-
#empty? ⇒ Boolean
Check if the cart is empty.
- #item_count ⇒ Object
-
#item_model ⇒ Object
Returns the class/model of the items you can buy.
-
#item_sum ⇒ Object
Calculates the sum of all cart_items, excluding the coupons discount!.
- #items ⇒ Object
- #items_with_coupons ⇒ Object
-
#remove_item(item, options = {}) ⇒ Object
removes a quantity of an article specified by :article_id, returns nil if no article has been found.
- #shipping_cost_calculator ⇒ Object
- #shipping_costs(options = {}) ⇒ Object
-
#total ⇒ Object
Calculates the total sum and applies the coupons discount!.
- #update_automatic_coupons! ⇒ Object
-
#update_item(item, options) ⇒ Object
sets the quantity of an article specified by :article_id, returns nil if no article has been found.
Class Method Details
.included(clazz) ⇒ Object
4 5 6 7 8 9 10 11 12 13 |
# 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) clazz.send(:has_many, :coupon_uses, :dependent => :destroy) 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(:attr_accessible, :coupon_code, :cart_items_attributes) clazz.send(:validate, :coupon_code_must_be_valid) end |
Instance Method Details
#add_item(item, options = {}) ⇒ Object
increases the quantity of an article. creates a new one if it doesn’t exist
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/shop_bunny/cart_module.rb', line 57 def add_item(item, = {}) [:quantity] ||= 1 cart_item = self.cart_items.select {|e| e.item.id == item.id}.first cart_item ||= self.cart_items.build(:item => item) cart_item.quantity += [:quantity] cart_item.save! update_automatic_coupons! self.save! self.reload cart_item end |
#as_json(options = {}) ⇒ Object
Make
127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/shop_bunny/cart_module.rb', line 127 def as_json(={}) { :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 |
#clear! ⇒ Object
Remove all items from the cart
117 118 119 120 121 122 123 124 |
# File 'lib/shop_bunny/cart_module.rb', line 117 def clear! cart_items.each {|i| i.destroy} cart_items.clear coupon_uses.each {|u| u.destroy} coupon_uses.clear coupons.clear end |
#empty? ⇒ Boolean
Check if the cart is empty
112 113 114 |
# File 'lib/shop_bunny/cart_module.rb', line 112 def empty? cart_items.empty? end |
#item_count ⇒ Object
19 20 21 |
# File 'lib/shop_bunny/cart_module.rb', line 19 def item_count self.cart_items.inject(0) {|sum,e| sum += e.quantity} end |
#item_model ⇒ Object
Returns the class/model of the items you can buy. (Products)
107 108 109 |
# File 'lib/shop_bunny/cart_module.rb', line 107 def item_model ShopBunny.item_model_class_name.constantize end |
#item_sum ⇒ Object
Calculates the sum of all cart_items, excluding the coupons discount!
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/shop_bunny/cart_module.rb', line 29 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 |
#items ⇒ Object
15 16 17 |
# File 'lib/shop_bunny/cart_module.rb', line 15 def items self.cart_items end |
#items_with_coupons ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/shop_bunny/cart_module.rb', line 40 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
removes a quantity of an article specified by :article_id, returns nil if no article has been found
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/shop_bunny/cart_module.rb', line 72 def remove_item(item, = {}) cart_item = self.cart_items.select {|e| e.item.id == item.id}.first if cart_item [:quantity] ||= cart_item.quantity if cart_item cart_item.quantity -= [:quantity] cart_item.save! self.reload end end update_automatic_coupons! cart_item end |
#shipping_cost_calculator ⇒ Object
102 103 104 |
# File 'lib/shop_bunny/cart_module.rb', line 102 def shipping_cost_calculator ShopBunny.shipping_cost_calculator end |
#shipping_costs(options = {}) ⇒ Object
23 24 25 26 |
# File 'lib/shop_bunny/cart_module.rb', line 23 def shipping_costs( = {}) return 0 if coupons.any?(&:removes_shipping_cost) shipping_cost_calculator.costs_for(self, ) end |
#total ⇒ Object
Calculates the total sum and applies the coupons discount!
51 52 53 54 |
# File 'lib/shop_bunny/cart_module.rb', line 51 def total [0, items_with_coupons + shipping_costs].max end |
#update_automatic_coupons! ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/shop_bunny/cart_module.rb', line 142 def update_automatic_coupons! coupon_uses.each do |use| use.destroy if use.coupon.value_of_automatic_add end save reload Coupon.valid.automatically_added_over(item_sum).each do |coupon| coupons << coupon end end |
#update_item(item, options) ⇒ Object
sets the quantity of an article specified by :article_id, returns nil if no article has been found
89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/shop_bunny/cart_module.rb', line 89 def update_item(item,) cart_item = self.cart_items.select {|e| e.item.id == item.id}.first if cart_item cart_item.quantity = [:quantity] cart_item.save! self.reload end update_automatic_coupons! cart_item end |