bogdan / datagrid

Gem to create tables grids with sortable columns and filters
MIT License
1.02k stars 115 forks source link

Scope not passed to filters when specified dynamically #289

Closed brendon closed 4 years ago

brendon commented 4 years ago

Previously I've been overriding the scope in the controller using @scope. This seems undocumented, but it works.

I see that I can override the scope like this:

grid:

class ProjectsGrid
 include Datagrid
 scope { Project.includes(:category) }
end

controller:

grid = ProjectsGrid.new(grid_params) do |scope|
  scope.where(:owner_id => current_user.id)
end

Unfortunately it seems that the scope isn't then passed to filters for some reason:

filter(:payment_submethod, :enum, select: :available_payment_submethods, multiple: true, dummy: true) do |values, scope|
  scope.merge OnlineStoreOrder.where(payment_submethod: values)
end

The last example is from my app. When passing in @scope from the controller it arrives at that block, but if I do it via the do |scope| block style above I get scope being undefined method 'merge' for nil:NilClass.

Do you have any pointers on this usage?

brendon commented 4 years ago

Sorry discard this one. I was doing some magic elsewhere that I forgot about!