Skip to content

Commit bb9b73c

Browse files
author
Kyle d'Oliveira
authored
Merge pull request #26 from mctaylorpants/fix-polymorphic-integer-lookup-rails-5-0
Fix polymorphic integer lookup rails 5.0
2 parents 6949640 + 78f3e39 commit bb9b73c

5 files changed

Lines changed: 54 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: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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(query_value)
18+
table = query_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+
type_mapping = klass.send("#{name}_type_mapping")
25+
26+
type_mapping.key(query_value.value.class.sti_name) ||
27+
type_mapping.key(query_value.base_class.to_s) ||
28+
type_mapping.key(query_value.base_class.sti_name)
29+
else
30+
query_value.base_class.name
31+
end
32+
end
33+
34+
35+
end
36+
end
37+
38+
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)