I’m working with kaminari today, and I spent a bit of time getting Draper to co-operate with the pagination. This is a fairly well documented issue, but I figured I’d post about this for my own future reference.
The issue: getting kaminari methods (such as current_page, total_pages, etc) to work on a decorated collection.
The solution: We need to delegate the kaminari methods, so that Draper knows to ignore them.
How to do this?
- Create a separate PaginatingDecorator that inherits from CollectionDraper, and delegate the kaminari methods.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PaginatingDecorator < Draper::CollectionDecorator | |
delegate :current_page, :total_pages, :limit_value | |
end |
- In your ApplicationDecorator class, make sure that CollectionDecorator knows to look at PaginatingDecorator.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApplicationDecorator < Draper::Decorator | |
def self.collection_decorator_class | |
PaginatingDecorator | |
end | |
end |
- If it’s not already the case, make sure your decorator inherits from ApplicationDecorator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AnimalDecorator < ApplicationDecorator | |
decorates :animal | |
delegate_all | |
... | |
end |