[FIX] domain and context generation on m2o fields

by default, an m2o field should behave like a charfield (== use its
textual value), but if the domain is fully default (no filter_domain
and no custom operator) and the user explicitly selected an object
(which is the only thing he can do at this point in the new search
view), then we can generate a domain matching exactly the stored id.

bzr revid: xmo@openerp.com-20120503163709-odlnib513h3f84v6
This commit is contained in:
Xavier Morel 2012-05-03 18:37:09 +02:00
parent 5c4ac52291
commit 64538c181e
2 changed files with 57 additions and 0 deletions

View File

@ -1270,6 +1270,7 @@ instance.web.search.DateTimeField = instance.web.search.DateField.extend(/** @le
}
});
instance.web.search.ManyToOneField = instance.web.search.CharField.extend({
default_operator: {},
init: function (view_section, field, view) {
this._super(view_section, field, view);
this.model = new instance.web.Model(this.attrs.relation);
@ -1302,6 +1303,15 @@ instance.web.search.ManyToOneField = instance.web.search.CharField.extend({
if (_(names).isEmpty()) { return null; }
return facet_from(self, names[0]);
})
},
value_from: function (facetValue) {
return facetValue.get('label');
},
make_domain: function (name, operator, facetValue) {
if (operator === this.default_operator) {
return [[name, '=', facetValue.get('value')]];
}
return this._super(name, operator, facetValue);
}
});

View File

@ -833,6 +833,53 @@ $(document).ready(function () {
self: "great"
}, "evaluation context should hold facet value as self");
});
test("M2O default", function () {
var f = new instance.web.search.ManyToOneField(
{}, {name: 'foo'}, {inputs: []});
var facet = new instance.web.search.Facet({
field: f,
values: [{label: "Foo", value: 42}]
});
deepEqual(f.get_domain(facet), [['foo', '=', 42]],
"m2o should use identity if default domain");
});
test("M2O custom operator", function () {
var f = new instance.web.search.ManyToOneField(
{attrs: {operator: 'boos'}}, {name: 'foo'}, {inputs: []});
var facet = new instance.web.search.Facet({
field: f,
values: [{label: "Foo", value: 42}]
});
deepEqual(f.get_domain(facet), [['foo', 'boos', 'Foo']],
"m2o should use label with custom operators");
});
test("M2O custom domain & context", function () {
var f = new instance.web.search.ManyToOneField({attrs: {
context: "{'whee': self}",
filter_domain: "[['filter', 'is', self]]"
}}, {name: 'foo'}, {inputs: []});
var facet = new instance.web.search.Facet({
field: f,
values: [{label: "Foo", value: 42}]
});
var domain = f.get_domain(facet);
deepEqual(domain.__domains, [
"[['filter', 'is', self]]"
]);
deepEqual(domain.get_eval_context(), {
self: "Foo"
}, "custom domain's self should be label");
var context = f.get_context(facet);
deepEqual(context.__contexts, [
"{'whee': self}"
]);
deepEqual(context.get_eval_context(), {
self: "Foo"
}, "custom context's self should be label");
});
module('drawer', {
setup: function () {