Skip to content

Commit

Permalink
Fix #3376: Polymorphic associations don't work with namespaced classes
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob-carlborg-apoex committed Aug 12, 2021
1 parent 238f18e commit 2039f0a
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion app/assets/javascripts/rails_admin/ra.widgets.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions spec/dummy_app/app/active_record/two_level/namespaced.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module TwoLevel
module Namespaced
def self.table_name_prefix
'two_level_namespaced_'
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module TwoLevel
module Namespaced
class PolymorphicAssociationTest < ActiveRecord::Base
has_many :comments, as: :commentable
end
end
end
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions spec/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 21 additions & 0 deletions spec/integration/fields/polymorphic_assosiation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2039f0a

Please sign in to comment.