Module: DestroyedAt

Defined in:
lib/destroyed_at.rb,
lib/destroyed_at/version.rb,
lib/destroyed_at/has_one_association.rb,
lib/destroyed_at/has_many_association.rb,
lib/destroyed_at/belongs_to_association.rb

Defined Under Namespace

Modules: BelongsToAssociation, ClassMethods, HasManyAssociation, HasOneAssociation, Mapper, Resource, Routes

Constant Summary collapse

VERSION =
"2.1.0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.destroy_target_of_association(owner, target) ⇒ Object


17
18
19
20
21
22
23
24
# File 'lib/destroyed_at.rb', line 17

def self.destroy_target_of_association(owner, target)
  if target.respond_to?(:destroyed_at) && owner.respond_to?(:destroyed_at)
    target.destroy(owner.destroyed_at)
  else
    # this will delete the target if DestroyedAt is not included.
    target.destroy
  end
end

.has_destroy_at?(object) ⇒ Boolean


26
27
28
# File 'lib/destroyed_at.rb', line 26

def self.has_destroy_at?(object)
  object.class.included_modules.include?(DestroyedAt)
end

.included(klass) ⇒ Object


8
9
10
11
12
13
14
15
# File 'lib/destroyed_at.rb', line 8

def self.included(klass)
  klass.instance_eval do
    default_scope { where(destroyed_at: nil) }
    after_initialize :_set_destruction_state
    define_model_callbacks :restore
    extend ClassMethods
  end
end

Instance Method Details

#deleteObject


75
76
77
78
# File 'lib/destroyed_at.rb', line 75

def delete
  self.destroyed_at = nil
  super
end

#destroy(timestamp = nil) ⇒ Object

Set an object's destroyed_at time.


41
42
43
44
45
46
47
48
49
# File 'lib/destroyed_at.rb', line 41

def destroy(timestamp = nil)
  timestamp ||= @marked_for_destruction_at || current_time_from_proper_timezone
  raw_write_attribute(:destroyed_at, timestamp)
  run_callbacks(:destroy) do
    destroy_associations
    self.class.unscoped.where(self.class.primary_key => id).update_all(destroyed_at: timestamp)
    @destroyed = true
  end
end

#mark_for_destruction(timestamp = nil) ⇒ Object


51
52
53
54
55
# File 'lib/destroyed_at.rb', line 51

def mark_for_destruction(timestamp = nil)
  @marked_for_destruction_at = timestamp

  super()
end

#persisted?Boolean


71
72
73
# File 'lib/destroyed_at.rb', line 71

def persisted?
  !new_record? && destroyed_at.present? || super
end

#restoreObject

Set an object's destroyed_at time to nil.


58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/destroyed_at.rb', line 58

def restore
  state = nil
  run_callbacks(:restore) do
    if state = (self.class.unscoped.where(self.class.primary_key => id).update_all(destroyed_at: nil) == 1)
      _restore_associations
      raw_write_attribute(:destroyed_at, nil)
      @destroyed = false
      true
    end
  end
  state
end