From 092997bffe90dd0ce5bf2d1436715b3258236f12 Mon Sep 17 00:00:00 2001 From: Scott Erickson Date: Fri, 17 Oct 2014 10:11:26 -0400 Subject: [PATCH] Added i18n coverage logic to the CocoModel. --- app/models/CocoModel.coffee | 26 +++++++++++++++++++++++++- test/app/models/CocoModel.spec.coffee | 24 ++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/app/models/CocoModel.coffee b/app/models/CocoModel.coffee index 82982f08b..ebe42cc49 100644 --- a/app/models/CocoModel.coffee +++ b/app/models/CocoModel.coffee @@ -332,6 +332,30 @@ class CocoModel extends Backbone.Model error: -> console.error 'Miserably failed to fetch unnotified achievements', arguments -CocoModel.pollAchievements = _.debounce CocoModel.pollAchievements, 500 + CocoModel.pollAchievements = _.debounce CocoModel.pollAchievements, 500 + + + #- Internationalization + + updateI18NCoverage: -> + i18nObjects = @findI18NObjects() + console.log 'i18n objects', i18nObjects + langCodeArrays = (_.keys(i18n) for i18n in i18nObjects) + console.log 'lang code arrays', langCodeArrays + window.codes = langCodeArrays + @set('i18nCoverage', _.intersection(langCodeArrays...)) + + findI18NObjects: (data, results) -> + data ?= @attributes + results ?= [] + + if _.isPlainObject(data) or _.isArray(data) + for [key, value] in _.pairs data + if key is 'i18n' + results.push value + else if _.isPlainObject(value) or _.isArray(value) + @findI18NObjects(value, results) + + return results module.exports = CocoModel diff --git a/test/app/models/CocoModel.spec.coffee b/test/app/models/CocoModel.spec.coffee index 9456707b3..0cce0b1cd 100644 --- a/test/app/models/CocoModel.spec.coffee +++ b/test/app/models/CocoModel.spec.coffee @@ -150,3 +150,27 @@ describe 'CocoModel', -> else return ready false request.response {status:200, responseText: JSON.stringify me} + + describe 'updateI18NCoverage', -> + class FlexibleClass extends CocoModel + @className: 'Flexible' + @schema: {} + + it 'only includes languages for which all objects include a translation', -> + m = new FlexibleClass({ + i18n: { es: {}, fr: {} } + prop1: 1 + prop2: 'string' + prop3: true + innerObject: { + i18n: { es: {}, de: {}, fr: {} } + prop4: [ + { + i18n: { es: {} } + } + ] + } + }) + + m.updateI18NCoverage() + expect(JSON.stringify(m.get('i18nCoverage'))).toBe('["es"]') \ No newline at end of file