Skip to content

Commit 90e0347

Browse files
committed
Refactor and optimize query handling in Datagrid
- Simplified `append_column_queries` method in `active_record.rb` by using early return for empty columns and removing redundant code. - Enhanced `apply` method in `base_filter.rb` to check for `dummy?` before proceeding with value application. - Improved `apply` method in `date_filter.rb` to handle `dummy?` condition and format date as timestamp only when necessary. - Updated test in `columns_spec.rb` to use grouped scope for better query testing.
1 parent 601fea0 commit 90e0347

File tree

4 files changed

+9
-9
lines changed

4 files changed

+9
-9
lines changed

lib/datagrid/drivers/active_record.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,10 @@ def to_scope(scope)
3131
end
3232

3333
def append_column_queries(assets, columns)
34-
if columns.present?
35-
assets = assets.select(assets.klass.arel_table[Arel.star]) if assets.select_values.empty?
36-
columns = columns.map { |c| "#{c.query} AS #{c.name}" }
37-
assets = assets.select(*columns)
38-
end
39-
assets
34+
return assets if columns.empty?
35+
assets = assets.select(assets.klass.arel_table[Arel.star]) if assets.select_values.empty?
36+
columns = columns.map { |c| "#{c.query} AS #{c.name}" }
37+
assets.select(*columns)
4038
end
4139

4240
def where(scope, attribute, value)

lib/datagrid/filters/base_filter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def unapplicable_value?(value)
3535
end
3636

3737
def apply(grid_object, scope, value)
38-
return scope if unapplicable_value?(value)
38+
return scope if dummy? || unapplicable_value?(value)
3939

4040
result = execute(value, scope, grid_object)
4141

lib/datagrid/filters/date_filter.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ def default_input_options
1212
end
1313

1414
def apply(grid_object, scope, value)
15-
value = Datagrid::Utils.format_date_as_timestamp(value) if grid_object.driver.timestamp_column?(scope, name)
15+
if !dummy? && grid_object.driver.timestamp_column?(scope, name)
16+
value = Datagrid::Utils.format_date_as_timestamp(value)
17+
end
1618
super
1719
end
1820

spec/datagrid/columns_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ class Report27 < Datagrid::Base
220220

221221
it "supports defining a query for a column" do
222222
report = test_grid do
223-
scope { Entry }
223+
scope { Entry.group("entries.id") }
224224
filter(:name)
225225
column(:id)
226226
column(:sum_group_id, "sum(group_id)")

0 commit comments

Comments
 (0)