bogdan / datagrid

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

Cannot use Sequel pagination #311

Closed snacks02 closed 1 year ago

snacks02 commented 1 year ago

Sequel has a built-in extension for pagination that doesn't seem to work with Datagrid.

# all_sites_grid.rb
class AllSitesGrid < BaseGrid
  scope do
    Site
  end
end

# sites_controller.rb
grid = AllSitesGrid.new do
  Site.dataset.paginate(1, 25)
end
SitesControllerTest#test_#all_sites_table_works:
Sequel::Error: You cannot paginate a dataset that already has a limit
    app/controllers/sites_controller.rb:269:in `all_sites_table'
    test/controllers/sites_controller_test.rb:248:in `block in <class:SitesControllerTest>'
    test/test_helper.rb:23:in `block in run'
    test/test_helper.rb:23:in `run'
snacks02 commented 1 year ago

The workaround that I came up with:

# all_sites_grid.rb
class AllSitesGrid < BaseGrid
  scope do
    []
  end
end

# sites_controller.rb
grid = AllSitesGrid.new do
  Site.dataset.paginate(1, 25).to_a
end
bogdan commented 1 year ago

Can you open the console and run Site.dataset.paginate(1, 25)? If it generates an exception for you, there is nothing datagrid can do about it and you are using sequel incorrectly. You may search for support in sequel forums.

Anyway, your grid imitation code looks incorrect. It should be:

grid = AllSitesGrid.new do |scope|
  scope.paginate(1, 25)
end
snacks02 commented 1 year ago

It works in the console:

irb(main):001:0> Site.dataset.paginate(1, 25)
=> #<Sequel::Postgres::Dataset: "SELECT * FROM \"sites\" LIMIT 25 OFFSET 0">

Not sure what you mean by grid imitation, but this is the error with your suggestion:

NoMethodError: undefined method `paginate' for Site:Class
bogdan commented 1 year ago

Try the following:

# all_sites_grid.rb
class AllSitesGrid < BaseGrid
  scope do
    Site.dataset
  end
end

# sites_controller.rb
grid = AllSitesGrid.new do |s|
  s.paginate(1, 25)
end
snacks02 commented 1 year ago

The same error even on master:

Sequel::Error: You cannot paginate a dataset that already has a limit
bogdan commented 1 year ago

Try the following in console:

AllSitesGrid.new.scope.paginate(1,25)
AllSitesGrid.new.assets.paginate(1,25)

What is the result?

snacks02 commented 1 year ago
irb(main):001:0> AllSitesGrid.new.scope.paginate(1, 25)
=> #<Sequel::Postgres::Dataset: "SELECT * FROM \"sites\" LIMIT 25 OFFSET 0">
irb(main):002:0> AllSitesGrid.new.assets.paginate(1, 25)
=> #<Sequel::Postgres::Dataset: "SELECT * FROM \"sites\" LIMIT 25 OFFSET 0">
bogdan commented 1 year ago

Try the following too:

AllSitesGrid.new{|s| s.paginate(1,25)}.assets.to_a

Also, let me know, which specific line is showing up in the backtrace with the error.

snacks02 commented 1 year ago

Sorry, but can't you try it? It should be easy to reproduce.

bogdan commented 1 year ago

I did. It is not reproducing. https://github.com/bogdan/datagrid/blob/44129941f0d62bb6c7702cdd9ab554d9217294e5/spec/datagrid/drivers/sequel_spec.rb#L38

snacks02 commented 1 year ago

The test fails though.

Also, doing scope.limit(10) results in the same error.

bogdan commented 1 year ago

I have no idea how limit appears in your code. You need to provide a full controller action, view, grid and model source code.

snacks02 commented 1 year ago

See #312.

snacks02 commented 1 year ago

Thanks!