From bb1c07570de7a1888dc3766d2c86dfb92ff72d89 Mon Sep 17 00:00:00 2001 From: Ruben Vereecken Date: Sat, 28 Jun 2014 17:04:13 +0200 Subject: [PATCH] added isTranslationPatch method to patches --- app/lib/deltas.coffee | 6 ++++-- app/views/editor/delta.coffee | 3 ++- server/patches/Patch.coffee | 10 ++++------ test/server/unit/patch.spec.coffee | 30 +++++++++++++++++++++++++++--- 4 files changed, 37 insertions(+), 12 deletions(-) diff --git a/app/lib/deltas.coffee b/app/lib/deltas.coffee index 7c3fa3884..d83191432 100644 --- a/app/lib/deltas.coffee +++ b/app/lib/deltas.coffee @@ -11,7 +11,8 @@ module.exports.expandDelta = (delta, left, schema) -> flattenedDeltas = flattenDelta(delta) (expandFlattenedDelta(fd, left, schema) for fd in flattenedDeltas) -flattenDelta = (delta, dataPath=null, deltaPath=null) -> + +module.exports.flattenDelta = flattenDelta = (delta, dataPath=null, deltaPath=null) -> # takes a single jsondiffpatch delta and returns an array of objects with return [] unless delta dataPath ?= [] @@ -26,7 +27,8 @@ flattenDelta = (delta, dataPath=null, deltaPath=null) -> results = results.concat flattenDelta( childDelta, dataPath.concat([dataIndex]), deltaPath.concat([deltaIndex])) results - pandFlattenedDelta = (delta, left, schema) -> + +expandFlattenedDelta = (delta, left, schema) -> # takes a single flattened delta and converts into an object that can be # easily formatted into something human readable. diff --git a/app/views/editor/delta.coffee b/app/views/editor/delta.coffee index 91a8f6dba..36e663a68 100644 --- a/app/views/editor/delta.coffee +++ b/app/views/editor/delta.coffee @@ -1,6 +1,7 @@ CocoView = require 'views/kinds/CocoView' template = require 'templates/editor/delta' deltasLib = require 'lib/deltas' +window.delta = deltasLib TEXTDIFF_OPTIONS = baseTextName: "Old" @@ -112,4 +113,4 @@ module.exports = class DeltaView extends CocoView delta = @model.getDelta() delta = deltasLib.pruneConflictsFromDelta delta, @conflicts if @conflicts delta = deltasLib.pruneExpandedDeltasFromDelta delta, @skippedDeltas if @skippedDeltas - delta \ No newline at end of file + delta diff --git a/server/patches/Patch.coffee b/server/patches/Patch.coffee index 252d68743..c813e1e69 100644 --- a/server/patches/Patch.coffee +++ b/server/patches/Patch.coffee @@ -47,14 +47,12 @@ PatchSchema.pre 'save', (next) -> document.save (err) -> next(err) PatchSchema.methods.isTranslationPatch = -> - console.log @get 'delta' - expanded = deltas.expandDelta @get('delta') - console.log 'expanded' - _.some expanded, (delta) -> 'i18n' in expanded.dataPath + expanded = deltas.flattenDelta @get('delta') + _.some expanded, (delta) -> 'i18n' in delta.dataPath PatchSchema.methods.isMiscPatch = -> - expanded = deltas.expandDelta @get 'delta' - _.some expanded, (delta) -> 'i18n' not in expanded.dataPath + expanded = deltas.flattenDelta @get('delta') + _.some expanded, (delta) -> 'i18n' not in delta.dataPath # Keep track of when a patch is pending. Accepted patches can be rejected still. PatchSchema.path('status').set (newVal) -> diff --git a/test/server/unit/patch.spec.coffee b/test/server/unit/patch.spec.coffee index 04a6cae01..e7ce13ddd 100644 --- a/test/server/unit/patch.spec.coffee +++ b/test/server/unit/patch.spec.coffee @@ -2,13 +2,37 @@ require '../common' describe 'schema methods', -> patch = new Patch + commitMessage: 'Accept this patch!' + editPath: '/who/knows/yes' + target: + id:null + collection: 'article' delta: - scripts: 0: i18n: 'aaahw yeahh' - _t: 'a' + "scripts": + "0": + "noteChain": + "1": + "sprites": + "0": + "say": + "i18n": + "nl-BE": [ "text": "aaahw yeahh" ] + "_t": "a" + "_t": "a" + "_t": "a" + "thangs": + "111": [ + "components": [ + "config": { + "stateless": true + } + ] + ] + "_t": "a" it 'is translation patch', -> expect(patch.isTranslationPatch()).toBeTruthy() - patch.set 'delta.i18n', undefined + patch.set 'delta.scripts', undefined expect(patch.isTranslationPatch()).toBeFalsy() it 'is miscellaneous patch', ->