Module: Ohm::SoftDelete

Defined in:
lib/ohm/softdelete.rb

Overview

Provides support for soft deletion.

class Post < Ohm::Model
  include Ohm::SoftDelete

  attribute :title
  index :title
end

post = Post.create(title: 'Title')

post.deleted?# => false


post.delete

post.deleted?# => true


Post.all.empty?# => true


Post.find(title: 'Title').include?(post)# => true


Post.exists?(post.id)# => true


post = Post[post.id]

post.deleted?# => true

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(model) ⇒ Object


36
37
38
39
40
# File 'lib/ohm/softdelete.rb', line 36

def self.included(model)
  model.attribute :deleted, ->(x) { !!x }

  model.extend ClassMethods
end

Instance Method Details

#deleteObject


42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ohm/softdelete.rb', line 42

def delete
  redis.queue("MULTI")
  redis.queue("SREM", model.all.key, id)
  redis.queue("SADD", model.deleted.key, id)
  redis.queue("HSET", key, :deleted, true)
  redis.queue("EXEC")
  redis.commit

  self.deleted = true

  self
end

#deleted?Boolean

Returns:

  • (Boolean)

68
69
70
# File 'lib/ohm/softdelete.rb', line 68

def deleted?
  deleted
end

#restoreObject


55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ohm/softdelete.rb', line 55

def restore
  redis.queue("MULTI")
  redis.queue("SADD", model.all.key, id)
  redis.queue("SREM", model.deleted.key, id)
  redis.queue("HDEL", key, :deleted)
  redis.queue("EXEC")
  redis.commit

  self.deleted = nil

  self
end