Preformatter
Sometimes you have to create some naggy before_validation methods to format strings the way you need to. I find this to be a cleaner way to do it and less painful to maintain.
The gem helps you by deleting spaces, special characters or replacing accents in the fields you need.
For now the gem is usable only in Rails 2.3.x projects, in the near future it will be updated for Rails 3.
This gem was built with code from the EMR Bear project PrescriptionRex
Install
gem install preformatter
In the environment.rb you must configure the gem as following:
config.gem 'preformatter'
Usage
All the code below goes directly into the model you want to use the preformatting, the formatting will be done before validation.
Deleting white spaces
no_space_in :field1, :field2, :field3
If the user input is for instance ‘some words with spaces’ it will be transformed into ‘somewordswithspaces’
Replacing accents
no_accents_in :field1, :field2, :field3
If the user input is ‘Sómé wórds wíth áccénts’ it will be transformed into ‘Some words with accents’
Replacing special characters
This method works a little different becouse you have to provide a string with all the special characters you dont want in an attribute.
no_special_characters '(){}-', :in => [:phone, :fax, :id]
If the user input is ‘(81)-83987832 {345}’ it will be transformed into ‘8183987832’
Copyright
Copyright © 2010 Innku, PrescriptionRex. See LICENSE for details.