[typo] ticket #13 - add Archive articles by month (I hope this is what it means!)

Ioan Bizau jr. ib at sdf-eu.org
Tue Apr 12 16:03:27 EDT 2005


There is a problem here. You used date_format, which is database 
dependent. AFAIK this thing is done using to_char in PostgreSQL and 
Oracle and cast in MSSQL.
I also wanted to fix this issue, but I got stuck here. From what I saw 
RoR doesn't offer us a solution. Maybe somebody else has an idea what to do?

Stuart Smith wrote:
> I have written some code to fix this ticket; how do I get the code
> reviewed? How do I get the code checked in once the changes have been
> agreed?
> 
> This is my first attempt at using ruby and my first attempt at RoR so
> please forgive me if the code is a bit 'amateurish'. 
> 
> Any comments would be most welcome.
> 
> Thanks
> 
> I have included my changes below:
> 
> app/controllers/articles_controller.rb
> 
> require "parsedate"
> include ParseDate
>   @@yr, @@mth, @@dy = nil
> 
>   def archive
>     # date is passed in as Mth YYYY e.g. January 2005
>     # using @@yr @@mth @@dy as class variables so hold values over
> multiple calls so I don't have
>     # to rewrite the index method to handle year and month of archive
> (it will always be set on 1st call)
>     if @params["date"]
>       @archivedate = '01 ' + @params["date"]
>       @@yr, @@mth, @@dy = parsedate(@archivedate)  # might be more
> efficient way to parse the date!
>     end
>     @pages = Paginator.new self, Article.count_by_date(@@yr, @@mth), 10,
> @params['page']
>     @articles = Article.find_all_by_date(@@yr, @@mth, nil,
> @pages.current.to_sql)
>     render_action "index"
>   end
> 
> 
> 
> app/helpers/application_helper.rb
> 
>   def archivelist()
>     archives = Archive.find_dates()
>     render_partial("shared/archives", archives)
>   end
> 
> 
> app/models/archive.rb
> 
> class Archive < ActiveRecord::Base
> 
>   has_many :articles
> 
>   def self.find_dates()
>     @archives=Article.find_by_sql(["SELECT distinct date_format
> (created_at, '%%M %%Y') date from articles ORDER by created_at "])
>   end
> 
> end
> 
> 
> app/models/article.rb
> 
>   belongs_to :archive
> 
> 
> app/views/layouts/articles.rhtml
> 
>   <%= archivelist %>
>   <br/>
> 
> 
> app/views/shared/_archives.rhtml
> 
> <b>Archives</b><br/>
> <% for archive in archives -%>
> <%= link_to
> archive.date, :controller=>"articles", :action=>"archive", :date=>archive.date %><br/>
> <% end -%>
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> _______________________________________________
> Typo-list mailing list
> Typo-list at rubyforge.org
> http://rubyforge.org/mailman/listinfo/typo-list
> 
> 


More information about the Typo-list mailing list