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
5 6 7 |
# File 'lib/acts_as_paranoid/core.rb', line 5 def self.included(base) base.extend ClassMethods end |
Instance Method Details
#delete ⇒ Object
Straight from ActiveRecord 5.1!
126 127 128 129 130 |
# File 'lib/acts_as_paranoid/core.rb', line 126 def delete self.class.delete(id) if persisted? stale_paranoid_value freeze end |
#deleted? ⇒ Boolean Also known as: destroyed?
236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/acts_as_paranoid/core.rb', line 236 def deleted? return true if @destroyed if self.class.string_type_with_deleted_value? paranoid_value == paranoid_configuration[:deleted_value] elsif self.class.boolean_type_not_nullable? paranoid_value == true else !paranoid_value.nil? end end |
#deleted_fully? ⇒ Boolean Also known as: destroyed_fully?
250 251 252 |
# File 'lib/acts_as_paranoid/core.rb', line 250 def deleted_fully? @destroyed end |
#destroy! ⇒ Object Also known as: destroy
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/acts_as_paranoid/core.rb', line 152 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([Array(self.class.primary_key), Array(id)].transpose.to_h) decrement_counters_on_associations end @_trigger_destroy_callback = true stale_paranoid_value self end end elsif paranoid_configuration[:double_tap_destroys_fully] destroy_fully! end end |
#destroy_dependent_associations! ⇒ Object
226 227 228 229 230 231 232 233 234 |
# File 'lib/acts_as_paranoid/core.rb', line 226 def destroy_dependent_associations! self.class.dependent_associations.each do |reflection| next unless (klass = get_reflection_class(reflection)).paranoid? klass .only_deleted.merge(get_association_scope(reflection: reflection)) .each(&:destroy!) end end |
#destroy_fully! ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/acts_as_paranoid/core.rb', line 132 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!([Array(self.class.primary_key), Array(id)].transpose.to_h) decrement_counters_on_associations end stale_paranoid_value @destroyed = true freeze end end end |
#paranoid_value ⇒ Object
121 122 123 |
# File 'lib/acts_as_paranoid/core.rb', line 121 def paranoid_value send(self.class.paranoid_column) end |
#persisted? ⇒ Boolean
117 118 119 |
# File 'lib/acts_as_paranoid/core.rb', line 117 def persisted? !(new_record? || @destroyed) end |
#recover(options = {}) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/acts_as_paranoid/core.rb', line 177 def recover( = {}) return if !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 if [:recursive] recover_dependent_associations([:recovery_window], ) end increment_counters_on_associations self.paranoid_value = self.class.paranoid_configuration[:recovery_value] if [:raise_error] save! else save end end end end |
#recover!(options = {}) ⇒ Object
202 203 204 205 206 |
# File 'lib/acts_as_paranoid/core.rb', line 202 def recover!( = {}) [:raise_error] = true recover() end |
#recover_dependent_associations(window, options) ⇒ Object
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/acts_as_paranoid/core.rb', line 208 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(get_association_scope(reflection: reflection)) # 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 |