Include helpers in lib classes

21 views
Skip to first unread message

Cyrille

unread,
Oct 22, 2007, 7:33:12 PM10/22/07
to WellRailed
Hi there,

Good long weekend everyone?

I've got a general question: what do you guys put into /lib? More
specifically: modules or classes?

I use classes, with class methods (is that even the right term for
Ruby?), such as:

class TimeUtils
def self.time_from_now

end
end

My problem is that I would like to include a TextHelper method
(pluralize in this case) and I can't figure out how to do that. I've
tried:

require File.dirname(__FILE__) + '/../vendor/rails/actionpack/lib/
action_view/helpers/text_helper'

class TimeUtils
def self.time_from_now
pluralize(1, "minute")
end
end

But I get:

NoMethodError: undefined method `pluralize' for TimeUtils:Class

Is there a way to include TextHelper?

Cheers

Cyrille

j.fa...@gmail.com

unread,
Oct 22, 2007, 8:32:43 PM10/22/07
to WellRailed
Hi! I remember trying to use pluralize in places like controllers and
when I read up about it people generally liked to use other methods
such as Inflector#pluralize. It's not as nice to use though.

I've haven't personally seen anyone use helpers outside of views...

Jean-François Trân

unread,
Oct 22, 2007, 8:39:13 PM10/22/07
to WellR...@googlegroups.com
Cyrille :

> My problem is that I would like to include a TextHelper method
> (pluralize in this case) and I can't figure out how to do that. I've
> tried:
>
> require File.dirname(__FILE__) + '/../vendor/rails/actionpack/lib/
> action_view/helpers/text_helper'
>
> class TimeUtils
> def self.time_from_now
> pluralize(1, "minute")
> end
> end
>
> But I get:
>
> NoMethodError: undefined method `pluralize' for TimeUtils:Class
>
> Is there a way to include TextHelper?

Hi,

Try :

# don't put the require line

class TimeUtils
extend ActionView::Helpers::TextHelper

# ...
end

-- Jean-François.

Cyrille

unread,
Oct 22, 2007, 8:43:03 PM10/22/07
to WellRailed
Hi there,

yeah, "extend" works. Thanks for that!

Cheers

Cyrille

On Oct 23, 1:39 pm, "Jean-François Trân" <jft...@rubyfrance.org>
wrote:

Reply all
Reply to author
Forward
0 new messages