2 Replies - 4752 Views - Last Post: 17 October 2012 - 03:57 PM Rate Topic: -----

#1 NotarySojac  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 428
  • Joined: 30-September 10

Rails: Display "posted X days ago" or if a long time ago dd-m

Posted 15 October 2012 - 05:09 PM

Hey, I wanted to tweak some code on a project so that new posts tell you how many days ago they were posted, but for really old posts, it should just give you the day-month-year format everyone in my country is used to.

Here's the code I cranked out, which... well I'm sure I've done it wrong because it looks really yucky and not dry... oh, I'll fix the dry part actually right now. But I still have a feeling there's a more proper way to do this yet I couldn't find it in any of my references.


Here's the NewsPost Model. The last function (present_post_date) is what I call from within the view to present the posted_on date. I'm using paperclip, so there's some stuff in the class that might look a little funky, but that's just something that handles image sizing and uploading for me automatically.
class NewsPost < ActiveRecord::Base
  #scope :desc, order("news_posts.created_at DESC")
  include ActionView::Helpers::DateHelper
  attr_accessible :headline, :body, :author, :posted_on, :headline2, :avatar
  has_attached_file :avatar, :styles => { :medium => "600x600>", :thumb => "100x100>" }
  
  
  def present_headline
    self.headline.bbcode_to_html.html_safe
  end
  
  def present_body
    image_tag = ("<br><img src='" + self.avatar.url(:medium) + "'>").html_safe unless self.avatar.to_s == "/avatars/original/missing.png"
    
    begin
      self.body.bbcode_to_html.html_safe + image_tag
    rescue Exception => e
      Rails.logger.error " "
      Rails.logger.error e
      Rails.logger.error self.body
      Rails.logger.error " "
      "Possible syntax error =/."
    end
    
  end
  
  
  def present_post_date
    time_from_now = distance_of_time_in_words_to_now(self.posted_on)
    format_for_old_posts = "Posted On:  " + self.posted_on.strftime("%m-%d-%Y")
    format_for_fresh_posts = "Posted " + distance_of_time_in_words_to_now(self.posted_on) + " ago"
    
    unless (time_from_now =~ /day|minute|hours|seconds/).nil?
      if time_from_now =~ /day/
        if time_from_now.to_i >= 24
          format_for_old_posts
        else
          format_for_fresh_posts
        end
      else
        format_for_fresh_posts
      end
    else
      format_for_old_posts
    end
  end
  
end



So is that a proper way to handle things or what would you do instead?

Is This A Good Question/Topic? 0
  • +

Replies To: Rails: Display "posted X days ago" or if a long time ago dd-m

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2087
  • View blog
  • Posts: 3,175
  • Joined: 21-June 11

Re: Rails: Display "posted X days ago" or if a long time ago dd-m

Posted 16 October 2012 - 01:56 AM

The comparison can be simplified if you work on the difference between now and then as a number instead of a string:

distance = Time.now - posted_on
if(distance > 24.days)
  format_for_old_posts
else
  format_for_fresh_posts
end


Was This Post Helpful? 2
  • +
  • -

#3 NotarySojac  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 428
  • Joined: 30-September 10

Re: Rails: Display "posted X days ago" or if a long time ago dd-m

Posted 17 October 2012 - 03:57 PM

Much cleaner! Thanks Sepp2k

  def present_post_date
    distance = Date.today - self.posted_on
    
    format_for_old_posts = "Posted On:  " + self.posted_on.strftime("%m-%d-%Y")
    format_for_fresh_posts = "Posted " + distance_of_time_in_words_to_now(self.posted_on) + " ago"
    
    if(distance > 24.days)
      format_for_old_posts
    else
      format_for_fresh_posts
    end
  end


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1