views:

93

answers:

1

I've searched all over web and have not found the answer. I'm trying to have a very standard archive option for my blog based on date. A request to url blog.com/archive/2009 shows all posts in 2009, blog.com/archive/2009/11 shows all posts in November 2009 etc. I found two different of code but not very helpful to me.

def display_by_date
 year = params[:year]
 month = params[:month]
 day = params[:day]
 day = '0'+day if day && day.size == 1
 @day = day
 if (year && month && day)
   render(:template => "blog/#{year}/#{month}/#{date}")
 elsif year
  render(:template => "blog/#{year}/list")
 end
end

def archive
 year = params[:year]
 month = params[:month]
 day = params[:day]
 day = '0'+day if day && day.size == 1
 if (year && month && day)
   @posts_by_month = Blog.find(:all, :conditions => ["year is?", year])
 else
   @posts_by_month = Blog.find(:all).group_by { |post| post.created_at.strftime("%B") }
 end
end

Any help is appreciated.

A: 

Edit: I've managed to get it to work. If you go to 'blog/2010' you see all posts made in 2010, and if you go to 'blog/2010/apr' you see all posts made in 2010 april etc.

def archive
 year = params[:year]
 month = params[:month]
 day = params[:day]
 if (year && month && day)
  requested_date = Date.new(year.to_i, Date.parse(month).month.to_i, day.to_i)
  from = requested_date - 1
  to = requested_date + 1
  @posts_by_month = Blog.find(:all, :conditions => ["due BETWEEN ? AND ?", from, to])
 elsif (year && month)
  requested_month = Date.new(year.to_i, Date.parse(month).month.to_i)
  end_month = requested_month.end_of_month
  @posts_by_month = Blog.find(:all, :conditions => ["due BETWEEN ? AND ?", requested_month, end_month])
 else
  requested_year = Date.new(year.to_i)
  @posts_by_month = Blog.find(:all, :conditions => ["created_at BETWEEN ? AND ?", requested_year, requested_year.end_of_year ])
 end
end

#routes.rb
map.connect 'blog/:year/:month/:day',
:controller => 'blogs',
:action => 'archive',
:year => /\d{4}/,
:month => /\w{3}/,
:day => /\d{2}/,
:day => nil,
:month => nil

I don't know if this is 'good' code, but I'm sure someone can make it even better. I'd appreciate it if someone did.

Senthil
This appears to be another question, not an answer… :)
Jonathan Sterling
Lol you're right. I'm trying to figure it out and I'll update the post when done.
Senthil