Skip to content

Instantly share code, notes, and snippets.

@jonpaul
Forked from workmad3/sanitize_helper.rb
Created August 10, 2011 14:24
Show Gist options
  • Save jonpaul/1136903 to your computer and use it in GitHub Desktop.
Save jonpaul/1136903 to your computer and use it in GitHub Desktop.
Sanitize class macro
class ActiveRecord::Base
def self.sanitize_attribute(*attr_names)
attr_names.each do |attr_name|
define_method attr_name do
self[attr_name].try(:html_safe)
end
define_method "#{attr_name}=" do |attr_value|
self[attr_name] = attr_value.try(:sanitize)
end
end
end
end
class String
def sanitize()
Sanitize.clean(self, Sanitize::Config::RELAXED).strip.html_safe
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment