Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to obtain per_page number of a Model in views?

The number of items for a page in Kaminari is defined by:

  1. defining per_page member in model, or
  2. defining default_per_page in config.

My question is, how to obtain that number of items in view?

Update

Pardon me for being not clear. What I want is to show is the order of item in each page. I have a table header:

<th>#</th>

And several columns below:

<td><%= (@sales.current_page - 1) * some_ruby_code + index + 1 %></td>

My question is what some_ruby_code should be?

1) If I substitute some_ruby_code with Sale.per_page, it will throw error when I decided to delete per_page.

2) If I substitute some_ruby_code with Kaminari.config.default_per_page, it won't show the proper value when I add per_page in model.

I want to know whether there are a method in Kaminari which detects the existence of per_page, return its value if exists, or Kaminari.config.default_per_page otherwise.

Thank you!

like image 570
Ismail Faruqi Avatar asked Jun 30 '13 14:06

Ismail Faruqi


2 Answers

You can use Sale.default_per_page.

See https://github.com/kaminari/kaminari/blob/1-0-stable/kaminari-core/lib/kaminari/models/configuration_methods.rb#L16

like image 183
vinc Avatar answered Nov 01 '22 13:11

vinc


You can use ActiveRecord's limit_value method for this, since Kaminari internally uses limit and offset.

User.all.limit(10).limit_value
=> 10

With kaminari is the same:

User.page(3).per(10).limit_value
=> 10

User.page(3).per(10).offset_value
=> 20
like image 26
Mauricio Pasquier Juan Avatar answered Nov 01 '22 13:11

Mauricio Pasquier Juan