Skip to content

Instantly share code, notes, and snippets.

@anotherjesse
Created July 29, 2008 21:20
Show Gist options
  • Save anotherjesse/3178 to your computer and use it in GitHub Desktop.
Save anotherjesse/3178 to your computer and use it in GitHub Desktop.
module ActiveRecord
class Base
# FIXME - WE SHOULD NOT PUT THESE INSIDE AR:B
include ActionView::Helpers::TagHelper, ActionView::Helpers::TextHelper, WhiteListHelper
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+;)/, '&amp;').gsub(/</, '&lt;')
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