From a3bea9a62bdaf205c0a9cd75945f6f6e9b023d6d Mon Sep 17 00:00:00 2001 From: Aaron Bohy Date: Tue, 13 Jun 2017 15:18:27 +0200 Subject: [PATCH] [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. --- .../static/src/js/views/basic/basic_model.js | 1 + .../src/js/views/form/form_controller.js | 10 +++- addons/web/static/tests/views/form_tests.js | 50 +++++++++++++++++++ 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/addons/web/static/src/js/views/basic/basic_model.js b/addons/web/static/src/js/views/basic/basic_model.js index 6c23550ba2e..ae48a98dd13 100644 --- a/addons/web/static/src/js/views/basic/basic_model.js +++ b/addons/web/static/src/js/views/basic/basic_model.js @@ -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--; } diff --git a/addons/web/static/src/js/views/form/form_controller.js b/addons/web/static/src/js/views/form/form_controller.js index d22a4095d1a..8a42168a098 100644 --- a/addons/web/static/src/js/views/form/form_controller.js +++ b/addons/web/static/src/js/views/form/form_controller.js @@ -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 diff --git a/addons/web/static/tests/views/form_tests.js b/addons/web/static/tests/views/form_tests.js index 75663e59b15..e578f3862c8 100644 --- a/addons/web/static/tests/views/form_tests.js +++ b/addons/web/static/tests/views/form_tests.js @@ -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: '
' + + '' + + '', + 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(); + }); }); });