Topic: Help me clean up a date field

I have a confusing method that I'm trying to cleanup.  The the database I have the date stored in a format like this 2007-03-19.  I want to display the articles Month, Day, and Year stacked on top of each other in the view.  This is what I have at this point, and my article methods seem a bit inefficient.

Here's my controller

  
def index
    @months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
    @articles = Article.find(:all, :conditions => "is_current = 1 ", :order => "date desc")
end

Here's my model

class Article < ActiveRecord::Base
  
  def month_number_to_words
    month_number = (self.date.to_s.split(/-/).values_at(1).to_s.to_i) - 1
    if month_number == 10 
      then month_number
      else month_number.to_s.gsub(/0*/,"").to_i 
    end
  end
  
  def day_of_the_month
    self.date.to_s.split(/-/).slice(2)
  end
  
  def year_written
    self.date.to_s.split(/-/).values_at(0)
  end      
end

And finally my view...

<div id="content_body">
<h3>Current Articles</h3>

<% @articles.each do |article| %>
<div class="article_summary">
	<div class="date">
		<span class="month"><%= @months.slice(article.month_number_to_words) %></span>
		<span class="day"><%= article.day_of_the_month %></span>
		<span class="year"><%= article.year_written %></span>
	</div>
	<p>
	<span class="title"><%= link_to article.headline, :action => "show", :id => article.id -%></span>
		<%= truncate(article.body, 250) %>
		<%= link_to "Read More", :action => "show", :id => article.id %>
	</p>
</div>
<% end %>
</div>

It all works, but is efficient and has way to many conversions from strings, integers, and arrays. Plus, the methods are not very understandable.  Any input is greatly appreciated.

Thanks,
Zack

Last edited by zreed20 (2007-10-31 08:35:49)

But they that wait upon the Lord shall renew their strength

Re: Help me clean up a date field

Why not just use strftime? http://www.ruby-doc.org/docs/Programmin … e.strftime

<%= article.date.strftime("%b %d, %Y") %>
#=> October 31, 2007

Or even better, use #to_formatted_s:

<%= article.date.to_formatted_s(:long) %>

Create your own date formats by putting this in your environment.rb:

ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.merge!(:my_date_format_ => '%b %d, %Y')
Ryan Heneise  |  Art of Mission  |  Now with extra-strong Donor Tools mojo

Re: Help me clean up a date field

You can also get date information out of the date object...

@article.date.year #=> "2007"
@article.date.day #=> "31"
@article.date.month #=> "Oct"

time_ago_in_words(@article.date) #=> "2 days ago"
Ryan Heneise  |  Art of Mission  |  Now with extra-strong Donor Tools mojo

Re: Help me clean up a date field

You can also assign the Time Object, as you would the Date Object, like so:

Time::DATE_FORMATS[:clean]            = "%B %d, %Y"
Time::DATE_FORMATS[:clean_with_time]  = "%B %d, %Y at %I:%M %p"

I tend to keep a file called time_declarations.rb in config/initializers/ folder.

Then I can do:

@article.created_at.to_s(:clean)

Also, it's best *not* to put these things in your environment.rb file, but rather in a self-created *.rb document in config/initializers/ (example: config/initializers/time_declarations.rb). Rails will automatically load anything in the initializers folder. (this is rails 1.2.3 and up)

This helps keeps things clean and easy to maintain.

Re: Help me clean up a date field

Wow! Thanks to both of you, my method seemed way to complicated for what I was trying to do.  strftime kept returning the number for the month rather than the name of the month. So, on a date in my database like this 2007-10-30.

@article.date.year #=> "2007"
@article.date.day #=> "30"
@article.date.month #=> "10"

I'd like to show Oct, instead of 10 for the month of October, and the month method or strftime("%b") doesn't seem to return that for me in this case.

Thanks for the heads up on putting the Time formatting in the initializers, I've always put that kind of stuff in a helper.

Zack

Last edited by zreed20 (2007-11-01 03:44:54)

But they that wait upon the Lord shall renew their strength

Re: Help me clean up a date field

Robert Evans wrote:

Also, it's best *not* to put these things in your environment.rb file, but rather in a self-created *.rb document in config/initializers/ (example: config/initializers/time_declarations.rb). Rails will automatically load anything in the initializers folder. (this is rails 1.2.3 and up)

This helps keeps things clean and easy to maintain.

Good call Robert - i was citing the *old* way of doing things - putting these settings in your initializers is the clean Rails 2.0 way.

Ryan Heneise  |  Art of Mission  |  Now with extra-strong Donor Tools mojo