Create a date-based archive from a model
All the functions it needs to run are included. You might already have a 'delta' and 'find_all_by_date' function some where in your code. You could easily use those instead.Customize the 'find_all_by_date' function to include, say, only published posts.
Add this to your model.
def self.find_all_by_date(year = nil, month = nil, day = nil) if year != nil from, to = delta(year, month, day) find(:all, :conditions => [ %{ created_at BETWEEN ? AND ? }, from, to], :order => 'created_at DESC') else find(:all, :order => 'created_at DESC') end end def self.list_years() list_date_part(:year) end def self.list_months(year) list_date_part(:month, year) end def self.list_days(year, month) list_date_part(:day, year, month) end def self.list_date_part(part, *fixed_parts) find_all_by_date(*fixed_parts).map{ |record| record.created_at.send(part) }.uniq end def self.delta(year, month = nil, day = nil) from = Time.mktime(year, month || 1, day || 1) to = from + 1.year to = from + 1.month unless month.blank? to = from + 1.day unless day.blank? to = to.tomorrow unless month.blank? or day return [from.midnight, to.midnight] end