public
Description: Provides a framework for saving incoming blank values as nil in the database in instances where you'd rather use DB NULL than simply a blank string.
Homepage: http://benhughes.name/
Clone URL: git://github.com/railsgarden/nilify_blanks.git
name age message
file .gitignore Thu Sep 10 05:10:24 -0700 2009 Added back test DB. [railsgarden]
file CHANGELOG Thu Sep 10 05:04:30 -0700 2009 Bumped version for recent change. [railsgarden]
file MIT-LICENSE Wed Nov 26 12:02:59 -0800 2008 Initial blank rails plugin. [railsgarden]
file Manifest Thu Sep 10 05:06:19 -0700 2009 Added back Manifest. [railsgarden]
file README.rdoc Thu Jan 15 22:07:12 -0800 2009 Also act as a gem. [railsgarden]
file Rakefile Thu Sep 10 05:04:30 -0700 2009 Bumped version for recent change. [railsgarden]
file init.rb Mon Mar 09 11:57:59 -0700 2009 Fixed issue with gem usage. [railsgarden]
file install.rb Wed Nov 26 12:02:59 -0800 2008 Initial blank rails plugin. [railsgarden]
directory lib/ Wed May 27 19:18:36 -0700 2009 Only activate if table exists. [railsgarden]
file nilify_blanks.gemspec Thu Sep 10 05:04:30 -0700 2009 Bumped version for recent change. [railsgarden]
file nilify_blanks_plugin.sqlite3 Thu Sep 10 05:09:34 -0700 2009 Added in test DB. [railsgarden]
directory tasks/ Wed Nov 26 12:02:59 -0800 2008 Initial blank rails plugin. [railsgarden]
directory test/ Thu Sep 10 05:10:24 -0700 2009 Added back test DB. [railsgarden]
file uninstall.rb Wed Nov 26 12:02:59 -0800 2008 Initial blank rails plugin. [railsgarden]
README.rdoc

Nilify Blanks

In Rails when saving a model from a form and values are not provided by the user, an empty string is recorded to the database instead of a NULL as many would prefer (mixing blanks and NULLs can become confusing). This plugin allows you to specify a list of attributes (or exceptions from all the attributes) that will be converted to nil if they are blank before a model is saved.

Only attributes responding to empty? with a value of true will be converted to nil. Therefore, this does not work with integer fields with the value of 0, for example. Usage is best shown through examples:

Install

  # In environment.rb
  config.gem 'railsgarden-nilify_blanks', :lib => 'nilify_blanks', :source => 'http://gems.github.com'

Basic Examples

  # Checks and converts all fields in the model
  class Post < ActiveRecord::Base
    nilify_blanks
  end

  # Checks and converts only the title and author fields
  class Post < ActiveRecord::Base
    nilify_blanks :only => [:author, :title]
  end

  # Checks and converts all fields except for title and author
  class Post < ActiveRecord::Base
    nilify_blanks :except => [:author, :title]
  end

Specifying a Callback

Checking uses an ActiveRecord before_save filter by default, but you can specify a different filter with the :before option. Any filter will work - just first remove the "before_" prefix from the name.

  class Post < ActiveRecord::Base
    nilify_blanks :before => :create
  end

  class Post < ActiveRecord::Before
    nilify_blanks :before => :validation_on_update
  end