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 =
"0.3.1"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.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



56
57
58
59
# File 'lib/destroyed_at.rb', line 56

def delete
  self.destroyed_at = nil
  super
end

#destroy(timestamp = nil) ⇒ Object

Set an object’s destroyed_at time.



28
29
30
31
32
33
34
35
36
# File 'lib/destroyed_at.rb', line 28

def destroy(timestamp = nil)
  timestamp ||= 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

#persisted?Boolean

Returns:

  • (Boolean)


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

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

#restoreObject

Set an object’s destroyed_at time to nil.



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

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