Skip to content

Instantly share code, notes, and snippets.

@adelcambre
Created August 13, 2008 22:22
Show Gist options
  • Save adelcambre/5322 to your computer and use it in GitHub Desktop.
Save adelcambre/5322 to your computer and use it in GitHub Desktop.
def get_archive_hash
counts = repository.adapter.query("SELECT COUNT(*) as count, #{specific_date_function} FROM articles WHERE published_at IS NOT NULL AND published = ? GROUP BY year, month ORDER BY year DESC, month DESC", true)
archives = counts.map do |entry|
{
:name => "#{Date::MONTHNAMES[entry.month.to_i]} #{entry.year}",
:month => entry.month.to_i,
:year => entry.year.to_i,
:article_count => entry.count
}
end
archives
end
private
def specific_date_function
if Merb::Orms::DataMapper.config[:adapter] == "sqlite3"
"strftime('%Y', published_at) as year, strftime('%m', published_at) as month"
else
"extract(year from published_at) as year, extract(month from published_at) as month"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment