Skip to content

Commit 7b2ffb1

Browse files
committed
Fix polymorphic id doesn't reset properly in an edge case
Refs. #3630
1 parent c0eac2a commit 7b2ffb1

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

spec/integration/fields/polymorphic_assosiation_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,30 @@
9090

9191
is_expected.to have_selector('select#team_comment_ids')
9292
end
93+
94+
context 'with records in different models share the same id', js: true do
95+
let!(:players) { [FactoryBot.create(:player, id: team.id, name: 'Jackie Robinson')] }
96+
97+
it 'clears the selected id on type change', js: true do
98+
visit edit_path(model_name: 'comment', id: comment.id)
99+
select 'Player', from: 'comment[commentable_type]'
100+
click_button 'Save'
101+
is_expected.to have_content 'Comment successfully updated'
102+
expect(comment.reload.commentable).to eq nil
103+
end
104+
105+
it 'updates correctly', js: true do
106+
visit edit_path(model_name: 'comment', id: comment.id)
107+
select 'Player', from: 'comment[commentable_type]'
108+
find('input.ra-filtering-select-input').set('Rob')
109+
page.execute_script("document.querySelector('input.ra-filtering-select-input').dispatchEvent(new KeyboardEvent('keydown'))")
110+
expect(page).to have_selector('ul.ui-autocomplete li.ui-menu-item a')
111+
page.execute_script %{[...document.querySelectorAll('ul.ui-autocomplete li.ui-menu-item')].find(e => e.innerText.includes("Jackie Robinson")).click()}
112+
click_button 'Save'
113+
is_expected.to have_content 'Comment successfully updated'
114+
expect(comment.reload.commentable).to eq players[0]
115+
end
116+
end
93117
end
94118

95119
context 'on show' do

src/rails_admin/filtering-select.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ import I18n from "./i18n";
297297
destroy: function () {
298298
this.input.remove();
299299
this.button.remove();
300+
this.element.html($('<option value="" selected="selected"></option>'));
300301
this.element.show();
301302
this.filtering_select.remove();
302303
$.Widget.prototype.destroy.call(this);

src/rails_admin/widgets.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,6 @@ import I18n from "./i18n";
258258
"options"
259259
);
260260
object_select.data("options", selected_data);
261-
object_select.val("");
262261
object_select.filteringSelect("destroy");
263262
object_select.filteringSelect(selected_data);
264263
});

0 commit comments

Comments
 (0)