README.rdoc 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. = Kaminari
  2. A Scope & Engine based, clean, powerful, customizable and sophisticated paginator for modern web app frameworks and ORMs
  3. == Features
  4. === Clean
  5. Does not globally pollute +Array+, +Hash+, +Object+ or <tt>AR::Base</tt>.
  6. === Easy to use
  7. Just bundle the gem, then your models are ready to be paginated. No configuration required. Don't have to define anything in your models or helpers.
  8. === Simple scope-based API
  9. Everything is method chainable with less "Hasheritis". You know, that's the Rails 3 way.
  10. No special collection class or anything for the paginated values, instead using a general <tt>AR::Relation</tt> instance. So, of course you can chain any other conditions before or after the paginator scope.
  11. === Customizable engine-based I18n-aware helper
  12. As the whole pagination helper is basically just a collection of links and non-links, Kaminari renders each of them through its own partial template inside the Engine. So, you can easily modify their behaviour, style or whatever by overriding partial templates.
  13. === ORM & template engine agnostic
  14. Kaminari supports multiple ORMs (ActiveRecord, Mongoid, MongoMapper) multiple web frameworks (Rails, Sinatra), and multiple template engines (ERB, Haml).
  15. === Modern
  16. The pagination helper outputs the HTML5 <nav> tag by default. Plus, the helper supports Rails 3 unobtrusive Ajax.
  17. == Supported versions
  18. * Ruby 1.8.7, 1.9.2, 1.9.3, 2.0 (trunk)
  19. * Rails 3.0.x, 3.1, 3.2, 4.0 (edge)
  20. * Haml 3+
  21. * Mongoid 2+
  22. * MongoMapper 0.9+
  23. * DataMapper 1.1.0+
  24. == Install
  25. Put this line in your Gemfile:
  26. gem 'kaminari'
  27. Then bundle:
  28. % bundle
  29. == Usage
  30. === Query Basics
  31. * the +page+ scope
  32. To fetch the 7th page of users (default +per_page+ is 25)
  33. User.page(7)
  34. * the +per+ scope
  35. To show a lot more users per each page (change the +per_page+ value)
  36. User.page(7).per(50)
  37. Note that the +per+ scope is not directly defined on the models but is just a method defined on the page scope. This is absolutely reasonable because you will never actually use +per_page+ without specifying the +page+ number.
  38. * the +padding+ scope
  39. Occasionally you need to padding a number of records that is not a multiple of the page size.
  40. User.page(7).per(50).padding(3)
  41. Note that the +padding+ scope also is not directly defined on the models.
  42. === General configuration options
  43. You can configure the following default values by overriding these values using <tt>Kaminari.configure</tt> method.
  44. default_per_page # 25 by default
  45. window # 4 by default
  46. outer_window # 0 by default
  47. left # 0 by default
  48. right # 0 by default
  49. page_method_name # :page by default
  50. param_name # :page by default
  51. There's a handy generator that generates the default configuration file into config/initializers directory.
  52. Run the following generator command, then edit the generated file.
  53. % rails g kaminari:config
  54. * changing +page_method_name+
  55. You can change the method name `page` to `bonzo` or `plant` or whatever you like, in order to play nice with existing `page` method or association or scope or any other plugin that defines `page` method on your models.
  56. === Configuring default +per_page+ value for each model
  57. * +paginates_per+
  58. You can specify default +per_page+ value per each model using the following declarative DSL.
  59. class User < ActiveRecord::Base
  60. paginates_per 50
  61. end
  62. === Controllers
  63. * the page parameter is in <tt>params[:page]</tt>
  64. Typically, your controller code will look like this:
  65. @users = User.order(:name).page params[:page]
  66. === Views
  67. * the same old helper method
  68. Just call the +paginate+ helper:
  69. <%= paginate @users %>
  70. This will render several <tt>?page=N</tt> pagination links surrounded by an HTML5 <+nav+> tag.
  71. === Helpers
  72. * the +paginate+ helper method
  73. <%= paginate @users %>
  74. This would output several pagination links such as <tt>« First ‹ Prev ... 2 3 4 5 6 7 8 9 10 ... Next › Last »</tt>
  75. * specifing the "inner window" size (4 by default)
  76. <%= paginate @users, :window => 2 %>
  77. This would output something like <tt>... 5 6 7 8 9 ...</tt> when 7 is the current page.
  78. * specifing the "outer window" size (0 by default)
  79. <%= paginate @users, :outer_window => 3 %>
  80. This would output something like <tt>1 2 3 4 ...(snip)... 17 18 19 20</tt> while having 20 pages in total.
  81. * outer window can be separetely specified by +left+, +right+ (0 by default)
  82. <%= paginate @users, :left => 1, :right => 3 %>
  83. This would output something like <tt>1 ...(snip)... 18 19 20</tt> while having 20 pages in total.
  84. * changing the parameter name (:+param_name+) for the links
  85. <%= paginate @users, :param_name => :pagina %>
  86. This would modify the query parameter name on each links.
  87. * extra parameters (:+params+) for the links
  88. <%= paginate @users, :params => {:controller => 'foo', :action => 'bar'} %>
  89. This would modify each link's +url_option+. :+controller+ and :+action+ might be the keys in common.
  90. * Ajax links (crazy simple, but works perfectly!)
  91. <%= paginate @users, :remote => true %>
  92. This would add <tt>data-remote="true"</tt> to all the links inside.
  93. * the +link_to_next_page+ helper method
  94. <%= link_to_next_page @items, 'Next Page' %>
  95. This simply renders a link to the next page. This would be helpful for creating "Twitter like" pagination feature.
  96. * the +page_entries_info+ helper method
  97. <%= page_entries_info @users %>
  98. This renders a helpful message with numbers of displayed vs. total entries.
  99. === I18n and labels
  100. The default labels for 'first', 'last', 'previous', '...' and 'next' are stored in the I18n yaml inside the engine, and rendered through I18n API. You can switch the label value per I18n.locale for your internationalized application.
  101. Keys and the default values are the following. You can override them by adding to a YAML file in your <tt>Rails.root/config/locales</tt> directory.
  102. en:
  103. views:
  104. pagination:
  105. first: "&laquo; First"
  106. last: "Last &raquo;"
  107. previous: "&lsaquo; Prev"
  108. next: "Next &rsaquo;"
  109. truncate: "..."
  110. === Customizing the pagination helper
  111. Kaminari includes a handy template generator.
  112. * to edit your paginator
  113. Run the generator first,
  114. % rails g kaminari:views default
  115. then edit the partials in your app's <tt>app/views/kaminari/</tt> directory.
  116. * for Haml users
  117. Haml templates generator is also available by adding the <tt>-e haml</tt> option (this is automatically invoked when the default template_engine is set to Haml).
  118. % rails g kaminari:views default -e haml
  119. * themes
  120. The generator has the ability to fetch several sample template themes from
  121. the external repository (https://github.com/amatsuda/kaminari_themes) in
  122. addition to the bundled "default" one, which will help you creating a nice
  123. looking paginator.
  124. % rails g kaminari:views THEME
  125. To see the full list of avaliable themes, take a look at the themes repository,
  126. or just hit the generator without specifying +THEME+ argument.
  127. % rails g kaminari:views
  128. * multiple themes
  129. To utilize multiple themes from within a single application, create a directory within the app/views/kaminari/ and move your custom template files into that directory.
  130. % rails g kaminari:views default (skip if you have existing kaminari views)
  131. % cd app/views/kaminari
  132. % mkdir my_custom_theme
  133. % cp _*.html.* my_custom_theme/
  134. Next reference that directory when calling the paginate method:
  135. <%= paginate @users, :theme => 'my_custom_theme' %>
  136. Customize away!
  137. Note: if the theme isn't present or none is specified, kaminari will default back to the views included within the gem.
  138. === Paginating a generic Array object
  139. Kaminari provides an Array wrapper class that adapts a generic Array object to the <tt>paginate</tt> view helper.
  140. However, the <tt>paginate</tt> helper doesn't automatically handle your Array object (this is intentional and by design).
  141. <tt>Kaminari::paginate_array</tt> method converts your Array object into a paginatable Array that accepts <tt>page</tt> method.
  142. Kaminari.paginate_array(my_array_object).page(params[:page]).per(10)
  143. You can specify the `total_count` value through options Hash. This would be helpful when handling an Array-ish object that has a different `count` value from actual `count` such as RSolr search result.
  144. == Creating friendly URLs and caching
  145. Because of the `page` parameter and Rails 3 routing, you can easily generate SEO and user-friendly URLs. For any resource you'd like to paginate, just add the following to your `routes.rb`:
  146. resources :my_resources do
  147. get 'page/:page', :action => :index, :on => :collection
  148. end
  149. This will create URLs like `/my_resources/page/33` instead of `/my_resources?page=33`. This is now a friendly URL, but it also has other added benefits...
  150. Because the `page` parameter is now a URL segment, we can leverage on Rails page caching[http://guides.rubyonrails.org/caching_with_rails.html#page-caching]!
  151. NOTE: In this example, I've pointed the route to my `:index` action. You may have defined a custom pagination action in your controller - you should point `:action => :your_custom_action` instead.
  152. == Sinatra/Padrino support
  153. Since version 0.13.0, kaminari started to support Sinatra or Sinatra-based frameworks experimentally.
  154. To use kaminari and its helpers with these frameworks,
  155. require 'kaminari/sinatra'
  156. or edit gemfile:
  157. gem 'kaminari', :require => 'kaminari/sinatra'
  158. More features are coming, and again, this is still experimental. Please let us know if you found anything wrong with the Sinatra support.
  159. == For more information
  160. Check out Kaminari recipes on the GitHub Wiki for more advanced tips and techniques.
  161. https://github.com/amatsuda/kaminari/wiki/Kaminari-recipes
  162. == Build Status {<img src="https://secure.travis-ci.org/amatsuda/kaminari.png"/>}[http://travis-ci.org/amatsuda/kaminari]
  163. == Questions, Feedback
  164. Feel free to message me on Github (amatsuda) or Twitter (@a_matsuda) ☇☇☇ :)
  165. == Contributing to Kaminari
  166. * Fork, fix, then send me a pull request.
  167. == Copyright
  168. Copyright (c) 2011 Akira Matsuda. See MIT-LICENSE for further details.