Skip to content

Commit 3ecb98e

Browse files
author
Kyle d'Oliveira
committed
If we assign nil to the association, we should not fail
1 parent 45af70d commit 3ecb98e

3 files changed

Lines changed: 16 additions & 3 deletions

File tree

lib/polymorphic_integer_type/extensions.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@ def belongs_to(name, options = {})
1616
mapping[t]
1717
end
1818

19-
define_method "#{foreign_type}=" do |klass|
20-
klass = klass.base_class if klass.kind_of?(Class)
19+
define_method "#{foreign_type}=" do |klass|
20+
klass = klass.base_class if klass.kind_of?(Class) && klass <= ActiveRecord::Base
2121
enum = mapping.key(klass.to_s) || klass
22+
if enum == NilClass
23+
enum = nil
24+
end
2225
super(enum)
2326
end
2427

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module PolymorphicIntegerType
2-
VERSION = "1.0.3"
2+
VERSION = "1.0.4"
33
end

spec/polymorphic_integer_type_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@
1717

1818
let(:link) { Link.create(:source => source, :target => target) }
1919

20+
context "when the source is nil" do
21+
let(:source) { nil }
22+
let(:target) { nil }
23+
it "should have the no id/type for the source" do
24+
expect(link.source_id).to be_nil
25+
expect(link.source_type).to be_nil
26+
expect(link.source).to be_nil
27+
end
28+
end
29+
2030
shared_examples "proper source" do
2131
it "should have the proper id, type and object for the source" do
2232
expect(link.source_id).to eql source.id

0 commit comments

Comments
 (0)