Sha256: 39848563449ec2f1b98b4044ebd589d0e1eaac610fd28135d8f9e011c0feff2d

Contents?: true

Size: 1.8 KB

Versions: 4

Compression:

Stored size: 1.8 KB

Contents

module ActiveRecord
  module Associations
    module ClassMethods
      
      # Add a boolean :null_object option to belongs_to.
      def belongs_to_with_null_object(association_id, options = {}) #:nodoc:
        # Extract our custom option.
        use_null_object = options.delete(:null_object)
        # Call the real belongs_to so that the association gets defined.
        belongs_to_without_null_object(association_id, options)
        # Modify the association if need be.
        if use_null_object
          
          # Determine the class of the association.
          association_class_name = options[:class_name] || association_id.to_s.classify
          association_class = association_class_name.constantize

          # Determine the null class for this association.
          null_class_name = "Null" + association_class_name
          if Object.const_defined?(null_class_name)
            null_class = Object.const_get(null_class_name.to_sym)
          else
            # Define the null class as an ancestor of the association class.
            null_class = Object.const_set(null_class_name, Class.new(association_class))
            null_class.class_eval do
              include Singleton
              include ActiveRecordNullObject::NullObject
            end
          end

          # Modify the "getter" of the belongs_to relationship to return an
          # instance of the association's null object instead of returning nil.
          class_eval do
            define_method("#{association_id}_with_null_object".to_sym) do
              send("#{association_id}_without_null_object".to_sym) || null_class.instance
            end
            alias_method_chain association_id, :null_object
          end
          
        end
      end
      alias_method_chain :belongs_to, :null_object
      
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
westarete-activerecord_null_object-0.0.0 lib/activerecord_null_object.rb
westarete-activerecord_null_object-0.0.1 lib/activerecord_null_object.rb
westarete-activerecord_null_object-0.1.0 lib/activerecord_null_object.rb
activerecord_null_object-0.1.0 lib/activerecord_null_object.rb