Sha256: f7c03f457bed61cf7cd8fcedb6afc9ad6e1a4c96ed493ae7e7d6dfb7dbacaefd

Contents?: true

Size: 1.36 KB

Versions: 3

Compression:

Stored size: 1.36 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]
          conditions = {:deleted_at => nil}
          if ActiveRecord::VERSION::STRING < "3.1"
            # 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)
            if !table_exists?
              warn "#{table_name} table missing, disabling soft_deletion default scope"
            elsif !column_names.include?("deleted_at")
              warn "#{table_name} does not have deleted_at column, disabling soft_deletion default scope"
            else
              default_scope :conditions => conditions
            end
          else
            default_scope { where(conditions) }
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
soft_deletion-0.5.2 lib/soft_deletion/setup.rb
soft_deletion-0.5.1 lib/soft_deletion/setup.rb
soft_deletion-0.5.0 lib/soft_deletion/setup.rb