module Shamu::Entities::ActiveRecordSoftDestroy

Add the ability to “soft-delete” a record. Marking it as deleted so it is no longer present in the default scope but without actually removing the record from the database.

> Note You must add a column `destroyed_at` to the model.

Public Class Methods

apply_destroyed_list_scope( criteria, scope ) click to toggle source

Apply list scoping that includes targeting `destroyed` state.

# File lib/shamu/entities/active_record_soft_destroy.rb, line 44
def self.apply_destroyed_list_scope( criteria, scope )
  return criteria if scope.destroyed.nil?

  if scope.destroyed
    criteria.destroyed
  else
    criteria.except_destroyed
  end
end

Public Instance Methods

destroy( options = nil ) click to toggle source

Mark the record as deleted. @overload destroy @return [Boolean] true if the record was destroyed.

Calls superclass method
# File lib/shamu/entities/active_record_soft_destroy.rb, line 62
def destroy( options = nil )
  if destroyed_at || ( options && options[:obliterate] )
    super()
  else
    update_attribute :destroyed_at, Time.now.utc
  end
end
destroy!( options = nil ) click to toggle source

Really destroy! the record. @overload destroy! @return [Boolean] true if the record was destroyed.

Calls superclass method
# File lib/shamu/entities/active_record_soft_destroy.rb, line 79
def destroy!( options = nil )
  if destroyed_at || ( options && options[:obliterate] )
    super()
  else
    update_attribute :destroyed_at, Time.now.utc
  end
end
obliterate() click to toggle source

Really destroy the record. @return [Boolean] true if the record was destroyed.

# File lib/shamu/entities/active_record_soft_destroy.rb, line 72
def obliterate
  destroy( obliterate: true )
end
obliterate!() click to toggle source

Really destroy! the record. @return [Boolean] true if the record was destroyed.

# File lib/shamu/entities/active_record_soft_destroy.rb, line 89
def obliterate!
  destroy!( obliterate: true )
end
soft_destroyed?() click to toggle source

@return [Boolean] true if the record has been soft destroyed.

# File lib/shamu/entities/active_record_soft_destroy.rb, line 100
def soft_destroyed?
  !!destroyed_at
end
undestroy() click to toggle source

Mark the record as no longer destroyed. @return [Boolean] true if the record was restored.

# File lib/shamu/entities/active_record_soft_destroy.rb, line 95
def undestroy
  update_attribute :destroyed_at, nil
end