From 562f8dbd4e7d248b2bbe6984ed93554bbcc52807 Mon Sep 17 00:00:00 2001 From: Will Marshall Date: Fri, 18 Jul 2014 11:21:51 +1000 Subject: [PATCH] Starting to restructure data injection --- .../darkswarm/services/cart.js.coffee | 6 ++++-- .../darkswarm/services/products.js.coffee | 2 +- app/helpers/injection_helper.rb | 2 +- app/serializers/api/image_serializer.rb | 11 +++++++++++ app/serializers/api/line_item_serializer.rb | 4 +++- app/serializers/api/variant_serializer.rb | 10 ++++++++++ .../products_controller_spec.js.coffee | 10 +++++----- .../darkswarm/services/product_spec.js.coffee | 14 +++++++------- spec/serializers/current_order_serializer.rb | 19 +++++++++++++++++++ 9 files changed, 61 insertions(+), 17 deletions(-) create mode 100644 app/serializers/api/image_serializer.rb create mode 100644 app/serializers/api/variant_serializer.rb create mode 100644 spec/serializers/current_order_serializer.rb diff --git a/app/assets/javascripts/darkswarm/services/cart.js.coffee b/app/assets/javascripts/darkswarm/services/cart.js.coffee index 2567d6a90c..18b36bf734 100644 --- a/app/assets/javascripts/darkswarm/services/cart.js.coffee +++ b/app/assets/javascripts/darkswarm/services/cart.js.coffee @@ -2,6 +2,8 @@ Darkswarm.factory 'Cart', (CurrentOrder)-> # Handles syncing of current cart/order state to server new class Cart order: CurrentOrder.order + line_items: CurrentOrder.order.line_items + constructor: -> + console.log @order.line_items + - # How to structure this? - diff --git a/app/assets/javascripts/darkswarm/services/products.js.coffee b/app/assets/javascripts/darkswarm/services/products.js.coffee index 05ac50c2fd..c334a306a6 100644 --- a/app/assets/javascripts/darkswarm/services/products.js.coffee +++ b/app/assets/javascripts/darkswarm/services/products.js.coffee @@ -13,7 +13,7 @@ Darkswarm.factory 'Products', ($resource, Enterprises, Dereferencer, Taxons) -> @products = $resource("/shop/products").query (products)=> @extend() @dereference() - @loading = false + @loading = false @ dereference: -> diff --git a/app/helpers/injection_helper.rb b/app/helpers/injection_helper.rb index 183d8e128f..c639d22104 100644 --- a/app/helpers/injection_helper.rb +++ b/app/helpers/injection_helper.rb @@ -4,7 +4,7 @@ module InjectionHelper end def inject_current_order - inject_json_ams "currentOrder", current_order, Api::CurrentOrderSerializer + inject_json_ams "currentOrder", current_order, Api::CurrentOrderSerializer, current_distributor: current_distributor, current_order_cycle: current_order_cycle end def inject_available_shipping_methods diff --git a/app/serializers/api/image_serializer.rb b/app/serializers/api/image_serializer.rb new file mode 100644 index 0000000000..fa67e9b1d5 --- /dev/null +++ b/app/serializers/api/image_serializer.rb @@ -0,0 +1,11 @@ +class Api::ImageSerializer < ActiveModel::Serializer + attributes :id, :alt, :small_url, :large_url + + def small_url + object.attachment.url(:small, false) + end + + def large_url + object.attachment.url(:large, false) + end +end diff --git a/app/serializers/api/line_item_serializer.rb b/app/serializers/api/line_item_serializer.rb index f0d2c0de63..d791febdfc 100644 --- a/app/serializers/api/line_item_serializer.rb +++ b/app/serializers/api/line_item_serializer.rb @@ -1,3 +1,5 @@ class Api::LineItemSerializer < ActiveModel::Serializer - attributes :id, :variant_id, :quantity, :price + attributes :id, :quantity, :price + + has_one :variant, serializer: Api::VariantSerializer end diff --git a/app/serializers/api/variant_serializer.rb b/app/serializers/api/variant_serializer.rb new file mode 100644 index 0000000000..ff7c3a8907 --- /dev/null +++ b/app/serializers/api/variant_serializer.rb @@ -0,0 +1,10 @@ +class Api::VariantSerializer < ActiveModel::Serializer + attributes :id, :is_master, :count_on_hand, :name_to_display, :on_demand, + :price + + has_many :images, serializer: Api::ImageSerializer + + def price + object.price_with_fees(options[:current_distributor], options[:current_order_cycle]) + end +end diff --git a/spec/javascripts/unit/darkswarm/controllers/products_controller_spec.js.coffee b/spec/javascripts/unit/darkswarm/controllers/products_controller_spec.js.coffee index 0aaf0eaaf3..1d56a79d27 100644 --- a/spec/javascripts/unit/darkswarm/controllers/products_controller_spec.js.coffee +++ b/spec/javascripts/unit/darkswarm/controllers/products_controller_spec.js.coffee @@ -2,11 +2,11 @@ describe 'ProductsCtrl', -> ctrl = null scope = null event = null - Product = null + Products = null beforeEach -> module('Darkswarm') - Product = + Products = all: -> update: -> products: ["testy mctest"] @@ -15,10 +15,10 @@ describe 'ProductsCtrl', -> inject ($controller) -> scope = {} - ctrl = $controller 'ProductsCtrl', {$scope: scope, Product: Product, OrderCycle: OrderCycle} + ctrl = $controller 'ProductsCtrl', {$scope: scope, Products: Products, OrderCycle: OrderCycle} - it 'fetches products from Product', -> - expect(scope.Product.products).toEqual ['testy mctest'] + it 'fetches products from Products', -> + expect(scope.Products.products).toEqual ['testy mctest'] it "increments the limit up to the number of products", -> scope.limit = 0 diff --git a/spec/javascripts/unit/darkswarm/services/product_spec.js.coffee b/spec/javascripts/unit/darkswarm/services/product_spec.js.coffee index 50b4138b77..e3b975b23b 100644 --- a/spec/javascripts/unit/darkswarm/services/product_spec.js.coffee +++ b/spec/javascripts/unit/darkswarm/services/product_spec.js.coffee @@ -1,6 +1,6 @@ -describe 'Product service', -> +describe 'Products service', -> $httpBackend = null - Product = null + Products = null Enterprises = null CurrentHubMock = {} product = @@ -15,30 +15,30 @@ describe 'Product service', -> $provide.value "CurrentHub", CurrentHubMock null inject ($injector, _$httpBackend_)-> - Product = $injector.get("Product") + Products = $injector.get("Products") Enterprises = $injector.get("Enterprises") $httpBackend = _$httpBackend_ it "Fetches products from the backend on init", -> $httpBackend.expectGET("/shop/products").respond([product]) $httpBackend.flush() - expect(Product.products[0].test).toEqual "cats" + expect(Products.products[0].test).toEqual "cats" it "dereferences suppliers", -> Enterprises.enterprises_by_id = {id: 9, name: "test"} $httpBackend.expectGET("/shop/products").respond([{supplier : {id: 9}}]) $httpBackend.flush() - expect(Product.products[0].supplier).toBe Enterprises.enterprises_by_id["9"] + expect(Products.products[0].supplier).toBe Enterprises.enterprises_by_id["9"] describe "determining the price to display for a product", -> it "displays the product price when the product does not have variants", -> $httpBackend.expectGET("/shop/products").respond([product]) $httpBackend.flush() - expect(Product.products[0].price).toEqual 11.00 + expect(Products.products[0].price).toEqual 11.00 it "displays the minimum variant price when the product has variants", -> product.variants = [{price: 22}, {price: 33}] $httpBackend.expectGET("/shop/products").respond([product]) $httpBackend.flush() - expect(Product.products[0].price).toEqual 22 + expect(Products.products[0].price).toEqual 22 diff --git a/spec/serializers/current_order_serializer.rb b/spec/serializers/current_order_serializer.rb new file mode 100644 index 0000000000..a3513b6413 --- /dev/null +++ b/spec/serializers/current_order_serializer.rb @@ -0,0 +1,19 @@ +describe Api::CurrentOrderSerializer do + let(:distributor) { create(:distributor_enterprise) } + let(:oc) { create(:simple_order_cycle) } + let(:li) { create(:line_item, variant: create(:variant)) } + let(:order) { create(:order, line_items: [li]) } + let(:serializer) { Api::CurrentOrderSerializer.new(order, current_distributor: distributor, current_order_cycle: oc ).to_json } + + it "serializers the current order" do + serializer.should match order.id.to_s + end + + it "includes line items" do + serializer.should match li.id.to_s + end + + it "includes variants of line items" do + serializer.should match li.variant.name + end +end