Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
31 lines (22 sloc) 635 Bytes
# Why repeat our sanitizing stuff? Extract it out to an ActiveRecord mixin. To use it:
#
# class TestModel < ActiveRecord::Base
# sanitize :title, :description
# end
module Sanitize
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
def sanitize(*attributes)
include ActionView::Helpers::SanitizeHelper
before_save do |object|
attributes.each do |attribute|
self.send("#{attribute}=", strip_tags(send(attribute).try(:strip)))
end
end
end
end
end
# Add Sanitize capabilities to ActiveRecord
ActiveRecord::Base.send :include, Sanitize