diff --git a/CHANGELOG.md b/CHANGELOG.md index 9557862e8a..2eea6d44ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ [Full Changelog](https://github.com/sferik/rails_admin/compare/v2.2.1...HEAD) +- Fix polymorphic associations don't work with namespaced classes([#3376](https://github.com/sferik/rails_admin/issues/3376)) ## [2.2.1](https://github.com/sferik/rails_admin/tree/v2.2.0) - 2021-08-08 diff --git a/app/assets/javascripts/rails_admin/ra.widgets.js b/app/assets/javascripts/rails_admin/ra.widgets.js index 2e3bcecdfa..534afeec54 100644 --- a/app/assets/javascripts/rails_admin/ra.widgets.js +++ b/app/assets/javascripts/rails_admin/ra.widgets.js @@ -202,7 +202,7 @@ urls = type_select.data('urls'); type_select.on('change', function(e) { var selected_data, selected_type; - selected_type = type_select.val().toLowerCase(); + selected_type = type_select.val().toLowerCase().replace(/::/g, '-'); selected_data = $("#" + selected_type + "-js-options").data('options'); object_select.data('options', selected_data); object_select.filteringSelect("destroy"); diff --git a/app/views/rails_admin/main/_form_polymorphic_association.html.haml b/app/views/rails_admin/main/_form_polymorphic_association.html.haml index f10fcaa0ec..8a712c46f4 100644 --- a/app/views/rails_admin/main/_form_polymorphic_association.html.haml +++ b/app/views/rails_admin/main/_form_polymorphic_association.html.haml @@ -10,11 +10,10 @@ default_options = { float_left: false } js_data = type_collection.inject({}) do |options, model| - model_name = model.second.underscore.downcase source_abstract_model = RailsAdmin.config(form.object.class).abstract_model - options.merge(model_name.gsub("_", "") => { + options.merge(model.second.downcase.gsub('::', '-') => { xhr: true, - remote_source: index_path(model_name, source_object_id: form.object.id, source_abstract_model: source_abstract_model.to_param, current_action: current_action, compact: true), + remote_source: index_path(model.second.underscore, source_object_id: form.object.id, source_abstract_model: source_abstract_model.to_param, current_action: current_action, compact: true), float_left: false }) end diff --git a/spec/dummy_app/app/active_record/two_level/namespaced.rb b/spec/dummy_app/app/active_record/two_level/namespaced.rb new file mode 100644 index 0000000000..8f70573569 --- /dev/null +++ b/spec/dummy_app/app/active_record/two_level/namespaced.rb @@ -0,0 +1,7 @@ +module TwoLevel + module Namespaced + def self.table_name_prefix + 'two_level_namespaced_' + end + end +end diff --git a/spec/dummy_app/app/active_record/two_level/namespaced/polymorphic_association_test.rb b/spec/dummy_app/app/active_record/two_level/namespaced/polymorphic_association_test.rb new file mode 100644 index 0000000000..cf647c323a --- /dev/null +++ b/spec/dummy_app/app/active_record/two_level/namespaced/polymorphic_association_test.rb @@ -0,0 +1,7 @@ +module TwoLevel + module Namespaced + class PolymorphicAssociationTest < ActiveRecord::Base + has_many :comments, as: :commentable + end + end +end diff --git a/spec/dummy_app/app/mongoid/two_level/namespaced/polymorphic_association_test.rb b/spec/dummy_app/app/mongoid/two_level/namespaced/polymorphic_association_test.rb new file mode 100644 index 0000000000..8ce74d5a44 --- /dev/null +++ b/spec/dummy_app/app/mongoid/two_level/namespaced/polymorphic_association_test.rb @@ -0,0 +1,11 @@ +module TwoLevel + module Namespaced + class PolymorphicAssociationTest + include Mongoid::Document + + field :name, type: String + + has_many :comments, as: :commentable + end + end +end diff --git a/spec/dummy_app/db/migrate/20210811121027_create_two_level_namespaced_polymorphic_association_tests.rb b/spec/dummy_app/db/migrate/20210811121027_create_two_level_namespaced_polymorphic_association_tests.rb new file mode 100644 index 0000000000..ea6494d648 --- /dev/null +++ b/spec/dummy_app/db/migrate/20210811121027_create_two_level_namespaced_polymorphic_association_tests.rb @@ -0,0 +1,9 @@ +class CreateTwoLevelNamespacedPolymorphicAssociationTests < ActiveRecord::Migration[5.0] + def change + create_table :two_level_namespaced_polymorphic_association_tests do |t| + t.string :name + + t.timestamps + end + end +end diff --git a/spec/factories.rb b/spec/factories.rb index 3407c04229..989b028890 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -97,4 +97,8 @@ parent: :paper_trail_test, class: 'PaperTrailTestWithCustomAssociation' end + + factory :two_level_namespaced_polymorphic_association_test, class: "TwoLevel::Namespaced::PolymorphicAssociationTest" do + sequence(:name) { |n| "name #{n}" } + end end diff --git a/spec/integration/fields/polymorphic_assosiation_spec.rb b/spec/integration/fields/polymorphic_assosiation_spec.rb index 5ffdc10e47..deaaf7a014 100644 --- a/spec/integration/fields/polymorphic_assosiation_spec.rb +++ b/spec/integration/fields/polymorphic_assosiation_spec.rb @@ -24,6 +24,27 @@ expect(@comment.commentable_type).to eq 'Ball' expect(@comment.commentable).to eq @hardball end + + context 'when the associated model is declared in a two-level namespace' do + it 'successfully saves the record', js: true do + polymorphic_association_tests = ['Jackie Robinson', 'Rob Wooten'].map do |name| + FactoryBot.create(:two_level_namespaced_polymorphic_association_test, name: name) + end + + visit new_path(model_name: 'comment') + + select 'Polymorphic association test', from: 'comment[commentable_type]' + find('input.ra-filtering-select-input').set('Rob') + + page.execute_script("$('input.ra-filtering-select-input').trigger('focus')") + page.execute_script("$('input.ra-filtering-select-input').trigger('keydown')") + expect(page).to have_selector('ul.ui-autocomplete li.ui-menu-item a') + + page.execute_script %{$('ul.ui-autocomplete li.ui-menu-item a:contains("Jackie Robinson")').trigger('mouseenter').click()} + click_button 'Save' + expect(Comment.first.commentable).to eq polymorphic_association_tests.first + end + end end context 'on update' do