Dscn3240_normalamiel

http://twitter.com/amiel

Paginate by day

require_dependency 'will_paginate'
require_dependency 'will_paginate/finder'
 
 
unless Time.instance_methods.include? 'at_end_of_day'
Time.class_eval do
def at_end_of_day
self.at_beginning_of_day + 1.day
end
end
end
 
module AddPaginationByDayToWillPaginate
# warning, you cannot pass this conditions, it has to be simple
def paginate_by_day *args, &block
options = args.pop
page = options[:page].to_i || 1
total_entries = options[:total_entries]
 
day = (page-1).days.ago
 
WillPaginate::Collection.create(page, 1) do |pager|
count_options = options.except :page, :total_entries, :finder
find_options = count_options.except(:count).update(:conditions => [ "#{table_name}.created_at BETWEEN ? AND ? ", day.at_beginning_of_day, day.at_end_of_day ])
 
args << find_options
 
pager.replace send(:all, *args, &block)
 
# magic counting for user convenience:
pager.total_entries = 365 # user can go back one year?
end
 
end
end
 
ActiveRecord::Base.extend AddPaginationByDayToWillPaginate
Reveal More
Added over 2 years ago
Post Code