Skip to content

Commit a6d813c

Browse files
Use super only for before ActiveRecord major version 5
Otherwise, use a hard-coded legacy version of ActiveRecord::Inheritance::ClassMethods#find_sti_class
1 parent a634b77 commit a6d813c

File tree

5 files changed

+66
-47
lines changed

5 files changed

+66
-47
lines changed

lib/inheritance_integer_type/extensions.rb

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,27 @@ def integer_inheritance=(val)
99

1010
def find_sti_class(type_name)
1111
lookup = self._inheritance_mapping[type_name.to_i]
12-
lookup ? super(lookup) : super
12+
if lookup
13+
if ActiveRecord::VERSION::MAJOR < 5
14+
super(lookup)
15+
else
16+
begin
17+
if store_full_sti_class
18+
ActiveSupport::Dependencies.constantize(lookup)
19+
else
20+
compute_type(lookup)
21+
end
22+
rescue NameError
23+
raise SubclassNotFound,
24+
"The single-table inheritance mechanism failed to locate the subclass: '#{type_name}'. " +
25+
"This error is raised because the column '#{inheritance_column}' is reserved for storing the class in case of inheritance. " +
26+
"Please rename this column if you didn't intend it to be used for storing the inheritance class " +
27+
"or overwrite #{name}.inheritance_column to use another column for that information."
28+
end
29+
end
30+
else
31+
super
32+
end
1333
end
1434

1535
def sti_name_with_integer_types
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module InheritanceIntegerType
2-
VERSION = "0.1.1"
2+
VERSION = "0.1.2"
33
end
Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,59 @@
11
require 'spec_helper'
22
require 'pry'
33
describe InheritanceIntegerType do
4+
[3,5].each do |major_version|
5+
let(:base) { Base.create(name: "Hello") }
6+
let(:left) { LeftChild.create(name: "Hello") }
7+
let(:deep) { DeepChild.create(name: "Hello") }
8+
9+
before { allow(ActiveRecord::VERSION).to receive(:MAJOR).and_return(major_version) }
10+
11+
describe "The parent" do
12+
subject { base }
13+
it { is_expected.to be_persisted }
14+
it "has no type" do
15+
expect(subject.type).to be_nil
16+
end
17+
it { is_expected.to eql Base.first }
18+
end
419

5-
let(:base) { Base.create(name: "Hello") }
6-
let(:left) { LeftChild.create(name: "Hello") }
7-
let(:deep) { DeepChild.create(name: "Hello") }
20+
describe "The inherited classes" do
21+
subject { left }
22+
it { is_expected.to be_persisted }
23+
it { is_expected.to eql LeftChild.first }
24+
end
825

9-
describe "The parent" do
10-
subject { base }
11-
it { is_expected.to be_persisted }
12-
it "has no type" do
13-
expect(subject.type).to be_nil
26+
describe "The deep inherited classes" do
27+
subject { deep }
28+
it { is_expected.to be_persisted }
29+
it { is_expected.to eql DeepChild.first }
1430
end
15-
it { is_expected.to eql Base.first }
16-
end
1731

18-
describe "The inherited classes" do
19-
subject { left }
20-
it { is_expected.to be_persisted }
21-
it { is_expected.to eql LeftChild.first }
22-
end
32+
describe "Belongs to associations" do
2333

24-
describe "The deep inherited classes" do
25-
subject { deep }
26-
it { is_expected.to be_persisted }
27-
it { is_expected.to eql DeepChild.first }
28-
end
34+
let(:belong) { BelongsTo.create(base: base, left_child: left) }
35+
subject { belong }
2936

30-
describe "Belongs to associations" do
37+
it "properly assocaites the base class" do
38+
expect(subject.base).to eql base
39+
end
3140

32-
let(:belong) { BelongsTo.create(base: base, left_child: left) }
33-
subject { belong }
41+
it "properly assocaites the children class" do
42+
expect(subject.left_child).to eql left
43+
end
3444

35-
it "properly assocaites the base class" do
36-
expect(subject.base).to eql base
37-
end
3845

39-
it "properly assocaites the children class" do
40-
expect(subject.left_child).to eql left
4146
end
4247

43-
44-
end
45-
46-
describe "Has many associations" do
47-
let(:other) { Other.create }
48-
before do
49-
[base, left, deep].each{|a| a.update_attributes(other: other) }
48+
describe "Has many associations" do
49+
let(:other) { Other.create }
50+
before do
51+
[base, left, deep].each{|a| a.update_attributes(other: other) }
52+
end
53+
subject { other }
54+
it "properly finds the classes through the association" do
55+
expect(other.bases).to match_array [base, left, deep]
56+
end
5057
end
51-
subject { other }
52-
it "properly finds the classes through the association" do
53-
expect(other.bases).to match_array [base, left, deep]
54-
end
55-
5658
end
57-
5859
end

spec/spec_helper.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
require 'support/deep_child'
77
require 'support/other'
88
require 'support/belongs_to'
9-
# require 'support/old_style'
10-
# require 'support/inherit_old_style'
119

1210
RSpec.configure do |config|
1311

spec/support/active_record.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
:adapter => "mysql2",
66
:host => "localhost",
77
:database => "inheritance_integer_type_test",
8-
:username => "root",
9-
:password => "iasEcerdyetvanyet"
8+
:username => "iit",
9+
:password => ""
1010
}
1111
ActiveRecord::Base.establish_connection(config)

0 commit comments

Comments
 (0)