Module: Paranoia
- Defined in:
- lib/paranoia.rb,
lib/paranoia/version.rb
Defined Under Namespace
Modules: Callbacks, Query
Constant Summary
collapse
- VERSION =
"2.2.0"
- @@default_sentinel_value =
nil
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.default_sentinel_value ⇒ Object
11
12
13
|
# File 'lib/paranoia.rb', line 11
def self.default_sentinel_value
@@default_sentinel_value
end
|
.default_sentinel_value=(val) ⇒ Object
Change default_sentinel_value in a rails initializer
7
8
9
|
# File 'lib/paranoia.rb', line 7
def self.default_sentinel_value=(val)
@@default_sentinel_value = val
end
|
.included(klazz) ⇒ Object
15
16
17
18
|
# File 'lib/paranoia.rb', line 15
def self.included(klazz)
klazz.extend Query
klazz.extend Callbacks
end
|
Instance Method Details
#delete ⇒ Object
91
92
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/paranoia.rb', line 91
def delete
raise ActiveRecord::ReadOnlyRecord, "#{self.class} is marked as readonly" if readonly?
if persisted?
add_to_transaction
update_columns(paranoia_destroy_attributes)
elsif !frozen?
assign_attributes(paranoia_destroy_attributes)
end
self
end
|
#destroy ⇒ Object
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/paranoia.rb', line 75
def destroy
transaction do
run_callbacks(:destroy) do
result = delete
next result unless result && ActiveRecord::VERSION::STRING >= '4.2'
each_counter_cached_associations do |association|
foreign_key = association.reflection.foreign_key.to_sym
next if destroyed_by_association && destroyed_by_association.foreign_key.to_sym == foreign_key
next unless send(association.reflection.name)
association.decrement_counters
end
result
end
end
end
|
#paranoia_destroyed? ⇒ Boolean
Also known as:
deleted?
123
124
125
|
# File 'lib/paranoia.rb', line 123
def paranoia_destroyed?
send(paranoia_column) != paranoia_sentinel_value
end
|
#really_destroy! ⇒ Object
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
# File 'lib/paranoia.rb', line 128
def really_destroy!
transaction do
run_callbacks(:real_destroy) do
dependent_reflections = self.class.reflections.select do |name, reflection|
reflection.options[:dependent] == :destroy
end
if dependent_reflections.any?
dependent_reflections.each do |name, reflection|
association_data = self.send(name)
next unless association_data && association_data.paranoid?
if reflection.collection?
next association_data.with_deleted.each(&:really_destroy!)
end
association_data.really_destroy!
end
end
write_attribute(paranoia_column, current_time_from_proper_timezone)
destroy_without_paranoia
end
end
end
|
#restore!(opts = {}) ⇒ Object
Also known as:
restore
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/paranoia.rb', line 104
def restore!(opts = {})
self.class.transaction do
run_callbacks(:restore) do
noop_if_frozen = ActiveRecord.version < Gem::Version.new("4.1")
if (noop_if_frozen && !@attributes.frozen?) || !noop_if_frozen
write_attribute paranoia_column, paranoia_sentinel_value
update_columns(paranoia_restore_attributes)
touch
end
restore_associated_records if opts[:recursive]
end
end
self
end
|