diff --git a/app/assets/javascripts/darkswarm/services/cart.js.coffee b/app/assets/javascripts/darkswarm/services/cart.js.coffee index c519d22fdd..0135416ac4 100644 --- a/app/assets/javascripts/darkswarm/services/cart.js.coffee +++ b/app/assets/javascripts/darkswarm/services/cart.js.coffee @@ -8,6 +8,7 @@ Darkswarm.factory 'Cart', (CurrentOrder, Variants, $timeout, $http)-> for line_item in @line_items line_item.variant.line_item = line_item Variants.register line_item.variant + line_item.variant.extended_name = @extendedVariantName(line_item.variant) orderChanged: => @unsaved() @@ -67,4 +68,12 @@ Darkswarm.factory 'Cart', (CurrentOrder, Variants, $timeout, $http)-> variant: variant quantity: null max_quantity: null - @line_items.push variant.line_item \ No newline at end of file + @line_items.push variant.line_item + + extendedVariantName: (variant) => + if variant.product_name == variant.name_to_display + variant.product_name + else + name = "#{variant.product_name} - #{variant.name_to_display}" + name += " (#{variant.options_text})" if variant.options_text + name diff --git a/app/serializers/api/variant_serializer.rb b/app/serializers/api/variant_serializer.rb index 5871d6def4..f995fa4345 100644 --- a/app/serializers/api/variant_serializer.rb +++ b/app/serializers/api/variant_serializer.rb @@ -1,6 +1,6 @@ class Api::VariantSerializer < ActiveModel::Serializer attributes :id, :is_master, :count_on_hand, :name_to_display, :unit_to_display, - :on_demand, :price, :fees, :price_with_fees + :options_text, :on_demand, :price, :fees, :price_with_fees, :product_name def price_with_fees object.price_with_fees(options[:current_distributor], options[:current_order_cycle]) @@ -13,4 +13,9 @@ class Api::VariantSerializer < ActiveModel::Serializer def fees object.fees_by_type_for(options[:current_distributor], options[:current_order_cycle]) end + + def product_name + object.product.name + end + end diff --git a/app/views/shared/menu/_cart.html.haml b/app/views/shared/menu/_cart.html.haml index b0e44f3bad..716d198f67 100644 --- a/app/views/shared/menu/_cart.html.haml +++ b/app/views/shared/menu/_cart.html.haml @@ -20,7 +20,7 @@ / %em {{ line_item.variant.unit_to_display }} / - if {{ line_item.product.name }} == {{ line_item.variant.name_to_display }} %strong - {{ line_item.variant.name_to_display }} + {{ line_item.variant.extended_name }} .columns.small-3.text-right %small diff --git a/spec/javascripts/unit/darkswarm/services/cart_spec.js.coffee b/spec/javascripts/unit/darkswarm/services/cart_spec.js.coffee index 4d13ccc8b5..4be1a13dd8 100644 --- a/spec/javascripts/unit/darkswarm/services/cart_spec.js.coffee +++ b/spec/javascripts/unit/darkswarm/services/cart_spec.js.coffee @@ -6,7 +6,10 @@ describe 'Cart service', -> beforeEach -> module 'Darkswarm' - variant = {id: 1} + variant = + id: 1 + name_to_display: 'name' + product_name: 'name' order = { line_items: [ variant: variant @@ -23,6 +26,9 @@ describe 'Cart service', -> it "registers variants with the Variants service", -> expect(Variants.variants[1]).toBe variant + it "generates extended variant names", -> + expect(Cart.line_items[0].variant.extended_name).toEqual "name" + it "creates and backreferences new line items if necessary", -> Cart.register_variant(v2 = {id: 2}) expect(Cart.line_items[1].variant).toBe v2 @@ -37,3 +43,23 @@ describe 'Cart service', -> expect(Cart.line_items_present()).toEqual [] order.line_items[0].quantity = 2 expect(Cart.total_item_count()).toEqual 2 + + describe "generating an extended variant name", -> + it "returns the product name when it is the same as the variant name", -> + variant = {product_name: 'product_name', name_to_display: 'product_name'} + expect(Cart.extendedVariantName(variant)).toEqual "product_name" + + describe "when the product name and the variant name differ", -> + it "returns a combined name when there is no options text", -> + variant = + product_name: 'product_name' + name_to_display: 'name_to_display' + expect(Cart.extendedVariantName(variant)).toEqual "product_name - name_to_display" + + it "returns a combined name when there is some options text", -> + variant = + product_name: 'product_name' + name_to_display: 'name_to_display' + options_text: 'options_text' + + expect(Cart.extendedVariantName(variant)).toEqual "product_name - name_to_display (options_text)"