Skip to content

Instantly share code, notes, and snippets.

@rust
Created December 22, 2011 07:24
Show Gist options
  • Select an option

  • Save rust/1509352 to your computer and use it in GitHub Desktop.

Select an option

Save rust/1509352 to your computer and use it in GitHub Desktop.
lib/kaminari_patch.rb
= paginator.render do
%div
= prev_page_tag
= (current_page.to_i - 1) * per_page + 1
\-
= [current_page.to_i * per_page, total_count].min
\/
= total_count
= next_page_tag
module Kaminari
module ActionViewExtension
def paginate(scope, options = {}, &block)
paginator = Kaminari::Helpers::Paginator.new self, options.reverse_merge(:current_page => scope.current_page, :num_pages => scope.num_pages, :per_page => scope.limit_value, :param_name => Kaminari.config.param_name, :remote => false, :total_count => scope.total_count)
paginator.to_s
end
end
end
@amatsuda
Copy link
Copy Markdown

# You can do it using Kaminari::PaginatableArray like this:

> arr = Kaminari.paginate_array((1..10).to_a, :total_count => 34).page(1).per(10)
 => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 
> arr.total_count
 => 34

# not sure whether this looks better though

@rust
Copy link
Copy Markdown
Author

rust commented Dec 22, 2011

In paginate views (app/views/kaminari/_paginate.html.haml), I can't use collection method, such as total_count . So in order to use such method, I use this patch that assigns total_count as :locals in partial view.

@amatsuda
Copy link
Copy Markdown

Ugh. I haven't noticed that people need total_count in views, but yes, probably that's kaminari's fault.
Feel free to raise an issue or PR if you need to put that feature in kaminari core. Thank you!

@cthurst
Copy link
Copy Markdown

cthurst commented May 17, 2012

Thanks for this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment