3

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
ZuzannaSt
  • 375
  • 1
  • 13

1 Answers1

7

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
Pardeep Dhingra
  • 3,916
  • 7
  • 30
  • 56
  • I'm actually using `all` to order items by date. The full line is: ` @matches = Match.all.order( "date DESC" ).paginate(:per_page => 10, :page => params[:page]).decorate ` But I did try without `all` and it didn't change a thing. Still an error. – ZuzannaSt May 07 '15 at 10:10
  • Does it mean I have to make a `PaginationDecorator` and add delegation to it? – ZuzannaSt May 07 '15 at 10:52
  • Yes, So that you can reuse that PaginationDecorator for pagination on other models. – Pardeep Dhingra May 07 '15 at 10:58
  • Ok, so after adding it all I'm running into this issue: [issue#650](https://github.com/drapergem/draper/issues/650) – ZuzannaSt May 07 '15 at 11:44
  • can you please update your code here ? have you verified you are using singular decorators ? – Pardeep Dhingra May 07 '15 at 11:58
  • Thanks for trying to help. :) I've updated my answer with the current decorators that are causing `Unknown key: :with. Valid keys are: :context` error. – ZuzannaSt May 07 '15 at 12:08
  • 1
    please inherit Pagination decorator with Draper::CollectionDecorator class PaginatingDecorator < Draper::CollectionDecorator – Pardeep Dhingra May 07 '15 at 12:10
  • Yesss it works! Thank you so much! Please update your answer with the explanation so I can approve it. :)) – ZuzannaSt May 07 '15 at 12:15