Skip to content

Instantly share code, notes, and snippets.

@amiel
Created August 20, 2009 01:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amiel/170755 to your computer and use it in GitHub Desktop.
Save amiel/170755 to your computer and use it in GitHub Desktop.
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