Class: Wishlist
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Wishlist
- Defined in:
- app/models/wishlist.rb
Overview
belongs_to
-
user
- User
has_many
-
products_wishlist
- ProductsWishlist -
products
- Product
Instance Method Summary collapse
-
#add_product(product, quantity = 1) ⇒ Object
Add a product in this wishlist.
-
#add_product_id(product_id, quantity = 1) ⇒ Object
Add a product in this wishlist.
-
#count_products ⇒ Object
Returns product’s count of this wishlist.
-
#is_empty? ⇒ Boolean
Returns true if wishlist is empty, returns false else.
-
#remove_product(product) ⇒ Object
Remove a product of this wishlist.
-
#remove_product_id(product_id) ⇒ Object
Remove a product of this wishlist.
-
#set_quantity(product, quantity) ⇒ Object
Update quantity of a Product.
-
#size(product = nil) ⇒ Object
Returns the total quantity of this wishlist by default.
-
#to_empty ⇒ Object
Empty this wishlist.
-
#total(with_tax = false, product = nil) ⇒ Object
Returns total price of this wishlist by default.
-
#weight ⇒ Object
Returns weight of this wishlist.
Instance Method Details
#add_product(product, quantity = 1) ⇒ Object
Add a product in this wishlist
Returns false if product is nil or not recorded
Parameters
-
:product
- a Product object -
:quantity
- the quantity (1 by default)
If this product is already in this wishlist, the quantity is add with the actual quantity
21 22 23 24 25 26 27 28 29 30 31 |
# File 'app/models/wishlist.rb', line 21 def add_product(product, quantity=1) return false if product.nil? || product.new_record? # if wishlist include product : add quantity # else add product with set_quantity unless products.include? product products_wishlists << ProductsWishlist.create(:product_id => product.id, :quantity => quantity) else set_quantity(product, size(product) + quantity) end end |
#add_product_id(product_id, quantity = 1) ⇒ Object
Add a product in this wishlist
Returns false if product is nil or not recorded
Parameters
-
:product_id
- an id of a Product -
:quantity
- the quantity (1 by default)
This method use add_product
41 42 43 |
# File 'app/models/wishlist.rb', line 41 def add_product_id(product_id, quantity=1) add_product(Product.find_by_id(product_id), quantity) end |
#count_products ⇒ Object
Returns product’s count of this wishlist
101 102 103 |
# File 'app/models/wishlist.rb', line 101 def count_products products.size end |
#is_empty? ⇒ Boolean
Returns true if wishlist is empty, returns false else
111 112 113 |
# File 'app/models/wishlist.rb', line 111 def is_empty? count_products == 0 end |
#remove_product(product) ⇒ Object
Remove a product of this wishlist
Returns false if product is nil
Parameters
-
:product
- a Product object
52 53 54 55 56 |
# File 'app/models/wishlist.rb', line 52 def remove_product(product) return false if product.nil? products_wishlists.find_by_product_id(product.id).destroy products_wishlists.reject! { |products_wishlist| products_wishlist.product_id == product.id } end |
#remove_product_id(product_id) ⇒ Object
Remove a product of this wishlist
Returns false if product is nil
Parameters
-
:product_id
- an id of a Product
This method use remove_product
65 66 67 |
# File 'app/models/wishlist.rb', line 65 def remove_product_id(product_id) remove_product(Product.find_by_id(product_id)) end |
#set_quantity(product, quantity) ⇒ Object
Update quantity of a Product
Parameters
-
:product
- a Product object -
:quantity
- the new quantity
74 75 76 77 78 79 80 81 82 |
# File 'app/models/wishlist.rb', line 74 def set_quantity(product, quantity) quantity = quantity.to_i products_wishlists.each do |products_wishlist| if products_wishlist.product_id == product.id return remove_product(product) if quantity == 0 products_wishlist.update_attribute(:quantity, quantity) end end end |
#size(product = nil) ⇒ Object
Returns the total quantity of this wishlist by default
Returns the product’s quantity if you precise a Product object in parameters
Parameters
-
:product
- a Product object
90 91 92 93 94 95 96 97 98 |
# File 'app/models/wishlist.rb', line 90 def size(product=nil) if product.nil? return products_wishlists.inject(0) { |total, products_wishlist| total + products_wishlist.quantity } else products_wishlist = products_wishlists.find_by_product_id(product.id) return products_wishlist.quantity unless products_wishlist.nil? end return 0 end |
#to_empty ⇒ Object
Empty this wishlist
106 107 108 |
# File 'app/models/wishlist.rb', line 106 def to_empty products_wishlists.destroy_all end |
#total(with_tax = false, product = nil) ⇒ Object
Returns total price of this wishlist by default
Returns total price of a Product in wishlist if you precise in parameters
Parameters
-
:with_tax
- add tax of price if true, false by default -
:product
- a Product object
122 123 124 125 126 127 128 |
# File 'app/models/wishlist.rb', line 122 def total(with_tax=false, product=nil) if product.nil? ProductsWishlist.find_all_by_wishlist_id(id).inject(0) { |total, products_wishlist| total + products_wishlist.total(with_tax) } else ProductsWishlist.find_all_by_wishlist_id_and_product_id(id, product.id).inject(0) { |total, products_wishlist| total + products_wishlist.total(with_tax) } end end |
#weight ⇒ Object
Returns weight of this wishlist
131 132 133 |
# File 'app/models/wishlist.rb', line 131 def weight products.inject(0) { |total, product| total + product.weight } end |