Wes Gamble
2006-Aug-10 04:54 UTC
[Rails] 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 -- Posted via http://www.ruby-forum.com/.
Curtis Summers
2006-Aug-10 21:12 UTC
[Rails] Re: Want to use a view helper (TextHelper) in a model class
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:>> "<b>cool</b> http://rubyonrails.com".strip_tags.auto_link=> "cool <a href=\"http://rubyonrails.com\">http://rubyonrails.com</a>" 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 Summers -- Posted via http://www.ruby-forum.com/.
Curtis Summers
2006-Aug-11 17:11 UTC
[Rails] Re: Want to use a view helper (TextHelper) in a model class
>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 = ''<strong class="highlight">\1</strong>'') 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 Summers -- Posted via http://www.ruby-forum.com/.
Peter De Berdt
2006-Aug-13 13:32 UTC
[Rails] Want to use a view helper (TextHelper) in a model class
On 10 Aug 2006, at 06:48, Wes Gamble wrote:> 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?They pertain more to String manipulation than to a model, if you ask me. Curtis Summers'' way of extending the String class is the best way to do it. Best regards Peter De Berdt -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060813/e0332107/attachment-0001.html