No commit activity in last 3 years
No release in over 3 years
This is a tiny gem that sanitizes html when a record is saved.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.11
~> 10.0

Runtime

 Project Readme

SanitizeHtmlFields

This is a tiny gem that sanitizes html when an instance of ActiveRecord is saved.

The gem automatically picks up all String columns of a model that have a corresponding $name_html column and sanitizes them using the standard Rails ActionView::Helpers::SanitizeHelper#sanitize method.

Installation

Add this line to your application's Gemfile:

gem 'sanitize_html_fields'

And then execute:

$ bundle

Or install it yourself as:

$ gem install sanitize_html_fields

Usage

Just add sanitize_html_fields to one of your ActiveRecord model classes and you're ready to go!

class MyModel < ActiveRecord::Base
  sanitize_html_fields
end

From now on SanitizeHtmlFields will automatically sanitize all of the model's String columns that have a corresponding $name_html column.

Let's say your model has a 'text' column whose contents you'd like to be sanitized. In this case you need another String column named text_html.

Options

You can pass an options hash to sanitize_html_fields which is then used for the individual calls to ActionView::Helpers::SanitizeHelper#sanitize.

With an option hash you can for example allow certain tags or attributes using the tags and attributes key. See the documentation of ActionView::Helpers::SanitizeHelper#sanitize for more details.

You can also use the convert_newlines key in the options hash to configure whether or not to convert line breaks to <br> tags. The default value for this option is false.

The remove_excessive_whitespaces key is used to control if excessive whitespace characters (multiple spaces, linebreaks, tabs or newlines) should be removed. The default value for this option is true.

License

The gem is available as open source under the terms of the MIT License.