refactor and bugfix for tests

This commit is contained in:
Rafael Schouten
2014-09-17 01:07:34 +10:00
parent 49476b17e8
commit 01c98bf6e4
10 changed files with 37 additions and 24 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -29,7 +29,7 @@
Fundraising fee
%li
%strong
.right = {{ variant.price | localizeCurrency }}
.right = {{ variant.price_with_fees | localizeCurrency }}
 
%a{"ng-click" => "expanded = !expanded"}

View File

@@ -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 }}

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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');
}
},

View File

@@ -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"