gist: 2522 Download_button fork
public
Description:
add a santize :column_name to sanitize to html
Public Clone URL: git://gist.github.com/2522.git
Ruby
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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

Owner

anotherjesse

Revisions