This repository is private.
All pages are served over SSL and all pushing and pulling is done over SSH.
No one may fork, clone, or view it unless they are added as a member.
Every repository with this icon (
) is private.
Every repository with this icon (
This repository is public.
Anyone may fork, clone, or view it.
Every repository with this icon (
) is public.
Every repository with this icon (
| name | age | message | |
|---|---|---|---|
| |
MIT-LICENSE | Fri Jul 03 11:43:21 -0700 2009 | |
| |
README.textile | Tue Sep 15 11:33:42 -0700 2009 | |
| |
Rakefile | Fri Jul 03 23:46:25 -0700 2009 | |
| |
install.rb | Fri Jul 03 23:46:25 -0700 2009 | |
| |
lib/ | Fri Jul 03 23:46:25 -0700 2009 | |
| |
rails/ | Fri Jul 03 23:46:25 -0700 2009 | |
| |
spec/ | Fri Jul 03 23:46:25 -0700 2009 | |
| |
uninstall.rb | Fri Jul 03 23:46:25 -0700 2009 | |
| |
version.yml | Fri Jul 03 23:46:25 -0700 2009 |
README.textile
Attribute Normalizer
I like to keep my Active Record models as strict as possible but I also like the further layer of protection/restriction setting database columns to not allow NULL adds. Normalizing to nil helps enforce this better by not letting ’’ slip through the cracks and I can still prevent those who insist on direct DB access from entering in shitty data as much as possible.
Install as a Ruby gem
TODO: gem-er-a-size this!Install as a Ruby on Rails Plugin
The traditional way.
./script/plugin install git://github.com/mdeering/attribute_normalizer.gitor the old-school but still c00l way!
piston import git://github.com/mdeering/attribute_normalizer.git vendor/plugins/attribute_normalizeror for all you hip gitsters.
git submodule add git://github.com/mdeering/attribute_normalizer.git vendor/plugins/attribute_normalizer git submodule initUsage
This is eager loaded into Active Record.
class Klass < ActiveRecord::Base
# Can take an array of attributes if you want
normalize_attributes :first_name, :last_name
normalize_attributes :home_phone_number, :office_phone_number_ do |value|
return nil unless value.is_a?(String)
value.gsub(/\W/, '').gsub(/^1/, '')
end
end
object = Klass.new
# Blank normalizes to nil
object.first_name = ''
object.first_name # => nil
# Whitespace is cleaned up
object.last_name = "\tDeering\n"
object.last_name # => 'Deering'
# Your given block will be executed to normalize
object.home_phone_number = '+1 (555) 123.4567'
object.home_phone_number # => '5551234567'
Credits
Original module code and concept was taken from Dan Kubb during a project we worked on together. I found that I was consistently using this across all my projects so I wanted to plugin-er-size and gem this up for easy reuse.







