Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Adds the ability to normalize and format attributes cleanly with code blocks
Ruby
Branch: master
Pull request Compare This branch is 4 commits ahead, 121 commits behind mdeering:master.

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
rails
spec
.gitignore
MIT-LICENSE
README.textile
ROADMAP.textile
Rakefile
attribute_normalizer.gemspec
install.rb
install.txt
uninstall.rb
version.yml

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

The attribute_normalizer gem is hosted over at Gemcutter

Setup Gemcutter as a gem source if you have not already.

It’s fairly simple to set up Gemcutter. Before we start, however, it’s worth making sure that we’ve upgraded to the latest version of RubyGems, which can be done by running

sudo gem update --system

From the command line. Once RubyGems has updated itself we can then install the Gemcutter gem with

sudo gem install gemcutter

After Gemcutter has installed we’ll need to update our gem sources to include gemcutter.org. To do this we run gem tumble.

$ gem tumble
Thanks for using Gemcutter!
Your gem sources are now:
- http://gemcutter.org
- http://gems.rubyforge.org/
- http://gems.github.com

Install the Attribute Normalizer gem

sudo gem install attribute_normalizer

Install as a Ruby on Rails Plugin

The traditional way.

./script/plugin install git://github.com/mdeering/attribute_normalizer.git

or the old-school but still c00l way!

piston import git://github.com/mdeering/attribute_normalizer.git vendor/plugins/attribute_normalizer

or for all you hip gitsters.

git submodule add git://github.com/mdeering/attribute_normalizer.git vendor/plugins/attribute_normalizer git submodule init

Usage

This is eager loaded into Active Record. It is usable inside of other ruby classes outside of ActiveRecord by just including the module AttributeNormalizer.


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|
    value.is_a?(String) ? value.gsub(/\W/, '').gsub(/^1/, '') : nil
  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.

Copyright

Copyright © 2009 Michael Deering See MIT-LICENSE for details.

Something went wrong with that request. Please try again.