From 31ee21d8e66c95cfc769290e31b1029ee1ecbe14 Mon Sep 17 00:00:00 2001 From: Jibidus Date: Tue, 27 Oct 2015 22:34:50 +0100 Subject: [PATCH] Implement Association#foreign_key_nullable? --- .../adapters/active_record/association.rb | 5 +++++ lib/rails_admin/adapters/mongoid/association.rb | 5 +++++ .../adapters/active_record/association_spec.rb | 15 +++++++++++++++ .../adapters/mongoid/association_spec.rb | 7 +++++++ 4 files changed, 32 insertions(+) diff --git a/lib/rails_admin/adapters/active_record/association.rb b/lib/rails_admin/adapters/active_record/association.rb index db7a281c3c..80723cb528 100644 --- a/lib/rails_admin/adapters/active_record/association.rb +++ b/lib/rails_admin/adapters/active_record/association.rb @@ -37,6 +37,11 @@ def foreign_key association.foreign_key.to_sym end + def foreign_key_nullable? + return if foreign_key.nil? or type != :has_many + klass.columns_hash[association.foreign_key].null + end + def foreign_type options[:foreign_type].try(:to_sym) || :"#{name}_type" if options[:polymorphic] end diff --git a/lib/rails_admin/adapters/mongoid/association.rb b/lib/rails_admin/adapters/mongoid/association.rb index c62951a673..ea288e6371 100644 --- a/lib/rails_admin/adapters/mongoid/association.rb +++ b/lib/rails_admin/adapters/mongoid/association.rb @@ -48,6 +48,11 @@ def foreign_key association.foreign_key.to_sym rescue nil end + def foreign_key_nullable? + return if foreign_key.nil? + true + end + def foreign_type return unless polymorphic? && [:referenced_in, :belongs_to].include?(macro) association.inverse_type.try(:to_sym) || :"#{name}_type" diff --git a/spec/rails_admin/adapters/active_record/association_spec.rb b/spec/rails_admin/adapters/active_record/association_spec.rb index 0d91ad2e3d..d73b68b94f 100644 --- a/spec/rails_admin/adapters/active_record/association_spec.rb +++ b/spec/rails_admin/adapters/active_record/association_spec.rb @@ -103,6 +103,7 @@ class ARComment < ActiveRecord::Base expect(association.type).to eq :has_many expect(association.klass).to eq Division expect(association.read_only?).to be_falsey + expect(association.foreign_key_nullable?).to be_truthy end end @@ -113,6 +114,7 @@ class ARComment < ActiveRecord::Base expect(association.type).to eq :has_many expect(association.klass).to eq Team expect(association.read_only?).to be_truthy + expect(association.foreign_key_nullable?).to be_truthy end end @@ -127,6 +129,18 @@ class ARComment < ActiveRecord::Base end end + describe 'has_many association with not nullable foreign key' do + let(:field_test) { RailsAdmin::AbstractModel.new(FieldTest) } + + context 'for direct has many' do + let(:association) { field_test.associations.detect { |a| a.name == :nested_field_tests } } + + it 'returns correct values' do + expect(association.foreign_key_nullable?).to be_falsey + end + end + end + describe 'has_and_belongs_to_many association' do subject { @post.associations.detect { |a| a.name == :a_r_categories } } @@ -135,6 +149,7 @@ class ARComment < ActiveRecord::Base expect(subject.klass).to eq ARCategory expect(subject.primary_key).to eq :id expect(subject.foreign_type).to be_nil + expect(subject.foreign_key_nullable?).to be_nil expect(subject.as).to be_nil expect(subject.polymorphic?).to be_falsey expect(subject.inverse_of).to be_nil diff --git a/spec/rails_admin/adapters/mongoid/association_spec.rb b/spec/rails_admin/adapters/mongoid/association_spec.rb index cd489bc7be..ea38b3b017 100644 --- a/spec/rails_admin/adapters/mongoid/association_spec.rb +++ b/spec/rails_admin/adapters/mongoid/association_spec.rb @@ -87,6 +87,7 @@ class MongoNote expect(subject.klass).to eq MongoBlog expect(subject.primary_key).to eq :_id expect(subject.foreign_key).to eq :mongo_blog_id + expect(subject.foreign_key_nullable?).to be_truthy expect(subject.foreign_type).to be_nil expect(subject.foreign_inverse_of).to be_nil expect(subject.as).to be_nil @@ -111,6 +112,7 @@ class MongoNote expect(subject.klass).to eq MongoPost expect(subject.primary_key).to eq :_id expect(subject.foreign_key).to eq :mongo_blog_id + expect(subject.foreign_key_nullable?).to be_truthy expect(subject.foreign_type).to be_nil expect(subject.foreign_inverse_of).to be_nil expect(subject.as).to be_nil @@ -130,6 +132,7 @@ class MongoNote expect(subject.klass).to eq MongoCategory expect(subject.primary_key).to eq :_id expect(subject.foreign_key).to eq :mongo_category_ids + expect(subject.foreign_key_nullable?).to be_truthy expect(subject.foreign_type).to be_nil expect(subject.foreign_inverse_of).to be_nil expect(subject.as).to be_nil @@ -150,6 +153,7 @@ class MongoNote expect(subject.klass).to eq [MongoBlog, MongoPost] expect(subject.primary_key).to eq :_id expect(subject.foreign_key).to eq :commentable_id + expect(subject.foreign_key_nullable?).to be_truthy expect(subject.foreign_type).to eq :commentable_type expect(subject.foreign_inverse_of).to be_nil expect(subject.as).to be_nil @@ -170,6 +174,7 @@ class MongoNote expect(subject.klass).to eq MongoComment expect(subject.primary_key).to eq :_id expect(subject.foreign_key).to eq :commentable_id + expect(subject.foreign_key_nullable?).to be_truthy expect(subject.foreign_type).to be_nil expect(subject.foreign_inverse_of).to be_nil expect(subject.as).to eq :commentable @@ -194,6 +199,7 @@ class MongoNote expect(subject.klass).to eq MongoNote expect(subject.primary_key).to eq :_id expect(subject.foreign_key).to be_nil + expect(subject.foreign_key_nullable?).to be_nil expect(subject.foreign_type).to be_nil expect(subject.foreign_inverse_of).to be_nil expect(subject.as).to be_nil @@ -213,6 +219,7 @@ class MongoNote expect(subject.klass).to eq MongoNote expect(subject.primary_key).to eq :_id expect(subject.foreign_key).to be_nil + expect(subject.foreign_key_nullable?).to be_nil expect(subject.foreign_type).to be_nil expect(subject.as).to be_nil expect(subject.polymorphic?).to be_falsey