Class: Workarea::WishList
- Inherits:
-
Object
- Object
- Workarea::WishList
- Includes:
- ApplicationDocument, UrlToken
- Defined in:
- app/models/workarea/wish_list.rb
Defined Under Namespace
Modules: Pricing Classes: InvalidToken, Item, PublicSearch, Request
Class Method Summary collapse
-
.find_by_token(token) ⇒ WishList
Find a wish list by the share token.
-
.find_products(user_id) ⇒ Array
Find a list of product ids in a user’s wish list.
-
.for_user(user_id) ⇒ WishList
Gets a wish list for a user.
-
.search(query, location = '') ⇒ Array
Find wish lists for public viewing.
Instance Method Summary collapse
-
#add_item(product_id, sku, quantity = 1, details = {}, customizations = {}) ⇒ Boolean
Add a wish list item.
-
#mark_item_purchased(sku, quantity) ⇒ self
Mark the items as being purchased.
- #price_adjustments ⇒ Object
-
#purchased_items ⇒ Array
Get items from this list that have been purchased.
-
#quantity ⇒ Integer
How many valid units are in the wish list.
-
#remove_item(sku) ⇒ self
Remove a SKU from the wish list.
- #reset_pricing! ⇒ Object
- #segment_ids ⇒ Object
-
#unpurchased_items ⇒ Array
Get unpurchased items.
-
#update_item_quantity(item_id, quantity) ⇒ Boolean
Update a wish list item’s quantity.
Class Method Details
.find_by_token(token) ⇒ WishList
Find a wish list by the share token
68 69 70 71 72 73 74 75 |
# File 'app/models/workarea/wish_list.rb', line 68 def self.find_by_token(token) raise(InvalidToken, token) if token.blank? Workarea::WishList.where( token: token, :privacy.ne => 'private' ).first || raise(InvalidToken, token) end |
.find_products(user_id) ⇒ Array
Find a list of product ids in a user’s wish list
83 84 85 86 |
# File 'app/models/workarea/wish_list.rb', line 83 def self.find_products(user_id) wish_list = Workarea::WishList.where(user_id: user_id).first wish_list ? wish_list.items.map(&:product_id) : [] end |
.for_user(user_id) ⇒ WishList
Gets a wish list for a user.
40 41 42 43 |
# File 'app/models/workarea/wish_list.rb', line 40 def self.for_user(user_id) existing = Workarea::WishList.where(user_id: user_id).first existing || Workarea::WishList.create!(user_id: user_id) end |
.search(query, location = '') ⇒ Array
Find wish lists for public viewing. Only returns wish lists with public privacy setting.
55 56 57 58 59 60 |
# File 'app/models/workarea/wish_list.rb', line 55 def self.search(query, location = '') Workarea::WishList::PublicSearch.new( query, location ).results end |
Instance Method Details
#add_item(product_id, sku, quantity = 1, details = {}, customizations = {}) ⇒ Boolean
Add a wish list item
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'app/models/workarea/wish_list.rb', line 127 def add_item(product_id, sku, quantity = 1, details = {}, customizations = {}) if existing_item = items.where(sku: sku, customizations: customizations).first change_quantity(sku, existing_item.quantity + quantity) else items.build( product_id: product_id, sku: sku, quantity: quantity, details: details, customizations: customizations ) end save end |
#mark_item_purchased(sku, quantity) ⇒ self
Mark the items as being purchased
164 165 166 167 168 169 170 171 |
# File 'app/models/workarea/wish_list.rb', line 164 def mark_item_purchased(sku, quantity) item = items.detect { |i| i.sku == sku } if item item.received = item.received + quantity item.purchased = true end save! end |
#price_adjustments ⇒ Object
184 185 186 187 188 |
# File 'app/models/workarea/wish_list.rb', line 184 def price_adjustments PriceAdjustmentSet.new( (items.map(&:price_adjustments).flatten || []) ) end |
#purchased_items ⇒ Array
Get items from this list that have been purchased
114 115 116 |
# File 'app/models/workarea/wish_list.rb', line 114 def purchased_items items.select(&:purchased?) end |
#quantity ⇒ Integer
How many valid units are in the wish list
98 99 100 |
# File 'app/models/workarea/wish_list.rb', line 98 def quantity items.select(&:valid?).sum(&:quantity) end |
#remove_item(sku) ⇒ self
Remove a SKU from the wish list
179 180 181 182 |
# File 'app/models/workarea/wish_list.rb', line 179 def remove_item(sku) items.where(sku: sku).first.delete save end |
#reset_pricing! ⇒ Object
190 191 192 193 |
# File 'app/models/workarea/wish_list.rb', line 190 def reset_pricing! items.each(&:reset_pricing!) self end |
#segment_ids ⇒ Object
195 196 197 |
# File 'app/models/workarea/wish_list.rb', line 195 def segment_ids [] end |
#unpurchased_items ⇒ Array
Get unpurchased items
106 107 108 |
# File 'app/models/workarea/wish_list.rb', line 106 def unpurchased_items items.reject(&:purchased?) end |
#update_item_quantity(item_id, quantity) ⇒ Boolean
Update a wish list item’s quantity
152 153 154 155 156 |
# File 'app/models/workarea/wish_list.rb', line 152 def update_item_quantity(item_id, quantity) item = items.where(id: item_id).first return false if item.nil? item.update_attributes(quantity: quantity) end |