Skip to content

Commit d2b2b31

Browse files
author
Kyle d'Oliveira
authored
Merge pull request #27 from mctaylorpants/replace-keys-patch
Prevent ActiveRecord from reloading associated records when the STI name is different
2 parents 8bf0411 + ffb91aa commit d2b2b31

4 files changed

Lines changed: 32 additions & 5 deletions

File tree

lib/polymorphic_integer_type.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,8 @@
1414
require "polymorphic_integer_type/activerecord_5_0_0/association_query_handler_extension"
1515
end
1616

17+
if ACTIVE_RECORD_VERSION < Gem::Version.new("5.2.0")
18+
require "polymorphic_integer_type/activerecord_4/belongs_to_polymorphic_association_extension"
19+
end
20+
1721
module PolymorphicIntegerType; end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module ActiveRecord
2+
module Associations
3+
class BelongsToPolymorphicAssociation < BelongsToAssociation
4+
private def replace_keys(record)
5+
super
6+
owner[reflection.foreign_type] = record.class.base_class
7+
end
8+
end
9+
end
10+
end

spec/polymorphic_integer_type_spec.rb

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,19 @@
2424
end
2525
end
2626

27-
context "when the source is a class that modifies the sti_name" do
27+
context "when the source is a class that modifies the sti_name or polymorphic_name" do
2828
it "properly sets the source_type to the modified class name" do
2929
link = Link.new(source: Namespaced::Animal.new)
3030
expect(link.source_type).to eql "Animal"
3131
end
32+
33+
it "can read dirty attributes from an associated object" do
34+
animal = Namespaced::Animal.create!(name: "Oldie")
35+
animal.name = "Newton"
36+
link = Link.create!(source: animal)
37+
38+
expect(link.source.name).to eq("Newton")
39+
end
3240
end
3341

3442
context "when querying the associations" do
@@ -109,7 +117,7 @@
109117

110118
end
111119

112-
context "When using a relation to the links with eagar loading" do
120+
context "When using a relation to the links with eager loading" do
113121
let!(:links){
114122
[Link.create(source: source, target: kibble),
115123
Link.create(source: source, target: water)]
@@ -124,7 +132,7 @@
124132

125133
end
126134

127-
context "When using a through relation to the links with eagar loading" do
135+
context "When using a through relation to the links with eager loading" do
128136
let!(:links){
129137
[Link.create(source: source, target: kibble),
130138
Link.create(source: source, target: water)]
@@ -139,7 +147,7 @@
139147

140148
end
141149

142-
context "When eagar loading the polymorphic association" do
150+
context "When eager loading the polymorphic association" do
143151
let(:link) { Link.create(source_id: source.id, source_type: source.class.to_s) }
144152
let(:source) { cat }
145153

@@ -160,6 +168,7 @@
160168
end
161169

162170

171+
163172
end
164173

165174
context "when the association is an STI table" do

spec/support/namespaced_animal.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,9 @@ class Animal < ActiveRecord::Base
33

44
self.store_full_sti_class = false
55
self.table_name = "animals"
6+
7+
def self.polymorphic_name
8+
"Animal"
9+
end
610
end
7-
end
11+
end

0 commit comments

Comments
 (0)