<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>yonggu</title>
    <link>https://ruby-china.org/yonggu</link>
    <description></description>
    <language>en-us</language>
    <item>
      <title>发布用于分页的 gem - simple_paginate</title>
      <description>&lt;p&gt;最近手上的项目需要使用到分页，并且只需要在前一页和后一页的导航，不需要链接到的具体的某一页，第一页或者最后一页。will_paginate 和 Kaminari 有点过于复杂，并且它们都会发送两条 SQL 语句来实现分页。于是就做了&lt;a href="https://github.com/xinminlabs/simple_paginate" rel="nofollow" target="_blank" title=""&gt;simple_paginate&lt;/a&gt;来通过发送一条 SQL 语句来实现，具体说明请看下面来自 README 的内容：&lt;/p&gt;
&lt;h2 id="simple_paginate"&gt;simple_paginate&lt;/h2&gt;
&lt;p&gt;Simple pagination solution for previous and next page navigation.&lt;/p&gt;
&lt;h2 id="Why simple_paginate"&gt;Why simple_paginate&lt;/h2&gt;
&lt;p&gt;We saw some websites using &lt;code&gt;will_paginate&lt;/code&gt; or &lt;code&gt;kaminari&lt;/code&gt; for pagination, but they just need previous and next page navigation, &lt;code&gt;will_paginate&lt;/code&gt; or &lt;code&gt;kaminari&lt;/code&gt; is overqualified.&lt;/p&gt;

&lt;p&gt;Pagination in databae sometimes is slow because&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;it sends a sql with OFFSET to get collection&lt;/li&gt;
&lt;li&gt;it sends additional sql with COUNT to get total number of record.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;code&gt;simple_paginate&lt;/code&gt; eliminates the additional COUNT sql, make your pagination faster.&lt;/p&gt;
&lt;h2 id="How simple_paginate works"&gt;How simple_paginate works&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;will_paginate&lt;/code&gt; and &lt;code&gt;kaminari&lt;/code&gt; uses &lt;code&gt;page&lt;/code&gt; and &lt;code&gt;per_page&lt;/code&gt; to calculate offset and limit, then send 2 sqls&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT * FROM posts OFFSET 20 LIMIT 10;
SELECT COUNT(*) FROM posts;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;after getting total count, they can calculate total pages, then render page numbers, prev, next, first and last page links.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;simple_paginate&lt;/code&gt; also uses &lt;code&gt;page&lt;/code&gt; and &lt;code&gt;per_page&lt;/code&gt; to calculate offset and limit, but it only sends 1 sql&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT * FROM posts OFFSET 20 LIMIT 11;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;it fetches one more record (11 = 10 + 1) to calculate if there is a next page records, so it doesn't need to send COUNT sql.&lt;/p&gt;
&lt;h2 id="Usage"&gt;Usage&lt;/h2&gt;&lt;h3 id="Query Basics"&gt;Query Basics&lt;/h3&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;## perform a paginate query:
@users = User.paginate(:page =&amp;gt; params[:page])
&lt;/code&gt;&lt;/pre&gt;&lt;h3 id="Helpers"&gt;Helpers&lt;/h3&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;## render previous page link:
&amp;lt;%= link_to_previous_page @users, 'Previous' %&amp;gt;

## render next page link:
&amp;lt;%= link_to_next_page @users, 'Next' %&amp;gt;

## render previous page link and next page link
&amp;lt;%= simple_paginate @users
&lt;/code&gt;&lt;/pre&gt;&lt;h3 id="General configuration options"&gt;General configuration options&lt;/h3&gt;
&lt;p&gt;You can configure the following default values by overriding these values using SimplePaginate.configure method.&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;default_per_page    # 25 by default
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There's a handy generator that generates the default configuration file into config/initializers directory. Run the following generator command, then edit the generated file.&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;% rails g simple_paginate:config
&lt;/code&gt;&lt;/pre&gt;&lt;h3 id="Customizing the pagination helper"&gt;Customizing the pagination helper&lt;/h3&gt;
&lt;p&gt;SimplePaginate includes a handy template generator, To edit your paginator, run the generator first:&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;% rails g simple_paginate:views 
&lt;/code&gt;&lt;/pre&gt;</description>
      <author>yonggu</author>
      <pubDate>Sun, 15 Mar 2015 23:02:32 +0800</pubDate>
      <link>https://ruby-china.org/topics/24665</link>
      <guid>https://ruby-china.org/topics/24665</guid>
    </item>
  </channel>
</rss>
