From b25def810c697c17a925b5fd4856d3f0b5075ecf Mon Sep 17 00:00:00 2001 From: Will Marshall Date: Fri, 18 Jul 2014 14:56:15 +1000 Subject: [PATCH] Working version of the cart integration --- .../darkswarm/services/cart.js.coffee | 16 +++++++------ .../darkswarm/services/products.js.coffee | 24 ++++++++++++------- app/views/shared/menu/_cart.html.haml | 2 +- app/views/shop/products/_master.html.haml | 3 +++ app/views/shop/products/_variants.html.haml | 3 +++ .../darkswarm/services/cart_spec.js.coffee | 4 ++++ .../darkswarm/services/product_spec.js.coffee | 21 ++++++++++++---- .../services/variants_spec.js.coffee | 9 +++++-- 8 files changed, 58 insertions(+), 24 deletions(-) diff --git a/app/assets/javascripts/darkswarm/services/cart.js.coffee b/app/assets/javascripts/darkswarm/services/cart.js.coffee index 0a1db5c5e9..be2ec66aad 100644 --- a/app/assets/javascripts/darkswarm/services/cart.js.coffee +++ b/app/assets/javascripts/darkswarm/services/cart.js.coffee @@ -8,14 +8,16 @@ Darkswarm.factory 'Cart', (CurrentOrder, Variants)-> line_item.variant.line_item = line_item Variants.register line_item.variant - register_variant: (variant)=> - @create_line_item(variant) unless @line_items.some (li)-> - li.variant == variant + line_items_present: => + @line_items.filter (li)-> + li.quantity > 0 + register_variant: (variant)=> + exists = @line_items.some (li)-> li.variant == variant + @create_line_item(variant) unless exists + create_line_item: (variant)-> - li = + variant.line_item = variant: variant quantity: 0 - variant.line_item = li - @line_items.push li - + @line_items.push variant.line_item diff --git a/app/assets/javascripts/darkswarm/services/products.js.coffee b/app/assets/javascripts/darkswarm/services/products.js.coffee index 5236d66e87..c79d904923 100644 --- a/app/assets/javascripts/darkswarm/services/products.js.coffee +++ b/app/assets/javascripts/darkswarm/services/products.js.coffee @@ -11,9 +11,9 @@ Darkswarm.factory 'Products', ($resource, Enterprises, Dereferencer, Taxons, Car update: => @loading = true @products = $resource("/shop/products").query (products)=> - @extend() - @dereference() - @registerVariants() + @extend() && @dereference() + @registerVariants() + @registerVariantsWithCart() @loading = false @ @@ -21,20 +21,26 @@ Darkswarm.factory 'Products', ($resource, Enterprises, Dereferencer, Taxons, Car for product in @products product.supplier = Enterprises.enterprises_by_id[product.supplier.id] Dereferencer.dereference product.taxons, Taxons.taxons_by_id - + + # May return different objects! If the variant has already been registered + # by another service, we fetch those registerVariants: -> for product in @products if product.variants + variants = [] product.variants = (Variants.register variant for variant in product.variants) - product.master = Variants.register master if product.master + product.master = Variants.register product.master if product.master + + registerVariantsWithCart: -> + for product in @products + if product.variants + for variant in product.variants + Cart.register_variant variant + Cart.register_variant product.master if product.master extend: -> for product in @products if product.variants?.length > 0 prices = (v.price for v in product.variants) product.price = Math.min.apply(null, prices) - product.hasVariants = product.variants?.length > 0 - - # Iterate through variants - # Cart.register_variant(v) diff --git a/app/views/shared/menu/_cart.html.haml b/app/views/shared/menu/_cart.html.haml index 080961922f..1c10cd7bdf 100644 --- a/app/views/shared/menu/_cart.html.haml +++ b/app/views/shared/menu/_cart.html.haml @@ -2,5 +2,5 @@ %span.nav-branded %i.ofn-i_027-shopping-cart %span - {{ Cart.order.line_items.length }} + {{ Cart.line_items_present().length }} items diff --git a/app/views/shop/products/_master.html.haml b/app/views/shop/products/_master.html.haml index c8efb21420..ae0536a726 100644 --- a/app/views/shop/products/_master.html.haml +++ b/app/views/shop/products/_master.html.haml @@ -13,6 +13,7 @@ "ofn-disable-scroll" => true, max: "{{product.on_demand && 9999 || product.count_on_hand }}", name: "variants[{{product.master.id}}]", + "ng-model" => "product.master.line_item.quantity", id: "variants_{{product.master.id}}"} %small x {{ product.master.unit_to_display }} @@ -24,6 +25,7 @@ "ofn-disable-scroll" => true, max: "{{product.on_demand && 9999 || product.count_on_hand }}", name: "variants[{{product.master.id}}]", + "ng-model" => "product.master.line_item.quantity", id: "variants_{{product.master.id}}"} .small-3.columns{"bo-if" => "product.group_buy"} @@ -32,6 +34,7 @@ placeholder: "max", "ofn-disable-scroll" => true, max: "{{product.on_demand && 9999 || product.count_on_hand }}", + "ng-model" => "product.master.line_item.max_quantity", name: "variant_attributes[{{product.master.id}}][max_quantity]"} %small x {{ product.master.unit_to_display }} diff --git a/app/views/shop/products/_variants.html.haml b/app/views/shop/products/_variants.html.haml index f2129abe7b..9586e04b99 100644 --- a/app/views/shop/products/_variants.html.haml +++ b/app/views/shop/products/_variants.html.haml @@ -16,6 +16,7 @@ placeholder: "0", "ofn-disable-scroll" => true, max: "{{variant.on_demand && 9999 || variant.count_on_hand }}", + "ng-model" => "variant.line_item.quantity", name: "variants[{{variant.id}}]", id: "variants_{{variant.id}}"} %small x {{ variant.unit_to_display }} @@ -26,6 +27,7 @@ min: 0, placeholder: "min", "ofn-disable-scroll" => true, + "ng-model" => "variant.line_item.quantity", max: "{{variant.on_demand && 9999 || variant.count_on_hand }}", name: "variants[{{variant.id}}]", id: "variants_{{variant.id}}"} @@ -34,6 +36,7 @@ min: 0, placeholder: "max", "ofn-disable-scroll" => true, + "ng-model" => "variant.line_item.max_quantity", max: "{{variant.on_demand && 9999 || variant.count_on_hand }}", name: "variant_attributes[{{variant.id}}][max_quantity]"} %small x {{ variant.unit_to_display }} diff --git a/spec/javascripts/unit/darkswarm/services/cart_spec.js.coffee b/spec/javascripts/unit/darkswarm/services/cart_spec.js.coffee index ef4465f91c..4079d70bcb 100644 --- a/spec/javascripts/unit/darkswarm/services/cart_spec.js.coffee +++ b/spec/javascripts/unit/darkswarm/services/cart_spec.js.coffee @@ -26,3 +26,7 @@ describe 'Cart service', -> expect(Cart.line_items[1].variant).toBe v2 expect(Cart.line_items[1].variant.line_item).toBe Cart.line_items[1] + it "returns a list of items actually in the cart", -> + expect(Cart.line_items_present()).toEqual [] + order.line_items[0].quantity = 1 + expect(Cart.line_items_present().length).toEqual 1 diff --git a/spec/javascripts/unit/darkswarm/services/product_spec.js.coffee b/spec/javascripts/unit/darkswarm/services/product_spec.js.coffee index 774f4c8a19..af384adcb6 100644 --- a/spec/javascripts/unit/darkswarm/services/product_spec.js.coffee +++ b/spec/javascripts/unit/darkswarm/services/product_spec.js.coffee @@ -3,10 +3,11 @@ describe 'Products service', -> Products = null Enterprises = null Variants = null + Cart = null CurrentHubMock = {} - currentOrder = - line_items: [] + currentOrder = null product = null + beforeEach -> product = test: "cats" @@ -14,15 +15,20 @@ describe 'Products service', -> id: 9 price: 11 variants: [] + currentOrder = + line_items: [] + module 'Darkswarm' module ($provide)-> $provide.value "CurrentHub", CurrentHubMock $provide.value "currentOrder", currentOrder null + inject ($injector, _$httpBackend_)-> Products = $injector.get("Products") Enterprises = $injector.get("Enterprises") Variants = $injector.get("Variants") + Cart = $injector.get("Cart") $httpBackend = _$httpBackend_ it "Fetches products from the backend on init", -> @@ -37,12 +43,17 @@ describe 'Products service', -> $httpBackend.flush() expect(Products.products[0].supplier).toBe Enterprises.enterprises_by_id["9"] - it "registers variants with the Variants service", -> - spyOn(Variants, "register") + it "registers variants with Variants service", -> product.variants = [{id: 1}] $httpBackend.expectGET("/shop/products").respond([product]) $httpBackend.flush() - expect(Variants.register).toHaveBeenCalledWith product.variants[0] + expect(Products.products[0].variants[0]).toBe Variants.variants[1] + + it "registers variants with the Cart", -> + product.variants = [{id: 8}] + $httpBackend.expectGET("/shop/products").respond([product]) + $httpBackend.flush() + expect(Cart.line_items[0].variant).toBe Products.products[0].variants[0] describe "determining the price to display for a product", -> it "displays the product price when the product does not have variants", -> diff --git a/spec/javascripts/unit/darkswarm/services/variants_spec.js.coffee b/spec/javascripts/unit/darkswarm/services/variants_spec.js.coffee index 73ecca714d..278e0a6850 100644 --- a/spec/javascripts/unit/darkswarm/services/variants_spec.js.coffee +++ b/spec/javascripts/unit/darkswarm/services/variants_spec.js.coffee @@ -1,9 +1,10 @@ describe 'Variants service', -> Variants = null - variant = - id: 1 + variant = null beforeEach -> + variant = + id: 1 module 'Darkswarm' inject ($injector)-> Variants = $injector.get("Variants") @@ -14,3 +15,7 @@ describe 'Variants service', -> it "will return an existing variant rather than duplicating", -> Variants.register(variant) expect(Variants.register({id: 1})).toBe variant + + it "will return the same object as passed", -> + expect(Variants.register(variant)).toBe variant +