Skip to content

Commit

Permalink
#foreign_key_nullable? breaks with has_many through
Browse files Browse the repository at this point in the history
  • Loading branch information
mshibuya authored and jibidus committed Nov 26, 2015
1 parent 2e302f4 commit bfa85ef
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/rails_admin/adapters/active_record/association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ def foreign_key
end

def foreign_key_nullable?
return if foreign_key.nil? || type != :has_many
klass.columns_hash[foreign_key.to_s].null
return true if foreign_key.nil? || type != :has_many
(column = klass.columns_hash[foreign_key.to_s]).nil? || column.null
end

def foreign_type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ class FieldTestWithSymbolForeignKey < FieldTest
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_falsey
expect(subject.foreign_key_nullable?).to be_truthy
expect(subject.as).to be_nil
expect(subject.polymorphic?).to be_falsey
expect(subject.inverse_of).to be_nil
Expand Down
36 changes: 36 additions & 0 deletions spec/rails_admin/config/fields/association_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,40 @@
end
end
end

describe '#removable?', active_record: true do
context 'with non-nullable foreign key' do
let(:field) { RailsAdmin.config('FieldTest').fields.detect { |f| f.name == :nested_field_tests } }
it 'is false' do
expect(field.removable?).to be false
end
end

context 'with nullable foreign key' do
let(:field) { RailsAdmin.config('Team').fields.detect { |f| f.name == :players } }
it 'is true' do
expect(field.removable?).to be true
end
end

context 'with polymorphic has_many' do
let(:field) { RailsAdmin.config('Player').fields.detect { |f| f.name == :comments } }
it 'does not break' do
expect(field.removable?).to be true
end
end

context 'with has_many through' do
before do
class TeamWithHasManyThrough < Team
has_many :drafts
has_many :draft_players, through: :drafts, source: :player
end
end
let(:field) { RailsAdmin.config('TeamWithHasManyThrough').fields.detect { |f| f.name == :draft_players } }
it 'does not break' do
expect(field.removable?).to be true
end
end
end
end

0 comments on commit bfa85ef

Please sign in to comment.