Module: DeleteSoftly::InstanceMethods

Defined in:
lib/instance_methods.rb

Instance Method Summary collapse

Instance Method Details

#deleted?Boolean

This method reports whether or not the record has been soft deleted.

Returns:

  • (Boolean)


6
7
8
# File 'lib/instance_methods.rb', line 6

def deleted?
  self.deleted_at?
end

#destroyObject

Custom destroy method for models using delete_softly



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/instance_methods.rb', line 11

def destroy 
  if persisted?
    with_transaction_returning_status do
      _run_destroy_callbacks do
        update_attribute :deleted_at, Time.now
      end
    end
  end

  @destroyed = true
end

#reviveObject Also known as: undelete, undestroy

Revive a destroyed item. For a model like:

class Post < ActiveRecord::Base
  delete_softly
end

Then the following can be done:

p = Post.find(1)
p.destroy
p = Post.find(1) # raise error
p = Post.with_deleted.find(1) # Original object but with deleted_at attribute set
p.revive #=> deleted_at => nil
p = Post.find(1) # business as usual

If papertrail is used for this model it will not store a copy



35
36
37
38
39
40
41
42
43
44
# File 'lib/instance_methods.rb', line 35

def revive
  # Disable paper_trail when it is present and active
  if self.class.respond_to?(:paper_trail_active) && self.class.paper_trail_active
    self.class.paper_trail_off
    update_attribute :deleted_at, nil
    self.class.paper_trail_on
  else
    update_attribute :deleted_at, nil
  end
end