Skip to content

Commit a390e4f

Browse files
committed
Fix rails 7 error
- Updated `lib/datagrid/drivers/active_record.rb` to fix the dynamic filter error by introducing `arel_field_with_alias` method. - Modified the selection of fields to use `arel_field_with_alias` for better handling of custom fields. - Updated `CHANGELOG.md` to reflect the changes in version 2.0.10.
1 parent e536cf8 commit a390e4f

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## [2.0.10]
4+
5+
* Fix dynamic filter error [#349](https://github.com/bogdan/datagrid/issues/349).
6+
37
## [2.0.9]
48

59
* Use new Rails 7.1 `locals` notation in ERB partials.

lib/datagrid/drivers/active_record.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,9 @@ def builtin_type(scope, field)
9191
scope.columns_hash[field.to_s].type
9292
else
9393
begin
94+
arel_field = arel_field_with_alias(scope, field)
9495
scope.connection.select_all(
95-
scope.unscope(:select, :order).select(field => "custom_field").limit(0).arel
96+
scope.unscope(:select, :order).select(arel_field).limit(0).arel
9697
).column_types['custom_field']&.type
9798
rescue ::ActiveRecord::StatementInvalid
9899
nil
@@ -139,6 +140,13 @@ def can_preload?(scope, association)
139140

140141
protected
141142

143+
def arel_field_with_alias(scope, field)
144+
unless defined?(::Arel::Nodes::Node) && field.is_a?(::Arel::Nodes::Node)
145+
field = Arel.sql(field.to_s)
146+
end
147+
field.as("custom_field")
148+
end
149+
142150
def prefix_table_name(scope, field)
143151
scope_has_column?(scope, field) ? [scope.table_name, field].join(".") : field
144152
end

0 commit comments

Comments
 (0)