Skip to content

Commit 43d02e7

Browse files
authored
Merge pull request #33 from Drew-Goddyn/handle-polymorphic-name
Add fallbacks in case our type mappings don't adhere to what #polymorphic_name gives us
2 parents 5a73ed9 + 529421d commit 43d02e7

8 files changed

Lines changed: 91 additions & 25 deletions

File tree

lib/polymorphic_integer_type/activerecord_5_0_0/polymorphic_array_value_extension.rb

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
11
module PolymorphicIntegerType
22
module PolymorphicArrayValueExtension
3+
4+
# original method:
5+
# def type_to_ids_mapping
6+
# default_hash = Hash.new { |hsh, key| hsh[key] = [] }
7+
# result = values.each_with_object(default_hash) do |value, hash|
8+
# hash[klass(value).polymorphic_name] << convert_to_id(value)
9+
# end
10+
# end
11+
312
def type_to_ids_mapping
4-
super.tap do |result|
5-
association = @associated_table.send(:association)
6-
klass = association.active_record
7-
name = association.name
13+
association = @associated_table.send(:association)
14+
name = association.name
15+
default_hash = Hash.new { |hsh, key| hsh[key] = [] }
16+
values.each_with_object(default_hash) do |value, hash|
17+
klass = respond_to?(:klass) ? klass(value) : klass.class
18+
if association.active_record.respond_to?("#{name}_type_mapping")
19+
mapping = association.active_record.send("#{name}_type_mapping")
20+
key ||= mapping.key(klass.polymorphic_name) if klass.respond_to?(:polymorphic_name)
21+
key ||= mapping.key(klass.sti_name)
22+
key ||= mapping.key(klass.base_class.to_s)
23+
key ||= mapping.key(klass.base_class.sti_name)
824

9-
if klass.respond_to?("#{name}_type_mapping")
10-
result.transform_keys! do |key|
11-
klass.send("#{name}_type_mapping").key(key)
12-
end
25+
hash[key] << convert_to_id(value)
26+
else
27+
hash[klass.polymorphic_name] << convert_to_id(value)
1328
end
14-
result
1529
end
1630
end
1731
end

spec/polymorphic_integer_type_spec.rb

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,15 @@
7373
it "properly finds the object with a find_by" do
7474
expect(Link.find_by(source: source, id: link.id)).to eql link
7575
end
76+
77+
context "when source and target are namedpaced without modifying polymorphic_name" do
78+
it "properly finds the object" do
79+
plant = Namespaced::Plant.create(name: "Mighty", kind: "Oak", owner: owner)
80+
activity = Namespaced::Activity.create(name: "swaying")
81+
link = Link.create(source: plant, target: activity)
82+
expect(Link.where(source: plant, id: link.id).first).to eql link
83+
end
84+
end
7685
end
7786

7887
shared_examples "proper source" do
@@ -103,7 +112,6 @@
103112
expect(source.source_links[0].source).to eql source
104113
end
105114
end
106-
107115
end
108116
context "When a link is given polymorphic record" do
109117
let(:link) { Link.create(source: source) }
@@ -116,9 +124,7 @@
116124

117125
include_examples "proper source"
118126
include_examples "proper target"
119-
120127
end
121-
122128
end
123129

124130
context "When a link is given polymorphic id and type" do
@@ -131,9 +137,7 @@
131137
before { link.update_attributes(target_id: target.id, target_type: target.class.to_s) }
132138
include_examples "proper source"
133139
include_examples "proper target"
134-
135140
end
136-
137141
end
138142

139143
context "When using a relation to the links with eager loading" do
@@ -146,9 +150,7 @@
146150
it "should be able to return the links and the targets" do
147151
expect(cat.source_links).to match_array links
148152
expect(cat.source_links.includes(:target).collect(&:target)).to match_array [water, kibble]
149-
150153
end
151-
152154
end
153155

154156
context "When using a through relation to the links with eager loading" do
@@ -161,9 +163,7 @@
161163
it "should be able to return the links and the targets" do
162164
expect(owner.pet_source_links).to match_array links
163165
expect(owner.pet_source_links.includes(:target).collect(&:target)).to match_array [water, kibble]
164-
165166
end
166-
167167
end
168168

169169
context "When eager loading the polymorphic association" do
@@ -178,16 +178,12 @@
178178
expect(links.first.source).to eql cat
179179
expect(links.last.source).to eql dog
180180
end
181-
182181
end
183182

184183
it "should be able to preload the association" do
185184
l = Link.includes(:source).where(id: link.id).first
186185
expect(l.source).to eql cat
187186
end
188-
189-
190-
191187
end
192188

193189
context "when the association is an STI table" do
@@ -239,7 +235,7 @@ class InlineDrink < ActiveRecord::Base
239235
include_examples "proper target"
240236

241237
it "creates foreign_type mapping method" do
242-
expect(Link.source_type_mapping).to eq({1 => "Person", 2 => "Animal"})
238+
expect(Link.source_type_mapping).to eq({1 => "Person", 2 => "Animal", 3 => "Plant"})
243239
expect(InlineLink.source_type_mapping).to eq({10 => "Person", 11 => "InlineAnimal"})
244240
end
245241

spec/spec_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
require 'support/link'
66
require 'support/animal'
77
require 'support/namespaced_animal'
8+
require 'support/namespaced_plant'
89
require 'support/dog'
910
require 'support/person'
1011
require 'support/food'
1112
require 'support/drink'
13+
require 'support/namespaced_activity'
1214
require 'byebug'
1315

1416
RSpec.configure do |config|

spec/support/configuration.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
PolymorphicIntegerType::Mapping.configuration do |config|
2-
config.add :source, {1 => "Person", 2 => "Animal"}
3-
config.add :target, {1 => "Food", 2 => "Drink"}
2+
config.add :source, {1 => "Person", 2 => "Animal", 3 => "Plant"}
3+
config.add :target, {1 => "Food", 2 => "Drink", 3 => "Activity"}
44
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class CreatePlantTable < ActiveRecord::Migration[5.2]
2+
3+
def up
4+
create_table :plants do |t|
5+
t.string :name
6+
t.string :type
7+
t.string :kind
8+
t.integer :owner_id
9+
end
10+
end
11+
12+
def down
13+
drop_table :plants
14+
end
15+
16+
end
17+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class CreateActivityTable < ActiveRecord::Migration[5.2]
2+
3+
def up
4+
create_table :activities do |t|
5+
t.string :name
6+
end
7+
end
8+
9+
def down
10+
drop_table :activities
11+
end
12+
13+
end
14+
15+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module Namespaced
2+
class Activity< ActiveRecord::Base
3+
include PolymorphicIntegerType::Extensions
4+
5+
self.store_full_sti_class = false
6+
self.table_name = "activities"
7+
8+
has_many :target_links, as: :target, integer_type: true, class_name: "Link"
9+
end
10+
end
11+

spec/support/namespaced_plant.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module Namespaced
2+
class Plant < ActiveRecord::Base
3+
include PolymorphicIntegerType::Extensions
4+
5+
self.store_full_sti_class = false
6+
self.table_name = "plants"
7+
8+
belongs_to :owner, class_name: "Person"
9+
has_many :source_links, as: :source, integer_type: true, class_name: "Link"
10+
end
11+
end

0 commit comments

Comments
 (0)