[FIX] web: Form: delete the correct record

When the user duplicated a record 'A', and deleted the new record
'A (copy)' directly, the wrong record was deleted: 'A' was deleted
instead of 'A (copy)', because de id of 'A' was bound to the delete
handler, and it wasn't updated after the duplication.

Also correctly updates the record's offset after the deletion, so
that it doesn't produce a warning when deleting the last record of
the dataset.
This commit is contained in:
Aaron Bohy
2017-06-14 10:15:37 +02:00
parent 70624772b2
commit a3bea9a62b
3 changed files with 60 additions and 1 deletions
@@ -208,6 +208,7 @@ var BasicModel = AbstractModel.extend({
delete self.localData[record.id];
} else {
record.res_ids.splice(record.offset, 1);
record.offset = Math.min(record.offset, record.res_ids.length - 1);
record.res_id = record.res_ids[record.offset];
record.count--;
}
@@ -151,7 +151,7 @@ var FormController = BasicController.extend({
if (this.is_action_enabled('delete')) {
otherItems.push({
label: _t('Delete'),
callback: this._deleteRecords.bind(this, [this.handle]),
callback: this._onDeleteRecord.bind(this),
});
}
if (this.is_action_enabled('create') && this.is_action_enabled('duplicate')) {
@@ -394,6 +394,14 @@ var FormController = BasicController.extend({
_onCreate: function () {
this.createRecord();
},
/**
* Deletes the current record
*
* @private
*/
_onDeleteRecord: function () {
this._deleteRecords([this.handle]);
},
/**
* Called when the user wants to discard the changes made to the current
* record -> @see discardChanges
@@ -5064,5 +5064,55 @@ QUnit.module('Views', {
form.destroy();
});
QUnit.test('delete a duplicated record', function (assert) {
assert.expect(5);
var newRecordID;
var form = createView({
View: FormView,
model: 'partner',
data: this.data,
arch: '<form string="Partners">' +
'<field name="display_name"/>' +
'</form>',
res_id: 1,
viewOptions: {sidebar: true},
mockRPC: function (route, args) {
if (args.method === 'search_read' && args.model === 'ir.attachment') {
// rpcs done by the sidebar
return $.when([]);
}
var result = this._super.apply(this, arguments);
if (args.method === 'copy') {
return result.then(function (id) {
newRecordID = id;
return id;
});
}
if (args.method === 'unlink') {
assert.deepEqual(args.args[0], [newRecordID],
"should delete the newly created record");
}
return result;
},
});
form.sidebar.$('a:contains(Duplicate)').click(); // duplicate record 1
assert.strictEqual(form.$('.o_form_editable').length, 1,
"form should be in edit mode");
assert.strictEqual(form.$('.o_field_widget').val(), 'first record (copy)',
"duplicated record should have correct name");
form.$buttons.find('.o_form_button_save').click(); // save duplicated record
form.sidebar.$('a:contains(Delete)').click(); // delete duplicated record
assert.strictEqual($('.modal').length, 1, "should have opened a confirm dialog");
$('.modal .modal-footer .btn-primary').click(); // confirm
assert.strictEqual(form.$('.o_field_widget').text(), 'first record',
"should have come back to previous record");
form.destroy();
});
});
});