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

Commit

Permalink
comments in Nillify module
Browse files Browse the repository at this point in the history
  • Loading branch information
cedum committed Sep 22, 2011
1 parent 3e3b4ee commit 208ac8f
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 208ac8f

Please sign in to comment.