Skip to content

Commit

Permalink
Implement Association#foreign_key_nullable?
Browse files Browse the repository at this point in the history
  • Loading branch information
jibidus committed Oct 28, 2015
1 parent 317ad17 commit 31ee21d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/rails_admin/adapters/active_record/association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions lib/rails_admin/adapters/mongoid/association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
15 changes: 15 additions & 0 deletions spec/rails_admin/adapters/active_record/association_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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 } }

Expand All @@ -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
Expand Down
7 changes: 7 additions & 0 deletions spec/rails_admin/adapters/mongoid/association_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 31ee21d

Please sign in to comment.