Skip to content

Commit 7fab9d4

Browse files
author
Kyle d'Oliveira
committed
Have the foreign_type use the base class to deal with STI classes properly
1 parent 77bc33e commit 7fab9d4

5 files changed

Lines changed: 17 additions & 0 deletions

File tree

lib/polymorphic_integer_type/extensions.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def belongs_to(name, options = {})
1717
end
1818

1919
define_method "#{foreign_type}=" do |klass|
20+
klass = klass.base_class if klass.kind_of?(Class)
2021
enum = mapping.key(klass.to_s) || klass
2122
super(enum)
2223
end

spec/polymorphic_integer_type_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,5 +131,15 @@
131131

132132
end
133133

134+
context "when the association is an STI table" do
135+
let(:link) { Link.create(:source => source, :target => whiskey) }
136+
let(:source) { Dog.create(:name => "Bela", :kind => "Dog", :owner => owner) }
137+
it "should have the proper id, type and object for the source" do
138+
expect(link.source_id).to eql source.id
139+
expect(link.source_type).to eql "Animal"
140+
expect(link.source).to eql source
141+
end
142+
end
143+
134144

135145
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)