Sha256: 69166ecb81ecde69f9ac8cc028efc9869f4ad3c7b986d68b78ff02e06d5c90cf

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

module SoftDeletion
  module Setup

    def self.included(base)
      base.extend ClassMethods
    end

    module ClassMethods
      # When you call this, it will include the core module and its methods
      #
      # Options:
      #
      # *default_scope*, value: true/false
      # If true, it will also define a default scope
      #
      # It will check if the column "deleted_at" exist before applying default scope
      def has_soft_deletion(options={})
        default_options = {:default_scope => false}

        include SoftDeletion::Core

        options = default_options.merge(options)

        if options[:default_scope] && table_exists? && column_names.include?("deleted_at")
          # Avoids a bad SQL request with versions of code without the column deleted_at (for example a migration prior to the migration
          # that adds deleted_at)
          conditions = {:deleted_at => nil}
          if ActiveRecord::VERSION::STRING < "3.1"
            default_scope :conditions => conditions
          else
            default_scope { where(conditions) }
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
soft_deletion-0.4.6 lib/soft_deletion/setup.rb