Skip to content

Commit 756cc15

Browse files
committed
Fix bug in Rails 5.0 when querying polymorphic type columns
The existing patch on PolymorphicArrayValue only handled queries that passed multiple values to a polymorphic type column, for example: Link.where(source: [source_1, source_2]) In order to handle passing a single value, we need to patch the AssociationQueryHandler. This issue only exists in Rails 5.0 and 5.1; it's been fixed in 5.2.
1 parent 90fad83 commit 756cc15

5 files changed

Lines changed: 50 additions & 3 deletions

File tree

lib/polymorphic_integer_type.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1+
ACTIVE_RECORD_VERSION = Gem::Version.new(ActiveRecord::VERSION::STRING)
2+
13
require "polymorphic_integer_type/version"
24
require "polymorphic_integer_type/extensions"
35
require "polymorphic_integer_type/mapping"
4-
if Gem::Version.new(ActiveRecord::VERSION::STRING) < Gem::Version.new("5")
5-
require "polymorphic_integer_type/predicate_builder_extension"
6+
7+
if ACTIVE_RECORD_VERSION < Gem::Version.new("5")
8+
require "polymorphic_integer_type/activerecord_4/predicate_builder_extension"
69
else
7-
require "polymorphic_integer_type/polymorphic_array_value_extension"
10+
require "polymorphic_integer_type/activerecord_5_0_0/polymorphic_array_value_extension"
11+
end
12+
13+
if ACTIVE_RECORD_VERSION >= Gem::Version.new("5.0") && ACTIVE_RECORD_VERSION < Gem::Version.new("5.2.0")
14+
require "polymorphic_integer_type/activerecord_5_0_0/association_query_handler_extension"
815
end
916

1017
module PolymorphicIntegerType; end

lib/polymorphic_integer_type/predicate_builder_extension.rb renamed to lib/polymorphic_integer_type/activerecord_4/predicate_builder_extension.rb

File renamed without changes.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
module PolymorphicIntegerType
2+
module AssociationQueryHandlerExtension
3+
def call(attribute, value)
4+
queries = {}
5+
table = value.associated_table
6+
7+
if value.base_class
8+
queries[table.association_foreign_type.to_s] = polymorphic_value_for(value)
9+
end
10+
11+
queries[table.association_foreign_key.to_s] = value.ids
12+
predicate_builder.build_from_hash(queries)
13+
end
14+
15+
protected
16+
17+
def polymorphic_value_for(value)
18+
table = value.associated_table
19+
association = table.send(:association)
20+
klass = association.active_record
21+
name = association.name
22+
23+
if klass.respond_to?("#{name}_type_mapping")
24+
klass.send("#{name}_type_mapping").key(value.base_class.sti_name)
25+
else
26+
value.base_class.name
27+
end
28+
end
29+
30+
31+
end
32+
end
33+
34+
ActiveRecord::PredicateBuilder::AssociationQueryHandler.prepend(PolymorphicIntegerType::AssociationQueryHandlerExtension)

lib/polymorphic_integer_type/polymorphic_array_value_extension.rb renamed to lib/polymorphic_integer_type/activerecord_5_0_0/polymorphic_array_value_extension.rb

File renamed without changes.

spec/polymorphic_integer_type_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,15 @@
3434
context "when querying the associations" do
3535
let(:source) { cat }
3636
let(:target) { nil }
37+
3738
it "properly finds the object with a where" do
3839
expect(Link.where(source: source, id: link.id).first).to eql link
3940
end
41+
42+
it "properly finds the object when passing an array of sources" do
43+
expect(Link.where(source: [source])).to eq [link]
44+
end
45+
4046
it "properly finds the object with a find_by" do
4147
expect(Link.find_by(source: source, id: link.id)).to eql link
4248
end

0 commit comments

Comments
 (0)