Trimify

Trimify is an ActiveRecord Plugin that allows you to easily trim leading and trailing white spaces from string attributes and optionally turn blank strings to nil. Only attributes you specify will be checked and converted. Using trimify on non-string attributes has no effect.

Installation

gem install trimify

Example

class User < ActiveRecord::Base
  trimify :first_name, :middle_name       
  trimify :last_name, :nilify => false    # default value of :nilify is true
end

user = User.create(:first_name => " Shompet ", :middle_name = "", :last_name => "")
user.first_name   # => "Shompet"
user.middle_name  # => nil
user.last_name    # => ""

Acknowledgments

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

Copyright © 2011 Behrang Saeedzadeh, released under the MIT license.