Search code examples
ruby-on-railstwitter-bootstrapdecoratorwill-paginatedraper

Draper with Bootstrap Pagination - undefined method 'total_pages'


I'm using Draper to decorate my views and move some logic out of them but I'm struggling with this question - how to setup Draper with Bootstrap Pagination (will_paginate)?

By default I have this:

delegate_all

And from Draper documentation I've tried adding this:

delegate :current_page, :per_page, :offset, :total_entries, :total_pages

But it still returns an error when calling pagination in the view. My controller defines decoration and pagination like this:

@matches = Match.all.paginate(:per_page => 10, :page => params[:page]).decorate

And my view:

<%= will_paginate @matches, renderer: BootstrapPagination::Rails %>

Update:

class ApplicationDecorator < Draper::Decorator
  def self.collection_decorator_class
    PaginatingDecorator
  end
end


class PaginatingDecorator < Draper::Decorator
  # support for will_paginate
  delegate :current_page, :total_entries, :total_pages, :per_page, :offset
end


class PlayerDecorator < ApplicationDecorator
  delegate_all
  decorates_association :matches


class MatchDecorator < ApplicationDecorator
  delegate_all
  decorates_association :players

Solution

  • https://github.com/drapergem/draper/issues/429

    # app/decorators/application_decorator.rb
    class ApplicationDecorator < Draper::Decorator
      def self.collection_decorator_class
        PaginatingDecorator
      end
    end
    
    # app/decorators/paginating_decorator.rb
    class PaginatingDecorator < Draper::CollectionDecorator
      # support for will_paginate
      delegate :current_page, :total_entries, :total_pages, :per_page, :offset
    end
    
    # app/decorators/whatever_decorator.rb
    class WhateverDecorator < ApplicationDecorator
    end