Skip to content

Commit

Permalink
Fix polymorphic id didn't reset properly in an edge case
Browse files Browse the repository at this point in the history
Refs. #3630
  • Loading branch information
mshibuya committed Sep 1, 2024
1 parent c0eac2a commit 3c3a734
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
24 changes: 24 additions & 0 deletions spec/integration/fields/polymorphic_assosiation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,30 @@

is_expected.to have_selector('select#team_comment_ids')
end

context 'with records in different models share the same id', js: true do
let!(:players) { [FactoryBot.create(:player, id: team.id, name: 'Jackie Robinson')] }

it 'clears the selected id on type change', js: true do
visit edit_path(model_name: 'comment', id: comment.id)
select 'Player', from: 'comment[commentable_type]'
click_button 'Save'
is_expected.to have_content 'Comment successfully updated'
expect(comment.reload.commentable).to eq nil
end

it 'updates correctly', js: true do
visit edit_path(model_name: 'comment', id: comment.id)
select 'Player', from: 'comment[commentable_type]'
find('input.ra-filtering-select-input').set('Rob')
page.execute_script("document.querySelector('input.ra-filtering-select-input').dispatchEvent(new KeyboardEvent('keydown'))")
expect(page).to have_selector('ul.ui-autocomplete li.ui-menu-item a')
page.execute_script %{[...document.querySelectorAll('ul.ui-autocomplete li.ui-menu-item')].find(e => e.innerText.includes("Jackie Robinson")).click()}
click_button 'Save'
is_expected.to have_content 'Comment successfully updated'
expect(comment.reload.commentable).to eq players[0]
end
end
end

context 'on show' do
Expand Down
1 change: 1 addition & 0 deletions src/rails_admin/filtering-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ import I18n from "./i18n";
destroy: function () {
this.input.remove();
this.button.remove();
this.element.html($('<option value="" selected="selected"></option>'));
this.element.show();
this.filtering_select.remove();
$.Widget.prototype.destroy.call(this);
Expand Down
1 change: 0 additions & 1 deletion src/rails_admin/widgets.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,6 @@ import I18n from "./i18n";
"options"
);
object_select.data("options", selected_data);
object_select.val("");
object_select.filteringSelect("destroy");
object_select.filteringSelect(selected_data);
});
Expand Down

0 comments on commit 3c3a734

Please sign in to comment.