Skip to content

Commit dc9b23d

Browse files
committed
Hack in Rails 7.2 support
1 parent 031850d commit dc9b23d

File tree

5 files changed

+60
-3
lines changed

5 files changed

+60
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -1 +1,55 @@
1-
require 'polyamorous/activerecord_6.1_ruby_2/join_association'
1+
module Polyamorous
2+
module JoinAssociationExtensions
3+
# Same as #join_constraints, but instead of constructing tables from the
4+
# given block, uses the ones passed
5+
def join_constraints_with_tables(foreign_table, foreign_klass, join_type, alias_tracker, tables)
6+
joins = []
7+
chain = []
8+
9+
reflection.chain.each.with_index do |reflection, i|
10+
table = tables[i]
11+
12+
@table ||= table
13+
chain << [reflection, table]
14+
end
15+
16+
base_klass.with_connection do |connection|
17+
# The chain starts with the target table, but we want to end with it here (makes
18+
# more sense in this context), so we reverse
19+
chain.reverse_each do |reflection, table|
20+
klass = reflection.klass
21+
22+
join_scope = reflection.join_scope(table, foreign_table, foreign_klass)
23+
24+
unless join_scope.references_values.empty?
25+
join_dependency = join_scope.construct_join_dependency(
26+
join_scope.eager_load_values | join_scope.includes_values, Arel::Nodes::OuterJoin
27+
)
28+
join_scope.joins!(join_dependency)
29+
end
30+
31+
arel = join_scope.arel(alias_tracker.aliases)
32+
nodes = arel.constraints.first
33+
34+
if nodes.is_a?(Arel::Nodes::And)
35+
others = nodes.children.extract! do |node|
36+
!Arel.fetch_attribute(node) { |attr| attr.relation.name == table.name }
37+
end
38+
end
39+
40+
joins << table.create_join(table, table.create_on(nodes), join_type)
41+
42+
if others && !others.empty?
43+
joins.concat arel.join_sources
44+
append_constraints(connection, joins.last, others)
45+
end
46+
47+
# The current table in this iteration becomes the foreign table in the next
48+
foreign_table, foreign_klass = table, klass
49+
end
50+
51+
joins
52+
end
53+
end
54+
end
55+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require 'polyamorous/activerecord_6.1_ruby_2/join_association'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require 'polyamorous/activerecord_6.1_ruby_2/join_dependency'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require 'polyamorous/activerecord_6.1_ruby_2/reflection'

lib/ransack/adapters/active_record/context.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def klassify(obj)
109109
#
110110
def join_sources
111111
base, joins = begin
112-
alias_tracker = ::ActiveRecord::Associations::AliasTracker.create(self.klass.connection, @object.table.name, [])
112+
alias_tracker = @object.alias_tracker
113113
constraints = if ::Gem::Version.new(::ActiveRecord::VERSION::STRING) >= ::Gem::Version.new(Constants::RAILS_6_1)
114114
@join_dependency.join_constraints(@object.joins_values, alias_tracker, @object.references_values)
115115
elsif ::Gem::Version.new(::ActiveRecord::VERSION::STRING) >= ::Gem::Version.new(Constants::RAILS_6_0)
@@ -282,7 +282,7 @@ def build_joins(relation)
282282

283283
join_list = join_nodes + convert_join_strings_to_ast(relation.table, string_joins)
284284

285-
alias_tracker = ::ActiveRecord::Associations::AliasTracker.create(self.klass.connection, relation.table.name, join_list)
285+
alias_tracker = relation.alias_tracker(join_list)
286286
join_dependency = if ::Gem::Version.new(::ActiveRecord::VERSION::STRING) >= ::Gem::Version.new(Constants::RAILS_6_0)
287287
Polyamorous::JoinDependency.new(relation.klass, relation.table, association_joins, Arel::Nodes::OuterJoin)
288288
else

0 commit comments

Comments
 (0)