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

Stuart Smith stuart.smith at smsconsulting.wanadoo.co.uk
Tue Apr 12 15:10:26 EDT 2005


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 -%>













More information about the Typo-list mailing list