Skip to content
This repository has been archived by the owner on Apr 3, 2023. It is now read-only.

Commit

Permalink
Merge pull request #2 from awstin/master
Browse files Browse the repository at this point in the history
comments in Nillify module
  • Loading branch information
tilsammans committed Sep 29, 2011
2 parents 3e3b4ee + 208ac8f commit 8c7d599
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/nilly_vanilly/nillify.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
module NillyVanilly
# Inject the nillifcation process in ActiveRecord
module Nillify
def self.included(base)
base.extend ClassMethods
end

module ClassMethods

# Register a before_save hook to nillify attributes defined by nillify_attributes accessor
def nillify(*attributes)
class_eval do
before_save :nillification
end

cattr_accessor :nillify_attributes
self.nillify_attributes = attributes
self.nillify_attributes = attributes

include InstanceMethods
end
end

module InstanceMethods

# Nillify attribute if it is a empty string
def nillification
for attribute in self.class.nillify_attributes
self.send("#{attribute}=", nil) if self.send(attribute) == ""
Expand Down

0 comments on commit 8c7d599

Please sign in to comment.