Created
August 20, 2009 01:20
-
-
Save amiel/170755 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment