[FIX] web: support integer/float fields in search view via auto-completion + tests

integer/float fields were not offering auto-completion in search views,
making them unsearchable except via advanced search.
This patch adds the missing complete() function and removes the incorrect
value_from() function that did not conform to the 7.0 search view API.
It seemed to be a leftover of the 6.1 search field implementation
of get_value(), wrongly renamed for 7.0.

Also includes corresponding tests.

bzr revid: odo@openerp.com-20130418112001-388op1t8ugr0rhfn
This commit is contained in:
Olivier Dony 2013-04-18 13:20:01 +02:00
commit dc24794855
2 changed files with 69 additions and 14 deletions

View File

@ -1346,20 +1346,22 @@ instance.web.search.CharField = instance.web.search.Field.extend( /** @lends ins
}
});
instance.web.search.NumberField = instance.web.search.Field.extend(/** @lends instance.web.search.NumberField# */{
value_from: function () {
if (!this.$el.val()) {
return null;
}
var val = this.parse(this.$el.val()),
check = Number(this.$el.val());
if (isNaN(val) || val !== check) {
this.$el.addClass('error');
throw new instance.web.search.Invalid(
this.attrs.name, this.$el.val(), this.error_message);
}
this.$el.removeClass('error');
return val;
}
complete: function (value) {
var val = this.parse(value);
if (isNaN(val)) { return $.when(); }
var label = _.str.sprintf(
_t("Search %(field)s for: %(value)s"), {
field: '<em>' + this.attrs.string + '</em>',
value: '<strong>' + _.str.escapeHTML(value) + '</strong>'});
return $.when([{
label: label,
facet: {
category: this.attrs.string,
field: this,
values: [{label: value, value: val}]
}
}]);
},
});
/**
* @class

View File

@ -614,6 +614,59 @@ openerp.testing.section('search.completions', {
{relation: 'dummy.model'}, view);
return f.complete("bob");
});
test('Integer: invalid', {asserts: 1}, function (instance) {
var view = {inputs: []};
var f = new instance.web.search.IntegerField(
{attrs: {string: "Dummy"}}, {}, view);
return f.complete("qux")
.done(function (completions) {
ok(!completions, "non-number => no completion");
});
});
test('Integer: non-zero', {asserts: 5}, function (instance) {
var view = {inputs: []};
var f = new instance.web.search.IntegerField(
{attrs: {string: "Dummy"}}, {}, view);
return f.complete("-2")
.done(function (completions) {
equal(completions.length, 1, "number fields provide 1 completion only");
var facet = new instance.web.search.Facet(completions[0].facet);
equal(facet.get('category'), f.attrs.string);
equal(facet.get('field'), f);
var value = facet.values.at(0);
equal(value.get('label'), "-2");
equal(value.get('value'), -2);
});
});
test('Integer: zero', {asserts: 3}, function (instance) {
var view = {inputs: []};
var f = new instance.web.search.IntegerField(
{attrs: {string: "Dummy"}}, {}, view);
return f.complete("0")
.done(function (completions) {
equal(completions.length, 1, "number fields provide 1 completion only");
var facet = new instance.web.search.Facet(completions[0].facet);
var value = facet.values.at(0);
equal(value.get('label'), "0");
equal(value.get('value'), 0);
});
});
test('Float: non-zero', {asserts: 5}, function (instance) {
var view = {inputs: []};
var f = new instance.web.search.FloatField(
{attrs: {string: "Dummy"}}, {}, view);
return f.complete("42.37")
.done(function (completions) {
equal(completions.length, 1, "float fields provide 1 completion only");
var facet = new instance.web.search.Facet(completions[0].facet);
equal(facet.get('category'), f.attrs.string);
equal(facet.get('field'), f);
var value = facet.values.at(0);
equal(value.get('label'), "42.37");
equal(value.get('value'), 42.37);
});
});
});
openerp.testing.section('search.serialization', {
dependencies: ['web.search'],