Reputation: 597
So, I downloaded and installed the ActiveHelper gem, but I still can't figure out how to use ActionView's DateHelper methods, such as time_ago_in_words, in normal Ruby code. Is this not included in ActiveHelper? Is it possible to use these methods outside of Rails?
http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-time_ago_in_words
Upvotes: 24
Views: 17388
Reputation: 10251
Try this:
require 'action_view'
require 'action_view/helpers'
include ActionView::Helpers::DateHelper
time_ago_in_words(Time.now - 60*60*2) + ' ago'
#=> "about 2 hours ago"
If you need to take advantage of Numeric ActiveSupport extensions:
require 'active_support/core_ext/numeric/time'
time_ago_in_words(Time.now - 2.hours) + ' ago'
#=> "about 2 hours ago"
# note that (Time.now - 2.hours) == (2.hours.ago)
Reference for Non Rails App : https://github.com/elgalu/time_ago_in_words#non-rails-apps
Upvotes: 40
Reputation: 27961
I have no idea what ActiveHelper is, but your underlying requirement is to have time_ago_in_words
available in a non-Rails app then:
require 'rubygems'
require 'action_view'
include ActionView::Helpers::DateHelper
time_ago_in_words Time.now - 4 * 86400
# => "4 days"
Upvotes: 6
Reputation: 29124
require 'rubygems'
require 'active_support/core_ext/numeric/time'
require 'action_view'
require 'action_view/helpers'
include ActionView::Helpers::DateHelper
70.minutes.ago
# => 2015-02-23 09:09:04 +0530
time_ago_in_words(3.minutes.from_now)
#=> "3 minutes"
Upvotes: 1