From 46c8157150fa2d3d7cdd39c13f87c2ea8b5dc833 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Thu, 13 Feb 2014 22:50:28 +0100 Subject: [PATCH] Fix domain inversion for xxx2many fields issue3659 review3331002 --- src/common.js | 33 ++++++++++++++++++++++++++------- src/model.js | 41 +++++++++++++++++++++++++++++++++++++++-- tests/sao.js | 9 ++++++++- 3 files changed, 73 insertions(+), 10 deletions(-) diff --git a/src/common.js b/src/common.js index eaf705fa18..d23157f0e8 100644 --- a/src/common.js +++ b/src/common.js @@ -1268,9 +1268,12 @@ 'child_of': function() {return true;}, 'not child_of': function() {return true;} }, - locale_part: function(expression, field_name) { + locale_part: function(expression, field_name, locale_name) { + if (locale_name === undefined) { + locale_name = 'id'; + } if (expression === field_name) { - return 'id'; + return locale_name; } if (expression.contains('.')) { return expression.split('.').slice(1).join('.'); @@ -1313,6 +1316,14 @@ (typeof value == 'string') && context_field.length == 2) { context_field = context_field.join(','); } + if (~['=', '!='].indexOf(operand) && + context_field instanceof Array && + typeof value == 'number') { + operand = { + '=': 'in', + '!=': 'not in' + }[operand]; + } return this.OPERATORS[operand](context_field, value); }, inverse_leaf: function(domain) { @@ -1361,7 +1372,11 @@ return [domain[3]].concat(domain.slice(1, -1)); } } - return [this.locale_part(domain[0], field_name)] + var local_name = 'id'; + if (typeof domain[2] == 'string') { + local_name = 'rec_name'; + } + return [this.locale_part(domain[0], field_name, local_name)] .concat(domain.slice(1)); } else { return domain.map(function(e) { @@ -1447,12 +1462,16 @@ }); Sao.common.DomainInversion.in_ = function(a, b) { if (a instanceof Array) { - for (var i = 0, len = a.length; i < len; i++) { - if (~b.indexOf(a[i])) { - return true; + if (b instanceof Array) { + for (var i = 0, len = a.length; i < len; i++) { + if (~b.indexOf(a[i])) { + return true; + } } + return false; + } else { + return Boolean(~a.indexOf(b)); } - return false; } else { return Boolean(~b.indexOf(a)); } diff --git a/src/model.js b/src/model.js index b955544338..6a67e30c8b 100644 --- a/src/model.js +++ b/src/model.js @@ -1100,7 +1100,7 @@ var localpart = leftpart.split('.', 1)[1]; var constraintfields = []; if (domain_readonly) { - inverted_domain.localize_domain( + inversion.localize_domain( original_domain.slice(1)) .forEach(function(leaf) { constraintfields.push(leaf); @@ -1551,6 +1551,43 @@ return result; }, set_client: function(record, value, force_change) { + // domain inversion could try to set id as value + if (typeof value == 'number') { + value = [value]; + } + + var previous_group = record._values[this.name]; + var previous_ids = []; + if (!jQuery.isEmptyObject(previous_group)) { + previous_group.forEach(function(r) { + previous_ids.push(r.id); + }); + } + this._set_value(record, value); + if (!Sao.common.compare(previous_ids, value)) { + record._changed[this.name] = true; + this.changed(record).done(function() { + // TODO parent + record.validate(null, true).then(function() { + record.group.changed().done(function() { + var root_group = record.group.root_group(); + root_group.screens.forEach(function(screen) { + screen.display(); + }); + }); + }); + }); + } else if (force_change) { + record._changed[this.name] = true; + this.changed(record).done(function() { + record.validate(null, true).then(function() { + var root_group = record.group.root_group(); + root_group.screens.forEach(function(screen) { + screen.display(); + }); + }); + }); + } }, get_client: function(record) { this._set_default_value(record); @@ -1709,7 +1746,7 @@ var attr_domain = domains[1]; var inversion = new Sao.common.DomainInversion(); return [inversion.localize_domain( - inversion.inverse_leaf(screen_domain)), + inversion.inverse_leaf(screen_domain), this.name), attr_domain]; }, validation_domains: function(record) { diff --git a/tests/sao.js b/tests/sao.js index e153430ead..ebef091b70 100644 --- a/tests/sao.js +++ b/tests/sao.js @@ -1754,7 +1754,9 @@ [[['x', '=', ['test', 1]]], {'x': ['test', 1]}, true], [[['x', '=', ['test', 1]]], {'x': 'test,1'}, true], [[['x', '=', ['test', 1]]], {'x': ['test', 2]}, false], - [[['x', '=', ['test', 1]]], {'x': 'test,2'}, false] + [[['x', '=', ['test', 1]]], {'x': 'test,2'}, false], + [[['x', '=', 1]], {'x': [1, 2]}, true], + [[['x', '=', 1]], {'x': [2]}, false] ].forEach(function(test) { var domain = test[0]; var context = test[1]; @@ -1780,6 +1782,11 @@ [['id', '=', 5], ['code', '=', 7]]), 'localize_domain(' + JSON.stringify(domain) + ', \'x\')'); + domain = [['x', 'ilike', 'foo%'], ['x.code', '=', 'test']]; + QUnit.ok(compare(localize_domain(domain, 'x'), + [['rec_name', 'ilike', 'foo%'], ['code', '=', 'test']]), + 'localize_domain(' + JSON.stringify(domain) + ', \'x\')'); + domain = ['OR', ['AND', ['x', '>', 7], ['x', '<', 15]], ['x.code', '=', 8]]; QUnit.ok(compare(localize_domain(domain, 'x'),