require 'redcloth' rescue nil require 'rubypants' rescue nil module HtmlField def self.included(base) base.extend(ClassMethods) base.extend(WhiteListHelper) end module ClassMethods DEFAULTS = { :processors => [:redcloth] } def html_field(source, options={}) options = DEFAULTS.merge(options) options[:destination] = "#{source}_html".to_sym unless options[:destination] before_validation do |record| str = record.send(source.to_sym) unless str.nil? options[:processors].each do |processor| str = if (processor_options = options["#{processor}_options".to_sym]) record.send(processor, str, processor_options) else record.send(processor, str) end end html = white_list(str) record.send("#{options[:destination]}=", html) end end end end def redcloth(str, options=[]) RedCloth.new(str, options).to_html end end