Fix ID -> Id ordering bug

This commit is contained in:
Juan Cruz Viotti 2014-11-19 14:57:30 -04:00
parent 34346dd3d0
commit be01094463
2 changed files with 21 additions and 1 deletions

View File

@ -19,6 +19,16 @@ renameObjectKey = (object, key, newKey) ->
exports.getKeyName = (key) ->
nameFromMap = KEY_DISPLAY_MAP[key]
return nameFromMap if nameFromMap?
# Prevent modifying a value that is part of
# the map values.
# This is really an heuristic, as making sure
# the client actually refers to that value means
# converting the value to lowercase-underscore-cased
# and do the check, but that seems overkill.
if _.values(KEY_DISPLAY_MAP).indexOf(key) isnt -1
return key
key = key.replace('_', ' ')
return _.str.titleize(key)
@ -63,5 +73,5 @@ exports.getDefaultContentsOrdering = (contents) ->
exports.normaliseOrdering = (ordering, contents) ->
if not _.isEmpty(ordering)
return _.map(ordering, _.str.titleize)
return _.map(ordering, exports.getKeyName)
return exports.getDefaultContentsOrdering(contents)

View File

@ -146,3 +146,13 @@ describe 'Table Helpers:', ->
result = tableHelpers.normaliseOrdering(null, [ OBJECTS.valid ])
for key, value of OBJECTS.valid
expect(result.indexOf(key)).to.not.equal(-1)
it 'should not give precendence to names from the map', ->
ordering = [ 'id', 'ip_address', 'name' ]
result = tableHelpers.normaliseOrdering(ordering, {})
expect(result).to.deep.equal([ 'ID', 'IP Address', 'Name' ])
it 'should preverse a string that is the result of a map lookup', ->
ordering = [ 'ID', 'IP Address' ]
result = tableHelpers.normaliseOrdering(ordering, {})
expect(result).to.deep.equal(ordering)