|
2 | 2 |
|
3 | 3 | module ChronoModel
|
4 | 4 | module Patches
|
5 |
| - # This class supports the AR < 7.1 code that expects to receive an |
6 |
| - # Arel::Table as the left join node. We need to replace the node |
7 |
| - # with a virtual table that fetches from the history at a given |
8 |
| - # point in time, we replace the join node with a SqlLiteral node |
9 |
| - # that does not respond to the methods that AR expects. |
10 |
| - # |
11 |
| - # This class provides AR with an object implementing the methods |
12 |
| - # it expects, yet producing SQL that fetches from history tables |
13 |
| - # as-of-time. |
14 |
| - # |
15 |
| - # TODO: Remove when dropping Rails < 7.1 compatibility |
| 5 | + # Replaces the left side of an Arel join (table or table alias) with a SQL |
| 6 | + # literal pointing to the history virtual table at the given as_of_time, |
| 7 | + # preserving any existing table alias. |
16 | 8 | class JoinNode < Arel::Nodes::SqlLiteral
|
17 |
| - attr_reader :name, :table_name, :table_alias, :as_of_time |
| 9 | + attr_reader :as_of_time |
18 | 10 |
|
19 | 11 | def initialize(join_node, history_model, as_of_time)
|
20 |
| - # Handle both Arel::Table (which has .name method) and other join node types |
21 |
| - # (which have .table_name method) to extract the table name consistently |
22 |
| - table_name = |
23 |
| - if join_node.respond_to?(:table_name) |
24 |
| - join_node.table_name |
25 |
| - elsif join_node.respond_to?(:name) |
26 |
| - join_node.name |
27 |
| - else |
28 |
| - raise ArgumentError, "Cannot determine table name from #{join_node.class}: expected :table_name or :name method" |
29 |
| - end |
30 |
| - |
31 |
| - @name = table_name |
32 |
| - @table_name = table_name |
33 |
| - @table_alias = join_node.table_alias if join_node.respond_to?(:table_alias) |
34 |
| - |
35 | 12 | @as_of_time = as_of_time
|
36 | 13 |
|
37 |
| - virtual_table = history_model |
38 |
| - .virtual_table_at(@as_of_time, table_name: @table_alias || @table_name) |
| 14 | + table_name = join_node.table_alias || join_node.name |
| 15 | + virtual_table = history_model.virtual_table_at(@as_of_time, table_name: table_name) |
39 | 16 |
|
40 | 17 | super(virtual_table)
|
41 | 18 | end
|
|
0 commit comments