Skip to content

Commit 25fc029

Browse files
committed
Enhance column type recognition and add dynamic filter spec
- Updated `normalized_column_type` method in `active_record.rb` to handle joined columns by checking if the scope has the column and using a custom field for type detection. - Added a new test in `dynamic_filter_spec.rb` to verify the guessing of column types for joined tables. - Modified schema definitions in `active_record.rb` to use `force: true` for `entries` and `groups` tables to ensure tables are recreated during tests.
1 parent 90e0347 commit 25fc029

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

lib/datagrid/drivers/active_record.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,12 @@ def contains(scope, field, value)
8787
end
8888

8989
def normalized_column_type(scope, field)
90-
return nil unless scope_has_column?(scope, field)
90+
builtin_type = scope_has_column?(scope, field) ?
91+
scope.columns_hash[field.to_s].type :
92+
scope.connection.select_all(
93+
scope.unscope(:select, :order).select(field => "custom_field").limit(0).arel
94+
).column_types['custom_field']&.type
9195

92-
builtin_type = scope.columns_hash[field.to_s].type
9396
{
9497
%i[string text time binary] => :string,
9598
%i[integer primary_key] => :integer,
@@ -100,6 +103,7 @@ def normalized_column_type(scope, field)
100103
}.each do |keys, value|
101104
return value if keys.include?(builtin_type)
102105
end
106+
nil
103107
end
104108

105109
def batch_each(scope, batch_size, &block)

spec/datagrid/filters/dynamic_filter_spec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,36 @@
186186
field: "shipping_date", operation: "<>", value: Date.parse("1996-08-05"),
187187
})
188188
end
189+
190+
it "supports guessing type of joined column" do
191+
skip unless defined?(::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) &&
192+
Entry.connection.is_a?(::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)
193+
194+
group = Group.create!(name: "Test Group")
195+
entry = Entry.create!(name: "Hello World", group:)
196+
197+
grid = test_grid do
198+
scope { Entry.joins(:group) }
199+
filter(
200+
:condition, :dynamic,
201+
operations: %w[= =~],
202+
select: [
203+
["Entry Name", "entries.name"],
204+
["Group Name", "groups.name"]
205+
]
206+
)
207+
end
208+
209+
grid.condition = ["entries.name", "=~", "Hello"]
210+
expect(grid.assets).to include(entry)
211+
212+
grid.condition = ["entries.name", "=~", "Test"]
213+
expect(grid.assets).to_not include(entry)
214+
215+
grid.condition = ["groups.name", "=~", "Test"]
216+
expect(grid.assets).to include(entry)
217+
218+
grid.condition = ["groups.name", "=~", "Hello"]
219+
expect(grid.assets).to_not include(entry)
220+
end
189221
end

spec/support/active_record.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
ActiveRecord::Schema.verbose = false
1111
ActiveRecord::Schema.define(version: 1) do
12-
create_table :entries do |t|
12+
create_table :entries, force: true do |t|
1313
t.integer :group_id
1414
t.string :name
1515
t.string :category
@@ -21,7 +21,7 @@
2121
t.timestamps
2222
end
2323

24-
create_table :groups do |t|
24+
create_table :groups, force: true do |t|
2525
t.string :name
2626
t.float :rating
2727
t.timestamps

0 commit comments

Comments
 (0)