Created
November 7, 2012 12:05
-
-
Save flada-auxv/4031106 to your computer and use it in GitHub Desktop.
月単位でのページネーション
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
<!-- beginning_of_monthはyyyy-mm-ddで取得するが、 | |
ddが入ってるのがクエリで見えてしまうとおかしいし不要な情報でもあるので加工してる。--> | |
<% Work.all.map {|w| w.date.beginning_of_month}.uniq.sort.each do |m| -%> | |
<%= link_to_unless_current m.month, month: m.to_s.sub!(%r[\-\d{2}$],"") %> | |
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
class Work < ActiveRecord::Base | |
# その月のレコードだけを取得する | |
# Stringの形式はyyyy-mm | |
# Time.parseはyyyy-mm-ddを与えてやる必要がある | |
# Time#all_monthは月初めから終わりのrangeオブジェクトを返す | |
scope :by_month, lambda {|arg| | |
arg = Time.parse(arg << "-01") if arg.instance_of?(String) | |
where(date: arg.all_month).order("date") | |
} | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
class WorksController < ApplicationController | |
# GET /works | |
def index | |
@works = @user.works.by_month(params[:month] || Time.now) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment