dannymcc
dannymcc

Reputation: 3814

Rails "moments ago" instead of created date?

Currently I have the following in my helper:

def created_today k 
  if k.created_at.to_date == Date.today then 
    content_tag(:span, 'Today', :class => "todayhighlight") 
  else 
    k.created_at.to_s(:kasecreated) 
  end
end

What I would like to do is replace TODAY with MOMENTS AGO if the created_at time is within the last 10 minutes.

Is this possible?

Would I just add another 'if' line after the current 'if k.created_at.to_date == Date.today then'?

Thanks,

Danny

UPDATE:

def created_today k 
  if k.created_at.to_date == Date.today then 
    content_tag(:span, 'Today', :class => "todayhighlight") 

  if k.created_at > 1.minutes.ago then
    content_tag(:span, 'Moments Ago', :class => "todayhighlight") 

  else 
    k.created_at.to_s(:kasecreated) 
  end
end

Upvotes: 0

Views: 1099

Answers (1)

Sven Koschnicke
Sven Koschnicke

Reputation: 6711

You could do

 if k.created_at > 10.minutes.ago then
   ...
 elsif k.created_at.to_date == Date.today then
   ...
 else
   ...

Note that you have to put the 10-minute-check before the today-check because a date less than 10 minutes ago is most likely on the same day.

Upvotes: 1

Related Questions