Skip to content

Commit

Permalink
Fixes leikind#82
Browse files Browse the repository at this point in the history
Only call group when there actually is a group set in WiceGrid#read relation

This fixes a bug with wrong paginated result set when having a wice_grid with
an include or column with assoc option on a many association. If this is the
case the grid tries to eager load this association (due to the include/references
on the relation). Rails then normally gets the correct page set by performing an
additonal query to get the matching IDs without the eager load, however this
functionality is disabled when there is a group set on the relation. Because
WiceGrid always called group even when there was no group set this behaviour was
always disabled, leading to an incorrect page set.

By only calling group when there actually is a group we fix this problem in most
cases. However, when providing a group to the grid, problems still occur, but so
does providing a group to a relation with eager load in PostgreSQL in Rails
itself already (e.g. `Author.eager_load(:books).group(:id)`). It might be better
to never eager load associations, but instead always preload them and add the
sort/filter conditions using a left_joins (in combination with a distinct to get
each main model only once).

Also return Arel in get_custom_order_reference when custom order is SqlLiteral

Fixes 'Query method called with non-attribute argument(s)' when using Arel.sql
for custom order in combination with Ruby 3. Should already have been fixed when
the deprecation warning was fixed, but apparently it was not.

Fixup c7a9da2

fix for issue leikind#82
  • Loading branch information
kreintjes authored and afdev82 committed Oct 24, 2024
1 parent a974053 commit fffbc1c
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion gemfiles/rails_5.0.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ PATH
wice_grid (4.1.0)
coffee-rails (> 3.2)
kaminari (~> 1.1)
rails (~> 5.0, < 5.3)
rails (>= 5.0, < 7)

GEM
remote: https://rubygems.org/
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails_5.1.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ PATH
wice_grid (4.1.0)
coffee-rails (> 3.2)
kaminari (~> 1.1)
rails (~> 5.0, < 5.3)
rails (>= 5.0, < 7)

GEM
remote: https://rubygems.org/
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails_5.2.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ PATH
wice_grid (4.1.0)
coffee-rails (> 3.2)
kaminari (~> 1.1)
rails (~> 5.0, < 5.3)
rails (>= 5.0, < 7)

GEM
remote: https://rubygems.org/
Expand Down
2 changes: 1 addition & 1 deletion lib/wice/columns/column_custom_dropdown.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def generate_conditions(table_alias, opts) #:nodoc:
opts = (opts.is_a?(Array) && opts.size == 1) ? opts[0] : opts

column_name =
"#{@column_wrapper.alias_or_table_name(table_alias)}.#{@column_wrapper.name}"
"#{@column_wrapper.alias_or_table_name(table_alias)}.\"#{@column_wrapper.name}\""
if opts.is_a?(Array)
opts_with_special_values, normal_opts = opts.partition { |v| ::Wice::GridTools.special_value(v) }

Expand Down
4 changes: 2 additions & 2 deletions lib/wice_grid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,8 @@ def read #:nodoc:
relation = @relation
.includes(@ar_options[:include])
.joins(@ar_options[:joins])
.group(@ar_options[:group])
.merge(@ar_options[:conditions])
relation = relation.group(@ar_options[:group]) if @ar_options[:group].present?
relation = add_references relation
relation = apply_sort_by relation

Expand Down Expand Up @@ -595,7 +595,7 @@ def get_custom_order_reference
end
end

return custom_order if custom_order.nil? || custom_order.is_a?(Arel::Attributes::Attribute)
return custom_order if custom_order.nil? || custom_order.is_a?(Arel::Attributes::Attribute) || custom_order.is_a?(Arel::Nodes::SqlLiteral)
return custom_order.gsub(/\?/, fully_qualified_column_name) if custom_order.is_a?(String)
return custom_order.call(fully_qualified_column_name) if custom_order.is_a?(Proc)
raise WiceGridArgumentError.new("invalid custom order #{custom_order.inspect}")
Expand Down

0 comments on commit fffbc1c

Please sign in to comment.