Want to use a view helper (TextHelper) in a model class

I have a model that deals with HTML and I want to use the
text_helper.sanitize method to strip the HTML of Javascript.

However, it doesn’t appear that I can get easy access to the text_helper
methods from within a model.

Anyone have any suggestions for how to do this?

In general, I think that there are some ActionView helpers which are
generic enough to want to use in a model class. Anyone have any
comments on that?

Thanks,
Wes

You could include the TextHelper module in your model class to get
access to the methods, but my solution was to extend the String class so
that I could call some of the TextHelpers directly on a string:

cool http://rubyonrails.com”.strip_tags.auto_link
=> “cool <a href="http://rubyonrails.com">http://rubyonrails.com

I posted the implementation of this on my blog a few days ago:

http://www.csummers.org/index.php/2006/08/07/extend-string-to-use-actionviews-text-helpers/

Comments and suggestions are welcome.

Curtis S.

http://www.csummers.org/index.php/2006/08/07/extend-string-to-use-actionviews-text-helpers/

Comments and suggestions are welcome.

If you look at the wrapper methods I’m constructing you’ll notice that I
just copied the default arguments from the TextHelper method definition
to the wrapper definitions. For example:

def highlight(phrase, highlighter = ‘\1’)
TextHelperSingleton.instance.highlight(self, phrase, highlighter)
end

This seems ugly, though, because I’d like not to have to copy the
defaults (DRY) in case they happen to change in TextHelper’s definition.
I tried making my argument defaults nil, but that doesn’t appear to
work.

Any suggestions?

Curtis S.

On 10 Aug 2006, at 06:48, Wes G. wrote:

generic enough to want to use in a model class. Anyone have any
comments on that?

They pertain more to String manipulation than to a model, if you ask
me. Curtis S.’ way of extending the String class is the best way
to do it.

Best regards

Peter De Berdt