Category Archives: Ruby

Introducting Trimify

Quite often users accidentally add redundant white spaces before or after text they enter in web forms. This can lead to an inconsistent database and also complicate, or even worse, break model validation. As such it’s necessary to intercept all textual data and remove these extraneous white spaces before models are validated and form data are persisted. Trimify is a Ruby gem that automates this process.

For example, let’s pretend that we have a Post model that has a title and a body. In order to trim content of title and body before validation, all we have to do is:

By default, trimify converts blank strings to nil. This can be overridden by setting the :nilify option to false:

The current version of Trimify is only compatible with Rails 2.3 but I am planning to release a Rails 3 compatible version soon. To use Trimify simply install it by running gem install trimify. The project is hosted at Github.

Acknowledgments

Trimify is inspired by the nilify plugin written by Tobias Schmidt.