Sha256: f71309731fdce2b07c82d824ee65c5959a3204e7cbb036d64dcfb993b2f8a1bc

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 KB

Contents

== 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

  Include the gem using bundler in your Gemfile:

    gem "nilify_blanks"

== 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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nilify_blanks-1.0.0 README.rdoc