Skip to content

Commit 877a6cc

Browse files
author
Carlos Silva
committed
Fix array join condition for new rails versions
1 parent 4988b47 commit 877a6cc

File tree

5 files changed

+39
-7
lines changed

5 files changed

+39
-7
lines changed

gemfiles/Gemfile.rails-5.2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
source 'https://rubygems.org'
22

3-
gem 'rails', '~> 5.2.2'
3+
gem 'rails', '~> 5.2.4'
44
gem 'pg', '~> 1.1.3'
55
gem "byebug"
66

lib/torque/postgresql/associations/association_scope.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def last_chain_scope(scope, *args)
2020
# 5.2 reflection, owner
2121

2222
reflection = args.size.eql?(2) ? args[0] : args[1]
23+
puts reflection.connected_through_array?.inspect
2324
return super unless reflection.connected_through_array?
2425

2526
table = args[0] if args.size > 2

lib/torque/postgresql/auxiliary_statement.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,16 @@ def build_join(base)
205205
end
206206

207207
# Add the scopes defined by the reflection
208+
# Possibilities:
209+
# table
210+
# table, foreign_klass
211+
# table, foreign_table, foreign_klass
208212
if association.respond_to?(:join_scope)
209-
args = [@query.arel_table]
210-
args << base if association.method(:join_scope).arity.eql?(2)
213+
arity = association.method(:join_scope).arity
214+
args = [@query.arel_table, foreign_table, base]
215+
args.delete_at(1) if arity <= 2 # Delete foreign_table
216+
args.delete_at(1) if arity <= 1 # Delete base (foreign_klass)
217+
211218
@query.merge(association.join_scope(*args))
212219
end
213220

lib/torque/postgresql/config.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module PostgreSQL
44

55
# Stores a version check for compatibility purposes
66
AR521 = (ActiveRecord.gem_version >= Gem::Version.new('5.2.1'))
7+
AR523 = (ActiveRecord.gem_version >= Gem::Version.new('5.2.3'))
78

89
# Use the same logger as the Active Record one
910
def self.logger

lib/torque/postgresql/reflection/abstract_reflection.rb

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,28 @@ def torque_join_keys
1717
method(:join_keys).arity.eql?(0) ? join_keys : join_keys(klass)
1818
end
1919

20+
# Fix for rails 5.2.3 where the join_scope method is the one now
21+
# responsible for building the join condition
22+
if Torque::PostgreSQL::AR523
23+
def join_scope(table, foreign_table, foreign_klass)
24+
return super unless connected_through_array?
25+
26+
predicate_builder = predicate_builder(table)
27+
scope_chain_items = join_scopes(table, predicate_builder)
28+
klass_scope = klass_join_scope(table, predicate_builder)
29+
30+
klass_scope.where!(build_id_constraint_between(table, foreign_table))
31+
klass_scope.where!(type => foreign_klass.polymorphic_name) if type
32+
klass_scope.where!(klass.send(:type_condition, table)) \
33+
if klass.finder_needs_type_condition?
34+
35+
scope_chain_items.inject(klass_scope, &:merge!)
36+
end
37+
end
38+
2039
# Manually build the join constraint
2140
def build_join_constraint(table, foreign_table)
22-
klass_attr = table[torque_join_keys.key.to_s]
23-
source_attr = foreign_table[torque_join_keys.foreign_key.to_s]
24-
25-
result = build_id_constraint(klass_attr, source_attr)
41+
result = build_id_constraint_between(table, foreign_table)
2642
result = table.create_and([result, klass.send(:type_condition, table)]) \
2743
if klass.finder_needs_type_condition?
2844

@@ -61,6 +77,13 @@ def build_id_constraint(klass_attr, source_attr)
6177

6278
private
6379

80+
def build_id_constraint_between(table, foreign_table)
81+
klass_attr = table[torque_join_keys.key.to_s]
82+
source_attr = foreign_table[torque_join_keys.foreign_key.to_s]
83+
84+
build_id_constraint(klass_attr, source_attr)
85+
end
86+
6487
# Prepare a value for an array constraint overlap condition
6588
def cast_constraint_to_array(type, value, should_cast)
6689
base_ready = type.try(:array) && value.is_a?(AREL_ATTR)

0 commit comments

Comments
 (0)