Module: ActsAsParanoid::Core
- Defined in:
- lib/acts_as_paranoid/core.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
-
#delete ⇒ Object
Straight from ActiveRecord 5.1!.
- #deleted? ⇒ Boolean (also: #destroyed?)
- #deleted_fully? ⇒ Boolean (also: #destroyed_fully?)
- #destroy! ⇒ Object (also: #destroy)
- #destroy_dependent_associations! ⇒ Object
- #destroy_fully! ⇒ Object
- #paranoid_value ⇒ Object
- #persisted? ⇒ Boolean
- #recover(options = {}) ⇒ Object
- #recover!(options = {}) ⇒ Object
- #recover_dependent_associations(window, options) ⇒ Object
Class Method Details
.included(base) ⇒ Object
3 4 5 |
# File 'lib/acts_as_paranoid/core.rb', line 3 def self.included(base) base.extend ClassMethods end |
Instance Method Details
#delete ⇒ Object
Straight from ActiveRecord 5.1!
109 110 111 112 113 |
# File 'lib/acts_as_paranoid/core.rb', line 109 def delete self.class.delete(id) if persisted? stale_paranoid_value freeze end |
#deleted? ⇒ Boolean Also known as: destroyed?
233 234 235 236 237 238 239 240 241 |
# File 'lib/acts_as_paranoid/core.rb', line 233 def deleted? @destroyed || !if self.class.string_type_with_deleted_value? paranoid_value != self.class.delete_now_value || paranoid_value.nil? elsif self.class.boolean_type_not_nullable? paranoid_value == false else paranoid_value.nil? end end |
#deleted_fully? ⇒ Boolean Also known as: destroyed_fully?
245 246 247 |
# File 'lib/acts_as_paranoid/core.rb', line 245 def deleted_fully? @destroyed end |
#destroy! ⇒ Object Also known as: destroy
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/acts_as_paranoid/core.rb', line 134 def destroy! if !deleted? with_transaction_returning_status do run_callbacks :destroy do if persisted? # Handle composite keys, otherwise we would just use `self.class.primary_key.to_sym => self.id`. self.class.delete_all(Hash[[Array(self.class.primary_key), Array(self.id)].transpose]) decrement_counters_on_associations end @_trigger_destroy_callback = true stale_paranoid_value self end end else if paranoid_configuration[:double_tap_destroys_fully] destroy_fully! end end end |
#destroy_dependent_associations! ⇒ Object
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/acts_as_paranoid/core.rb', line 214 def destroy_dependent_associations! self.class.dependent_associations.each do |reflection| next unless (klass = get_reflection_class(reflection)).paranoid? scope = klass.only_deleted # Merge in the association's scope scope = if ActiveRecord::VERSION::MAJOR >= 6 scope.merge(ActiveRecord::Associations::AssociationScope.scope(association(reflection.name))) else scope.merge(association(reflection.name).association_scope) end scope.each do |object| object.destroy! end end end |
#destroy_fully! ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/acts_as_paranoid/core.rb', line 115 def destroy_fully! with_transaction_returning_status do run_callbacks :destroy do destroy_dependent_associations! if persisted? # Handle composite keys, otherwise we would just use `self.class.primary_key.to_sym => self.id`. self.class.delete_all!(Hash[[Array(self.class.primary_key), Array(self.id)].transpose]) decrement_counters_on_associations end stale_paranoid_value @destroyed = true freeze end end end |
#paranoid_value ⇒ Object
104 105 106 |
# File 'lib/acts_as_paranoid/core.rb', line 104 def paranoid_value self.send(self.class.paranoid_column) end |
#persisted? ⇒ Boolean
100 101 102 |
# File 'lib/acts_as_paranoid/core.rb', line 100 def persisted? !(new_record? || @destroyed) end |
#recover(options = {}) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/acts_as_paranoid/core.rb', line 161 def recover(={}) return if !self.deleted? = { :recursive => self.class.paranoid_configuration[:recover_dependent_associations], :recovery_window => self.class.paranoid_configuration[:dependent_recovery_window], :raise_error => false }.merge() self.class.transaction do run_callbacks :recover do recover_dependent_associations([:recovery_window], ) if [:recursive] increment_counters_on_associations self.paranoid_value = self.class.paranoid_configuration[:recovery_value] if [:raise_error] self.save! else self.save end end end end |
#recover!(options = {}) ⇒ Object
183 184 185 186 187 |
# File 'lib/acts_as_paranoid/core.rb', line 183 def recover!(={}) [:raise_error] = true recover() end |
#recover_dependent_associations(window, options) ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/acts_as_paranoid/core.rb', line 189 def recover_dependent_associations(window, ) self.class.dependent_associations.each do |reflection| next unless (klass = get_reflection_class(reflection)).paranoid? scope = klass.only_deleted # Merge in the association's scope scope = if ActiveRecord::VERSION::MAJOR >= 6 scope.merge(ActiveRecord::Associations::AssociationScope.scope(association(reflection.name))) else scope.merge(association(reflection.name).association_scope) end # We can only recover by window if both parent and dependant have a # paranoid column type of :time. if self.class.paranoid_column_type == :time && klass.paranoid_column_type == :time scope = scope.deleted_inside_time_window(paranoid_value, window) end scope.each do |object| object.recover() end end end |