Sha256: c5df2d269adf474b3a0a8110078ed56d95b4f50ef4bf6e6b557d2e5f25f73033

Contents?: true

Size: 796 Bytes

Versions: 3

Compression:

Stored size: 796 Bytes

Contents

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

        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) == ""
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
nilly_vanilly-2.0.0 lib/nilly_vanilly/nillify.rb
nilly_vanilly-1.0.0 lib/nilly_vanilly/nillify.rb
nilly_vanilly-0.3.0 lib/nilly_vanilly/nillify.rb