I made a new pagination library for the NamedScope era. You can use will_paginate for ordinary case. It must be better than the young PaginationScope library. But if you want to paginate with complicated chained named_scopes, this is suitable.

Usage

At first, include the module into your model class.

   1  class Post
   2    include PaginationScope

By this, the scope named "paginate" is made.

And then, in the controller of which you want to use pagination, you can get the scope for pagination like this.

   1  class PostsController < ApplicationController
   2    def index
   3      @posts = Post.not_deleted.paginate(params[:page], 10)

Finally, the pagination links are generated by calling the view helper, like this.

   1  <%= paginate @posts %>

Have fun!

posted by Png takiuchi on Sat 16 Aug 2008 at 23:58

named_scopeによるPagination を行うためのGemを作りました。 通常はwill_paginateを利用するほうがいいと思いますが、 :joinsを含む複雑なnamed_scopeを介してpaginationを行いたい場合には、 利用すると便利かもしれません。

pagination_scope

使い方

まず、モデルクラスの中でincludeします。

   1  class Post
   2    include PaginationScope
   3  end

これによって、Postクラスにpaginateというnamed_scopeが作成されます。

続いて、Paginationを利用したいコントローラのアクションメソッド内で、

   1  class PostsController < ApplicationController
   2    def index
   3      @posts = Post.not_deleted.paginate(params[:page], 10)

のようにScopeを取得します。

続いて、Viewで以下のようにPagination用HTMLを生成します。

   1  <%= paginate @posts %>

paginateメソッドは PaginationScope によって導入されるViewヘルパーです。

posted by Png genki on Sat 16 Aug 2008 at 23:25

Railsアプリケーションでpaginationといえば、 will_paginate等のプラグインやGemを使うのが一般的だと思います。 しかし、named_scopeでjoinsを使った場合にうまく paginationができなかったので、 named_scopeだけを使ってpaginationする方法を考えてみました。

まずは以下のようなnamed_scopeを作ります。 以下の例はPostクラスで宣言される事を想定しています。

   1  named_scope :paginate, proc{|page, per_page|
   2    {:offset => per_page*((page || 1).to_i - 1),
   3     :limit => per_page}} do
   4      def count
   5        proxy_scope.count(:group => 'posts.id').size
   6      end
   7      def num_pages
   8        (count.to_f/proxy_options[:limit]).ceil
   9      end
  10      def page
  11        proxy_options[:offset]/proxy_options[:limit] + 1
  12      end
  13      def pages(window = 5, left = 2, right = 2)
  14        (1..num_pages).inject([]) do |result, i|
  15          i <= left || (num_pages - i) < right ||
  16            (i-page).abs < window ? result << i :
  17              (result.last.nil? ? result : result << nil)
  18        end
  19      end
  20    end

countを再定義しているのは、:joinsを含む別なnamed_scopeをチェーンした時に、正しいcountを求めるためです。

コントローラでは、以下のようにScopeを取得します。

   1  @posts = Post.paginate(params[:page], 5)

Viewでは以下のように記述します。

   1  <% if @posts.page > 1 %>
   2    <%= link_to '&laquo; Newer',
   3        url_for(:page => @posts.page - 1) %>
   4  <% else %>
   5    &laquo; Newer
   6  <% end %>
   7  <% @posts.pages.each do |i| %>
   8    <% if i.nil? %>
   9      ...
  10    <% elsif i == @posts.page %>
  11      <%= i %>
  12    <% else %>
  13      <%= link_to i, url_for(:page => i) %>
  14    <% end %>
  15  <% end %>
  16  <% if @posts.page < @posts.num_pages %>
  17    <%= link_to 'Older &raquo;',
  18        url_for(:page => @posts.page + 1) %>
  19  <% else %>
  20    Older &raquo;
  21  <% end %>

posted by Png genki on Sat 16 Aug 2008 at 21:10

今回はブログ左上に表示されるアイコンの設定の仕方について説明します。

ログインをしていただき、右上にあるsettingsをクリックします。

mihon1.PNG
Accountの画面の左側にある灰色の長方形の下にある「Upload」をクリックします。
「blog.s21g.comでアップロードするファイルを選択します」というウインドウが表示されるので、アイコンにしたい画像を選択します。
わずかな時間の後で、画像が表示されます。

これでアイコンが表示されるようになります。

posted by Png mari on Thu 14 Aug 2008 at 02:05
Contents
PaginationScope: The Pagination Library of the NamedScope Era
pagination_scope
named_scopeによるpagination
s21gブログの使い方(5) ~アイコンの設定の仕方~
Comments
瀧内元気: MacOS版は以下にあります * [genki/ViMouse](https://githu... '23-1
KingofSmack: Here also good reads for this mobile applicatio... '14-5
Spencer: You don't have to re-compile it, this version w... '14-4
staiano: Any chance we can get a recompile for 10.9? '14-1
dsjf: https://gist.github.com/6bf1bf2c3cbb5eb6e7a7 これ... '13-1
Services from s21g
twpro(ツイプロ)
Twitterプロフィールを快適検索
地価2009
土地の値段を調べてみよう
MyRestaurant
自分だけのレストラン手帳
Formula
ブログに数式を埋め込める数式コミュニティ