Module: Paranoid2::Persistence

Extended by:
ActiveSupport::Concern
Defined in:
lib/paranoid2/persistence.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#delete(opts = {}) ⇒ Object



13
14
15
16
17
18
# File 'lib/paranoid2/persistence.rb', line 13

def delete(opts = {})
  with_paranoid(opts) do
    touch(:deleted_at) if !deleted? && persisted?
    self.class.unscoped { super() } if paranoid_force
  end
end

#destroy(opts = {}) ⇒ Object



5
6
7
# File 'lib/paranoid2/persistence.rb', line 5

def destroy(opts = {})
  with_paranoid(opts) { super() }
end

#destroy!(opts = {}) ⇒ Object



9
10
11
# File 'lib/paranoid2/persistence.rb', line 9

def destroy!(opts = {})
  with_paranoid(opts) { super() }
end

#destroy_rowObject



64
65
66
67
68
69
70
71
72
# File 'lib/paranoid2/persistence.rb', line 64

def destroy_row
  if paranoid_force
    self.deleted_at = Time.now
    super
  else
    delete
    1
  end
end

#destroyed?Boolean Also known as: deleted?

Returns:

  • (Boolean)


54
55
56
# File 'lib/paranoid2/persistence.rb', line 54

def destroyed?
  !deleted_at.nil?
end

#persisted?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/paranoid2/persistence.rb', line 58

def persisted?
  !new_record?
end

#restore(opts = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/paranoid2/persistence.rb', line 20

def restore(opts={})
  return if !destroyed?

  attrs = timestamp_attributes_for_update_in_model
  current_time = current_time_from_proper_timezone
  changes = {}
  attrs.each do |column|
    changes[column.to_s] = write_attribute(column.to_s, current_time)
  end
  changes['deleted_at'] = write_attribute('deleted_at', nil)

  changes[self.class.locking_column] = increment_lock if locking_enabled?

  @changed_attributes.except!(*changes.keys)
  primary_key = self.class.primary_key
  self.class.unscoped.where({ primary_key => self[primary_key] }).update_all(changes)

  if opts.fetch(:associations) { true }
    restore_associations
  end
end

#restore_associationsObject



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

def restore_associations
  self.class.reflect_on_all_associations.each do |a|
    next unless a.klass.paranoid?

    if a.collection?
      send(a.name).restore_all
    else
      a.klass.unscoped { send(a.name).try(:restore) }
    end
  end
end