Class: Cart
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Cart
- Includes:
- ActionView::Helpers::NumberHelper
- Defined in:
- app/models/cart.rb
Instance Attribute Summary collapse
-
#currency ⇒ Object
readonly
Returns the value of attribute currency.
Instance Method Summary collapse
-
#add(product, quantity = 1) ⇒ Object
Add n number of this product to our cart (addition).
- #alter_cart_item_quantity(change_type, product, quantity) ⇒ Object
- #empty! ⇒ Object
- #empty? ⇒ Boolean
- #item_count ⇒ Object
-
#items ⇒ Object
Because ‘has_many :cart_items, :as => :items’ does not appear to work.
- #paypal_encrypted(return_url, notify_url) ⇒ Object
- #purchased? ⇒ Boolean
-
#remove(product) ⇒ Object
Remove a product no matter what the quantity.
-
#set_item_quantity(product, quantity) ⇒ Object
Set the total number of this product in our cart to quantity.
- #shipping_weight ⇒ Object
- #total ⇒ Object
- #totalf ⇒ Object
Instance Attribute Details
#currency ⇒ Object (readonly)
Returns the value of attribute currency.
2 3 4 |
# File 'app/models/cart.rb', line 2 def currency @currency end |
Instance Method Details
#add(product, quantity = 1) ⇒ Object
Add n number of this product to our cart (addition)
17 18 19 |
# File 'app/models/cart.rb', line 17 def add(product, quantity=1) self.alter_cart_item_quantity(:add, product, quantity.to_i) end |
#alter_cart_item_quantity(change_type, product, quantity) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'app/models/cart.rb', line 31 def alter_cart_item_quantity(change_type, product, quantity) item = self.items.detect{|item| item.product_id == product.id} if item if quantity >= 1 case change_type when :add then item.increment_quantity(quantity) when :set then item.quantity = quantity end else item.remove_from_cart! end else item = self.items.build(:product_id => product.id, :quantity => quantity) end item.save! end |
#empty! ⇒ Object
64 65 66 |
# File 'app/models/cart.rb', line 64 def empty! self.items.each{ |cart_item| cart_item.remove_from_cart! } end |
#empty? ⇒ Boolean
60 61 62 |
# File 'app/models/cart.rb', line 60 def empty? self.items.empty? end |
#item_count ⇒ Object
68 69 70 71 72 |
# File 'app/models/cart.rb', line 68 def item_count count = 0 self.items.each{|item| count += item.quantity} count end |
#items ⇒ Object
Because ‘has_many :cart_items, :as => :items’ does not appear to work
10 |
# File 'app/models/cart.rb', line 10 def items ; self.cart_items ; end |
#paypal_encrypted(return_url, notify_url) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'app/models/cart.rb', line 76 def paypal_encrypted(return_url, notify_url) values = { :invoice => self.id, :notify_url => notify_url, :business => ECO['paypal']['email'], :cert_id => ECO['paypal']['cert_id'], :cmd => '_cart', :upload => 1, :weight => self.shipping_weight, :return => order_confirmation_path() } self.items.each_with_index do |item, index| values.merge!({ "amount_#{index+1}" => item.unit_price, "item_name_#{index+1}" => item.name, "item_number_#{index+1}" => item.id, "quantity_#{index+1}" => item.quantity }) end encrypt_for_paypal(values) end |
#purchased? ⇒ Boolean
12 13 14 |
# File 'app/models/cart.rb', line 12 def purchased? self.purchased_at.present? end |
#remove(product) ⇒ Object
Remove a product no matter what the quantity
22 23 24 |
# File 'app/models/cart.rb', line 22 def remove(product) self.alter_cart_item_quantity(:set, product, 0) end |
#set_item_quantity(product, quantity) ⇒ Object
Set the total number of this product in our cart to quantity. Setting to 0 will remove the item
27 28 29 |
# File 'app/models/cart.rb', line 27 def set_item_quantity(product, quantity) self.alter_cart_item_quantity(:set, product, quantity) end |
#shipping_weight ⇒ Object
56 57 58 |
# File 'app/models/cart.rb', line 56 def shipping_weight self.items.inject(0) { |sum, cart_item| sum + cart_item.shipping_weight } end |
#total ⇒ Object
52 53 54 |
# File 'app/models/cart.rb', line 52 def total self.items.inject(0) { |sum, cart_item| sum + cart_item.price } end |
#totalf ⇒ Object
48 49 50 |
# File 'app/models/cart.rb', line 48 def totalf Cart.format_price(self.total()) end |