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 =
"1.0.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
|
# 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)
elsif target.respond_to?(:destroyed_at)
target.destroy
end
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
#delete ⇒ Object
64
65
66
67
|
# File 'lib/destroyed_at.rb', line 64
def delete
self.destroyed_at = nil
super
end
|
#destroy(timestamp = nil) ⇒ Object
Set an object’s destroyed_at time.
36
37
38
39
40
41
42
43
44
|
# File 'lib/destroyed_at.rb', line 36
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
60
61
62
|
# File 'lib/destroyed_at.rb', line 60
def persisted?
!new_record? && destroyed_at.present? || super
end
|
#restore ⇒ Object
Set an object’s destroyed_at time to nil.
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/destroyed_at.rb', line 47
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
|