StripAttributes

StripAttributes is a Rails plugin that automatically strips all ActiveRecord model attributes of leading and trailing whitespace before validation. If the attribute is blank, it strips the value to nil.

It works by adding a before_validation hook to the record. By default, all attributes are stripped of whitespace, but :only and :except options can be used to limit which attributes are stripped. Both options accept a single attribute (:only => :field) or arrays of attributes (:except => [:field1, :field2, :field3]).

Changes from the original StripAttributes

Defaults to all models get stripping, and options per-model can be overrriden. Inspired by the xss_terminate style.

Examples

# all attributes will be stripped (default)
class DrunkPokerPlayer < ActiveRecord::Base
end

# all attributes will be stripped except :boxers
class SoberPokerPlayer < ActiveRecord::Base
  strip_attributes! :except => :boxers
end

# only :shoe, :sock, and :glove will be stripped
class ConservativePokerPlayer < ActiveRecord::Base
  strip_attributes! :only => [:shoe, :sock, :glove]
end

Installation

gem 'strip_attributes', :git => 'http://github.com/joenoon/strip_attributes.git'

Credits

StripAttributes source is hosted on GitHub: github.com/joenoon/strip_attributes

Original Credits:

The idea was triggered by the information at wiki.rubyonrails.org/rails/pages/HowToStripWhitespaceFromModelFields but was modified from the original to include more idiomatic ruby and rails support.

Original StripAttributes source is hosted on GitHub: github.com/rmm5t/strip_attributes

License

Copyright © 2009-2010 Joe Noon released under the MIT license Copyright © 2007-2008 Ryan McGeary released under the MIT license en.wikipedia.org/wiki/MIT_License