# ==== belongs_to
# * user - User
#
# ==== has_many
# * products_wishlist - ProductsWishlist
# * products - Product
class Wishlist < ActiveRecord::Base
has_many :products_wishlists, :dependent => :destroy
has_many :products, :through => :products_wishlists
belongs_to :user
# 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
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 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
def add_product_id(product_id, quantity=1)
add_product(Product.find_by_id(product_id), quantity)
end
# Remove a product of this wishlist
#
# Returns false if product is nil
#
# ==== Parameters
#
# * :product - a Product object
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 a product of this wishlist
#
# Returns false if product is nil
#
# ==== Parameters
# * :product_id - an id of a Product
# This method use remove_product
def remove_product_id(product_id)
remove_product(Product.find_by_id(product_id))
end
# Update quantity of a Product
#
# ==== Parameters
# * :product - a Product object
# * :quantity - the new quantity
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
# 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
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
# Returns product's count of this wishlist
def count_products
products.size
end
# Empty this wishlist
def to_empty
products_wishlists.destroy_all
end
# Returns true if wishlist is empty, returns false else
def is_empty?
count_products == 0
end
# 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
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
# Returns weight of this wishlist
def weight
products.inject(0) { |total, product| total + product.weight }
end
end