diff --git a/app/assets/javascripts/darkswarm/filters/localize_currency.js.coffee b/app/assets/javascripts/darkswarm/filters/localize_currency.js.coffee index 3eab92d007..ffa98c5a33 100644 --- a/app/assets/javascripts/darkswarm/filters/localize_currency.js.coffee +++ b/app/assets/javascripts/darkswarm/filters/localize_currency.js.coffee @@ -4,13 +4,13 @@ # @return: string Darkswarm.filter "localizeCurrency", (currencyConfig)-> (amount) -> - decimals = if currencyConfig.hide_cents then 0 else 2 - amount_fixed = amount.toFixed(2) - currency_str = "" - currency_str = " " + currencyConfig.currency if currencyConfig.display_currency + currency_code = if currencyConfig.display_currency then " " + currencyConfig.currency else "" + decimals = if currencyConfig.hide_cents == "true" then 0 else 2 + # We need to use parseFloat before toFixed as the amount should be a passed in as a string. + amount_fixed = parseFloat(amount).toFixed(decimals) + # Build the final price string. if currencyConfig.symbol_position == 'before' - currencyConfig.symbol + amount_fixed + currency_str + currencyConfig.symbol + amount_fixed + currency_code else - amount_fixed + " " + currencyConfig.symbol + currency_str - + amount_fixed + " " + currencyConfig.symbol + currency_code diff --git a/app/assets/javascripts/darkswarm/services/cart.js.coffee b/app/assets/javascripts/darkswarm/services/cart.js.coffee index def1c9b6d0..2b58bb70ae 100644 --- a/app/assets/javascripts/darkswarm/services/cart.js.coffee +++ b/app/assets/javascripts/darkswarm/services/cart.js.coffee @@ -3,7 +3,7 @@ Darkswarm.factory 'Cart', (CurrentOrder, Variants, $timeout, $http)-> new class Cart dirty: false order: CurrentOrder.order - line_items: CurrentOrder.order?.line_items || [] + line_items: CurrentOrder.order?.line_items || [] constructor: -> for line_item in @line_items line_item.variant.line_item = line_item @@ -22,13 +22,13 @@ Darkswarm.factory 'Cart', (CurrentOrder, Variants, $timeout, $http)-> # TODO what shall we do here? data: => - variants = {} + variants = {} for li in @line_items_present() - variants[li.variant.id] = + variants[li.variant.id] = quantity: li.quantity max_quantity: li.max_quantity {variants: variants} - + saved: => @dirty = false @@ -48,15 +48,15 @@ Darkswarm.factory 'Cart', (CurrentOrder, Variants, $timeout, $http)-> total: => @line_items_present().map (li)-> - li.variant.getPrice() + li.variant.totalPrice() .reduce (t, price)-> t + price , 0 register_variant: (variant)=> exists = @line_items.some (li)-> li.variant == variant - @create_line_item(variant) unless exists - + @create_line_item(variant) unless exists + create_line_item: (variant)-> variant.line_item = variant: variant diff --git a/app/assets/javascripts/darkswarm/services/variants.js.coffee b/app/assets/javascripts/darkswarm/services/variants.js.coffee index 5ec8f06e38..bc2050e4d4 100644 --- a/app/assets/javascripts/darkswarm/services/variants.js.coffee +++ b/app/assets/javascripts/darkswarm/services/variants.js.coffee @@ -5,7 +5,7 @@ Darkswarm.factory 'Variants', -> @variants[variant.id] ||= @extend variant extend: (variant)-> - variant.getPrice = -> + variant.totalPrice = -> variant.price_with_fees * variant.line_item.quantity variant.basePricePercentage = Math.round(variant.price / variant.price_with_fees * 100) variant diff --git a/app/assets/javascripts/templates/price_breakdown.html.haml b/app/assets/javascripts/templates/price_breakdown.html.haml index 95ec795988..cf82e31457 100644 --- a/app/assets/javascripts/templates/price_breakdown.html.haml +++ b/app/assets/javascripts/templates/price_breakdown.html.haml @@ -29,7 +29,7 @@ Fundraising fee %li %strong - .right = {{ variant.price | localizeCurrency }} + .right = {{ variant.price_with_fees | localizeCurrency }}   %a{"ng-click" => "expanded = !expanded"} diff --git a/app/assets/javascripts/templates/shop_variant.html.haml b/app/assets/javascripts/templates/shop_variant.html.haml index 607e770e31..b1538bb69e 100644 --- a/app/assets/javascripts/templates/shop_variant.html.haml +++ b/app/assets/javascripts/templates/shop_variant.html.haml @@ -48,7 +48,7 @@ .small-4.medium-2.large-2.columns.variant-price .table-cell.price %i.ofn-i_009-close - {{ variant.price | localizeCurrency }} + {{ variant.price_with_fees | localizeCurrency }} -# Now in a template in app/assets/javascripts/templates ! %price-breakdown{"price-breakdown" => "_", variant: "variant", @@ -59,4 +59,4 @@ .small-12.medium-2.large-2.columns.total-price.text-right .table-cell %strong - {{ variant.getPrice() | localizeCurrency }} + {{ variant.totalPrice() | localizeCurrency }} diff --git a/app/helpers/spree/products_helper_decorator.rb b/app/helpers/spree/products_helper_decorator.rb index f918a47649..4ec4a88588 100644 --- a/app/helpers/spree/products_helper_decorator.rb +++ b/app/helpers/spree/products_helper_decorator.rb @@ -2,7 +2,7 @@ module Spree ProductsHelper.class_eval do # Return the price of the variant def variant_price_diff(variant) - "(#{number_to_currency variant.price})" + "(#{Spree::Money.new(variant.price).to_s})" end diff --git a/app/serializers/spree/api/variant_serializer.rb b/app/serializers/spree/api/variant_serializer.rb index c136d02644..cd31196ca0 100644 --- a/app/serializers/spree/api/variant_serializer.rb +++ b/app/serializers/spree/api/variant_serializer.rb @@ -7,6 +7,7 @@ class Spree::Api::VariantSerializer < ActiveModel::Serializer end def price - object.price.nil? ? 0.to_f : object.price.to_f + # Decimals are passed to json as strings, we need to run parseFloat.toFixed(2) on the client side. + object.price.nil? ? 0.to_f : object.price end end diff --git a/app/views/shared/menu/_cart.html.haml b/app/views/shared/menu/_cart.html.haml index 1ecb9231af..345365735b 100644 --- a/app/views/shared/menu/_cart.html.haml +++ b/app/views/shared/menu/_cart.html.haml @@ -28,7 +28,7 @@ %small \= %strong - .right {{ line_item.variant.getPrice() | localizeCurrency }} + .right {{ line_item.variant.totalPrice() | localizeCurrency }} %li.total-cart{"ng-show" => "Cart.line_items_present().length > 0"} .row diff --git a/config/ng-test.conf.js b/config/ng-test.conf.js index eadaf984ae..f87aa3d48a 100644 --- a/config/ng-test.conf.js +++ b/config/ng-test.conf.js @@ -27,9 +27,16 @@ module.exports = function(config) { 'app/assets/javascripts/admin/util.js.erb' ], + preprocessors: { + '**/*.coffee': ['coffee'] + }, + coffeePreprocessor: { options: { sourceMap: true + }, + transformPath: function(path) { + return path.replace(/\.coffee$/, '.js'); } }, diff --git a/spec/javascripts/unit/darkswarm/filters/localize_currency_spec.js.coffee b/spec/javascripts/unit/darkswarm/filters/localize_currency_spec.js.coffee index 9acc4a1b63..0d21c7de6c 100644 --- a/spec/javascripts/unit/darkswarm/filters/localize_currency_spec.js.coffee +++ b/spec/javascripts/unit/darkswarm/filters/localize_currency_spec.js.coffee @@ -3,12 +3,13 @@ describe 'convert number to localised currency ', -> beforeEach -> currencyconfig = - currency: "D" symbol: "$" symbol_position: "before" + currency: "D" hide_cents: "false" - decimal_mark: "." - thousands_separator: "," + # Not used yet... + # decimal_mark: "." + # thousands_separator: "," module 'Darkswarm' module ($provide)-> $provide.value "currencyConfig", currencyconfig @@ -34,4 +35,8 @@ describe 'convert number to localised currency ', -> currencyconfig.display_currency = "true" expect(filter(5)).toEqual "$5.00 D" + it "can hide cents", -> + currencyconfig.hide_cents = "true" + expect(filter(5)).toEqual "$5" +