B Seven
B Seven

Reputation: 45943

How to include a specific Rails method (or file) in a non-Rails Ruby project?

I would like to use the distance_of_time_in_words method in the date_helper.rb Rails file (see on Github) in an non-Rails Ruby project.

How can I include it? It requires other files, so how to include them?

I don't want to include all of Rails because that would slow down the development process.

Ruby 1.9.3

Upvotes: 0

Views: 219

Answers (2)

halfelf
halfelf

Reputation: 10107

This method, distance_of_time_in_words is in actionpack/lib/action_view/helpers/date_helper.rb. So you should require 'action_view' and action_view/helpers to load this method. And the method is defined in module ActionView::Helpers::DateHelper, you can include it in your class. The method is an instance method.

require 'action_view'
require 'action_view/helpers'

class Klass
    include ActionView::Helpers::DateHelper
end

c = Klass.new
c.distance_of_time_in_words( ...... )    

Upvotes: 3

Joshua Cheek
Joshua Cheek

Reputation: 31726

If this is the only thing you want from it, then I'd just go take the source code and hack it to remove the dependencies (which appears to just be some I18n translations. To support the hack, you can probably translate this test suite.

Why would I do this instead of using the gem? Because it's just such an enormous dependency. It's so enormous that you actually notice it loading all that code. I'd rather rip out the method and hack it to work than depend on all of that (again, assuming this is the only thing you want from the lib).

Upvotes: 2

Related Questions