Created
July 25, 2008 21:04
-
-
Save anotherjesse/2522 to your computer and use it in GitHub Desktop.
add a santize :column_name to sanitize to html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| include ActionView::Helpers::TagHelper, ActionView::Helpers::TextHelper, WhiteListHelper | |
| module ActiveRecord | |
| class Base | |
| def self.sanitize(attr_name, options = {}) | |
| define_method "#{attr_name}=" do |val| | |
| write_attribute attr_name, val | |
| html = '' | |
| val.strip! if val.respond_to?(:strip!) | |
| unless val.blank? | |
| # Turn URLs and e-mails into links | |
| html = auto_link(val) | |
| # Turn newlines into <p> or <br /> | |
| html = unsimple_format(html) | |
| # Escape entities, remove bad tags and attributes/values | |
| html = white_list(html, {}) do |node, bad| | |
| if white_listed_bad_tags.include?(bad) then | |
| nil | |
| else | |
| node.to_s.gsub(/&(?!#?\w+;)/, '&').gsub(/</, '<') | |
| end | |
| end | |
| html = wordwrap(html) | |
| end | |
| write_attribute "#{attr_name}_html", html | |
| end | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment