Created
August 13, 2008 22:22
-
-
Save adelcambre/5322 to your computer and use it in GitHub Desktop.
This file contains 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
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