Skip to content

Commit 976f934

Browse files
author
Kyle d'Oliveira
committed
Add a new extension that will ensure that we can query for associations directly
1 parent d636957 commit 976f934

5 files changed

Lines changed: 41 additions & 7 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ spec/support/active_record.rb
1717
test/tmp
1818
test/version_tmp
1919
tmp
20+
.byebug_history
21+
polymorphic_integer_type_test

lib/polymorphic_integer_type.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require "polymorphic_integer_type/version"
22
require "polymorphic_integer_type/extensions"
33
require "polymorphic_integer_type/mapping"
4+
require "polymorphic_integer_type/polymorphic_array_value_extension"
45

56
module PolymorphicIntegerType; end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module PolymorphicIntegerType
2+
module PolymorphicArrayValueExtension
3+
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
8+
9+
if klass.respond_to?("#{name}_type_mapping")
10+
result.transform_keys! do |key|
11+
klass.send("#{name}_type_mapping").key(key)
12+
end
13+
end
14+
result
15+
end
16+
end
17+
end
18+
end
19+
20+
ActiveRecord::PredicateBuilder::PolymorphicArrayValue.prepend(PolymorphicIntegerType::PolymorphicArrayValueExtension)

spec/polymorphic_integer_type_spec.rb

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@
2424
end
2525
end
2626

27+
context "when querying the associations" do
28+
let(:source) { cat }
29+
let(:target) { nil }
30+
it "properly finds the object with a where" do
31+
expect(Link.where(source: source, id: link.id).first).to eql link
32+
end
33+
it "properly finds the object with a find_by" do
34+
expect(Link.find_by(source: source, id: link.id)).to eql link
35+
end
36+
end
37+
2738
shared_examples "proper source" do
2839
it "should have the proper id, type and object for the source" do
2940
expect(link.source_id).to eql source.id
@@ -47,12 +58,12 @@
4758

4859
context "and the link is accessed through the associations" do
4960
before { link }
50-
61+
5162
it "should have the proper source" do
5263
expect(source.source_links[0].source).to eql source
5364
end
5465
end
55-
66+
5667
end
5768
context "When a link is given polymorphic record" do
5869
let(:link) { Link.create(source: source) }
@@ -137,7 +148,7 @@
137148

138149

139150
end
140-
151+
141152
context "when the association is an STI table" do
142153
let(:link) { Link.create(source: source, target: whiskey) }
143154
let(:source) { Dog.create(name: "Bela", kind: "Dog", owner: owner) }
@@ -147,7 +158,7 @@
147158
expect(link.source).to eql source
148159
end
149160
end
150-
161+
151162
context "when mapping is given inline in the belongs_to model" do
152163
class InlineLink < ActiveRecord::Base
153164
include PolymorphicIntegerType::Extensions
@@ -187,7 +198,7 @@ class InlineDrink < ActiveRecord::Base
187198
include_examples "proper target"
188199

189200
it "creates foreign_type mapping method" do
190-
expect(Link.source_type_mapping).to eq({0 => "Person", 1 => "Animal"})
201+
expect(Link.source_type_mapping).to eq({1 => "Person", 2 => "Animal"})
191202
expect(InlineLink.source_type_mapping).to eq({10 => "Person", 11 => "InlineAnimal"})
192203
end
193204

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, {0 => "Person", 1 => "Animal"}
3-
config.add :target, {0 => "Food", 1 => "Drink"}
2+
config.add :source, {1 => "Person", 2 => "Animal"}
3+
config.add :target, {1 => "Food", 2 => "Drink"}
44
end

0 commit comments

Comments
 (0)