Skip to content

Commit ef8e2cf

Browse files
authored
Merge pull request #1503 from activerecord-hackery/rails-7-2
Make ransack work with Rails 7.2
2 parents 489bf8f + d4b0dfe commit ef8e2cf

File tree

5 files changed

+65
-6
lines changed

5 files changed

+65
-6
lines changed

.github/workflows/test.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ jobs:
1313
fail-fast: false
1414
matrix:
1515
rails:
16+
- 7-2-stable
1617
- v7.1.0
1718
- v7.0.3
1819
- v6.1.6
1920
ruby:
2021
- 3.2.2
2122
- 3.1.4
22-
- 3.0.6
2323
env:
2424
DB: sqlite3
2525
RAILS: ${{ matrix.rails }}
@@ -39,13 +39,13 @@ jobs:
3939
fail-fast: false
4040
matrix:
4141
rails:
42+
- 7-2-stable
4243
- v7.1.0
4344
- v7.0.3
4445
- v6.1.6
4546
ruby:
4647
- 3.2.2
4748
- 3.1.4
48-
- 3.0.6
4949
env:
5050
DB: mysql
5151
RAILS: ${{ matrix.rails }}
@@ -74,13 +74,13 @@ jobs:
7474
fail-fast: false
7575
matrix:
7676
rails:
77+
- 7-2-stable
7778
- v7.1.0
7879
- v7.0.3
7980
- v6.1.6
8081
ruby:
8182
- 3.2.2
8283
- 3.1.4
83-
- 3.0.6
8484
env:
8585
DB: postgres
8686
RAILS: ${{ matrix.rails }}

Gemfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ rails_version = case rails
1515
end
1616

1717
gem 'faker'
18-
gem 'sqlite3'
18+
gem 'sqlite3', '~> 1.4'
1919
gem 'pg'
2020
gem 'pry'
2121
gem 'byebug'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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

lib/polyamorous/polyamorous.rb

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ module Polyamorous
1515
require 'polyamorous/activerecord/join_dependency'
1616
require 'polyamorous/activerecord/reflection'
1717

18+
if ::ActiveRecord.version > ::Gem::Version.new("7.1")
19+
require "polyamorous/activerecord/join_association_7_2"
20+
end
21+
1822
ActiveRecord::Reflection::AbstractReflection.send(:prepend, Polyamorous::ReflectionExtensions)
1923

2024
Polyamorous::JoinDependency.send(:prepend, Polyamorous::JoinDependencyExtensions)

lib/ransack/adapters/active_record/context.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def klassify(obj)
110110
#
111111
def join_sources
112112
base, joins = begin
113-
alias_tracker = ::ActiveRecord::Associations::AliasTracker.create(self.klass.connection, @object.table.name, [])
113+
alias_tracker = @object.alias_tracker
114114
constraints = @join_dependency.join_constraints(@object.joins_values, alias_tracker, @object.references_values)
115115

116116
[
@@ -278,7 +278,7 @@ def build_joins(relation)
278278

279279
join_list = join_nodes + convert_join_strings_to_ast(relation.table, string_joins)
280280

281-
alias_tracker = ::ActiveRecord::Associations::AliasTracker.create(self.klass.connection, relation.table.name, join_list)
281+
alias_tracker = relation.alias_tracker(join_list)
282282
join_dependency = Polyamorous::JoinDependency.new(relation.klass, relation.table, association_joins, Arel::Nodes::OuterJoin)
283283
join_dependency.instance_variable_set(:@alias_tracker, alias_tracker)
284284
join_nodes.each do |join|

0 commit comments

Comments
 (0)