Skip to content

Commit 085b54e

Browse files
author
Kyle d'Oliveira
committed
Merge branch 'master' into rails-4
Conflicts: lib/polymorphic_integer_type/extensions.rb
2 parents 795014e + 959f12b commit 085b54e

6 files changed

Lines changed: 31 additions & 3 deletions

File tree

lib/polymorphic_integer_type/extensions.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ def belongs_to(name, scope = nil, options = {})
1717
mapping[t]
1818
end
1919

20-
define_method "#{foreign_type}=" do |klass|
21-
enum = mapping.key(klass.to_s) || klass
20+
define_method "#{foreign_type}=" do |klass|
21+
enum = mapping.key(klass.to_s)
22+
enum ||= mapping.key(klass.base_class.to_s) if klass.kind_of?(Class) && klass <= ActiveRecord::Base
23+
enum ||= klass if klass != NilClass
2224
super(enum)
2325
end
2426

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.1"
2+
VERSION = "1.0.5"
33
end

spec/polymorphic_integer_type_spec.rb

Lines changed: 20 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
@@ -131,5 +141,15 @@
131141

132142
end
133143

144+
context "when the association is an STI table" do
145+
let(:link) { Link.create(:source => source, :target => whiskey) }
146+
let(:source) { Dog.create(:name => "Bela", :kind => "Dog", :owner => owner) }
147+
it "should have the proper id, type and object for the source" do
148+
expect(link.source_id).to eql source.id
149+
expect(link.source_type).to eql "Animal"
150+
expect(link.source).to eql source
151+
end
152+
end
153+
134154

135155
end

spec/spec_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require 'support/configuration'
44
require 'support/link'
55
require 'support/animal'
6+
require 'support/dog'
67
require 'support/person'
78
require 'support/food'
89
require 'support/drink'

spec/support/dog.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Dog < Animal
2+
3+
4+
end

spec/support/migrations/2_create_animal_table.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ class CreateAnimalTable < ActiveRecord::Migration
33
def up
44
create_table :animals do |t|
55
t.string :name
6+
t.string :type
67
t.string :kind
78
t.integer :owner_id
89
end

0 commit comments

Comments
 (0)