Module: Shamu::Entities::ActiveRecordSoftDestroy

Extended by:
ActiveSupport::Concern
Defined in:
lib/shamu/entities/active_record_soft_destroy.rb

Overview

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.

Attributes collapse

Scopes collapse

Instance Method Summary collapse

Instance Attribute Details

#destroyed_atDateTime

Returns when the record was destroyed.

Returns:

  • (DateTime)

    when the record was destroyed.



# File 'lib/shamu/entities/active_record_soft_destroy.rb', line 18

Class Method Details

.apply_destroyed_list_scope(criteria, scope) ⇒ Object

Apply list scoping that includes targeting destroyed state.



44
45
46
47
48
49
50
51
52
# 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

Instance Method Details

#destroyBoolean

Mark the record as deleted.

Returns:

  • (Boolean)

    true if the record was destroyed.



62
63
64
65
66
67
68
# 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!Boolean

Really destroy! the record.

Returns:

  • (Boolean)

    true if the record was destroyed.



79
80
81
82
83
84
85
# 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

#destroyedActiveRecord::Relation

Limit the records to those that have been soft destroyed.

Returns:

  • (ActiveRecord::Relation)


30
# File 'lib/shamu/entities/active_record_soft_destroy.rb', line 30

scope :destroyed, ->() { unscope( where: :destroyed_at ).where( arel_table[ :destroyed_at ].not_eq( nil ) ) }

#except_destroyedActiveRecord::Relation

Limit the records to those that have not been destroyed.

Returns:

  • (ActiveRecord::Relation)


34
# File 'lib/shamu/entities/active_record_soft_destroy.rb', line 34

scope :except_destroyed, ->() { unscope( where: :destroyed_at ).where( arel_table[ :destroyed_at ].eq( nil ) ) }

#including_destroyedActiveRecord::Relation

Include live and soft destroyed records.

Returns:

  • (ActiveRecord::Relation)


38
# File 'lib/shamu/entities/active_record_soft_destroy.rb', line 38

scope :including_destroyed, ->() { unscope( where: :destroyed_at ) }

#obliterateBoolean

Really destroy the record.

Returns:

  • (Boolean)

    true if the record was destroyed.



72
73
74
# File 'lib/shamu/entities/active_record_soft_destroy.rb', line 72

def obliterate
  destroy( obliterate: true )
end

#obliterate!Boolean

Really destroy! the record.

Returns:

  • (Boolean)

    true if the record was destroyed.



89
90
91
# File 'lib/shamu/entities/active_record_soft_destroy.rb', line 89

def obliterate!
  destroy!( obliterate: true )
end

#soft_destroyed?Boolean

Returns true if the record has been soft destroyed.

Returns:

  • (Boolean)

    true if the record has been soft destroyed.



100
101
102
# File 'lib/shamu/entities/active_record_soft_destroy.rb', line 100

def soft_destroyed?
  !!destroyed_at
end

#undestroyBoolean

Mark the record as no longer destroyed.

Returns:

  • (Boolean)

    true if the record was restored.



95
96
97
# File 'lib/shamu/entities/active_record_soft_destroy.rb', line 95

def undestroy
  update_attribute :destroyed_at, nil
end