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

Instance Method Details

#destroyBoolean

Mark the record as deleted.

Returns:

  • (Boolean)

    true if the record was destroyed.



47
48
49
50
51
52
53
# File 'lib/shamu/entities/active_record_soft_destroy.rb', line 47

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.



64
65
66
67
68
69
70
# File 'lib/shamu/entities/active_record_soft_destroy.rb', line 64

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 ) ) }

#including_destroyedActiveRecord::Relation

Include live and soft destroyed records.

Returns:

  • (ActiveRecord::Relation)


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

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

#obliterateBoolean

Really destroy the record.

Returns:

  • (Boolean)

    true if the record was destroyed.



57
58
59
# File 'lib/shamu/entities/active_record_soft_destroy.rb', line 57

def obliterate
  destroy( obliterate: true )
end

#obliterate!Boolean

Really destroy! the record.

Returns:

  • (Boolean)

    true if the record was destroyed.



74
75
76
# File 'lib/shamu/entities/active_record_soft_destroy.rb', line 74

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.



85
86
87
# File 'lib/shamu/entities/active_record_soft_destroy.rb', line 85

def soft_destroyed?
  !!destroyed_at
end

#undestroyBoolean

Mark the record as no longer destroyed.

Returns:

  • (Boolean)

    true if the record was restored.



80
81
82
# File 'lib/shamu/entities/active_record_soft_destroy.rb', line 80

def undestroy
  update_attribute :destroyed_at, nil
end