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?

New Topic/Question
Reply




MultiQuote



|