I'm on Rails 3.0. For a model's updated_at
column, I need to output that as a user friendly format in JSON, so instead of seeing this 2011-09-14T22:01:32Z
as the value, I would like to show less than a minute ago
(or whatever user-friendly message I decide) for updated_at
. I wrote a method that returns the output in the user-friendly format and now needs the functionality to be available in views, controller and model. What's the best way to do this so it isn't convoluted or bad practice.
EDIT Per answer from @klochner, here's what my date.rb
looks like but now it throws date.rb:21:in
format': wrong number of arguments (2 for 0) (ArgumentError)`
include ActionView::Helpers::DateHelper
class ActiveSupport::TimeWithZone
def friendly
format
end
end
class Date
def friendly
format
end
end
class Time
def friendly
format
end
end
def format
if self.today?
format_time = ...
elsif (self < Time.now.beginning_of_day) and (self > Time.now.yesterday.beginning_of_day)
format_time = ...
else
format_time = ...
end
end
@klochner suggestion changing format
to format_helper
works great. Thanks!