How to Use the Rails Helper "Distance_Of_Time_In_Words" in Plain Old Ruby (Non-Rails)

How can I use the rails helper distance_of_time_in_words in plain old ruby (non-rails)

This should work:

require 'action_view'

include ActionView::Helpers::DateHelper

Both of these need to be done for a couple of reasons. First, you need to require the library, so that its modules and methods are available to be called. This is why you need to do require 'action_view'.

Second, since distance_of_time_in_words is module, which does not stand on its own, it needs to be included in class. You can then access it by calling distance_of_time_in_words on an instance of that class.

When you are in the console, you already have an instance of the Object class running. You can verify this by calling self in the irb console. When you call include ActionView::Helpers::DateHelper, you are including those methods for any instance of the Object class. Since that is the implicit receiver of the irb console, you can then just save distance_of_time_in_words right on the console and get what you want!

Hope that helps.

Joe

distance_of_time_in_words and then remove the word about

distance_of_time_in_words(Time.now, real_time + 0.seconds, true).gsub('about ','')

look here for more information

try a helper (put this code in app/helpers/application_helper.rb)

def remove_unwanted_words string
bad_words = ["less than", "about"]

bad_words.each do |bad|
string.gsub!(bad + " ", '')
end

return string
end

in bad words you can define strings which you want to have removed from that string.
user it like this:

<%= remove_unwanted_words distance_of_time_in_words(Time.now, real_time + 0.seconds, true) %>

How come helper.distance_of_time_in_words_to_now Time.now works in development but not production

I figured out the problem. i18n gem version 0.6.10 was yanked, but I was using that in my system. As soon as I rolled back to 0.6.9 everything worked fine.

Include distance_of_time_in_words value in JSON output

Add the method posted_on to Post model

class Post < ActiveRecord::Base
include ActionView::Helpers::DateHelper

def posted_on
distance_of_time_in_words(created_at, Time.now)
end
end

In your controller, pass the methods option to to_json

def index
@user = current_user
@posts = Post.all
end
respond_to do |format|
format.html
format.json {
render json: @posts.to_json(:include => {
:user => { :only => [:first_name, :last_name]},
:category => { :only => [:name, :id]}
}, :methods => :posted_on),
:callback => params[:callback]
}
end
end

Rails distance_of_time_in_words_to_now options

If you check out the source of this method (http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-distance_of_time_in_words) you will see it uses localization. The key for days is :x_days, so in your en.yml file, you can override that, using :count, like the following:

en:
x_days: "in %{count} days"

Now for "tomorrow", I would create a helper that either returns your custom responses or fallsback to distance_of_time_in_words, like so:

def custom_distance_of_time_in_words_to_now(time)
return "tomorrow" if time.tomorrow? #however you decide to check
distance_of_time_in_words_to_now(time)
end

My custom distance_of_time_in_words?

Have a look at the implementation of distance_of_time_in_words in the Rails codebase, that will be a good place to start.

https://github.com/rails/rails/blob/master/actionpack/lib/action_view/helpers/date_helper.rb

Rails distance_of_time_in_words with direction

According to the docs there isn't.

But, this is trivial enough that you can wrap it in a simple method:

def distance_of_time(time)
if time > Time.now
"in #{distance_of_time_in_words_to_now time}"
else
"#{distance_of_time_in_words_to_now time} ago"
end
end

Working with distance_of_time_in_words in Rails 3

The distance_of_time_in_words method is an ActionView Helper, thus needs to be called from the View (not the controller).

http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html



Related Topics



Leave a reply



Submit