Module: ActsAsParanoid::InstanceMethods

Defined in:
lib/rails4_acts_as_paranoid.rb

Instance Method Summary collapse

Instance Method Details

#act_on_dependent_destroy_associationsObject



336
337
338
339
340
341
342
343
344
# File 'lib/rails4_acts_as_paranoid.rb', line 336

def act_on_dependent_destroy_associations
  self.class.dependent_associations.each do |association|
    if association.collection? && self.send(association.name).paranoid?
      association.klass.with_deleted.where(association.foreign_key.to_sym => self.id.to_json).each do |object|
        object.destroy!
      end
    end
  end
end

#deleteObject



285
286
287
288
289
290
291
292
293
294
295
# File 'lib/rails4_acts_as_paranoid.rb', line 285

def delete
  return self unless check_persisted_for_delete(false)
  if !deleted?
    with_transaction_returning_status do
      self.class.delete_all(self.class.primary_key.to_sym => self.id)
      set_paranoid_value false
    end
  else
    delete!
  end
end

#delete!Object



276
277
278
279
280
281
282
283
# File 'lib/rails4_acts_as_paranoid.rb', line 276

def delete!
  return self unless check_persisted_for_delete(true)
  with_transaction_returning_status do
    act_on_dependent_destroy_associations
    self.class.delete_all!(self.class.primary_key.to_sym => self.id)
    set_paranoid_value true
  end
end

#deleted?Boolean Also known as: destroyed?

Returns:

  • (Boolean)


346
347
348
# File 'lib/rails4_acts_as_paranoid.rb', line 346

def deleted?
  !(paranoid_value == self.class.non_deleted_value)
end

#destroyObject



262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/rails4_acts_as_paranoid.rb', line 262

def destroy
  return self unless check_persisted_for_delete(false)
  if !deleted?
    with_transaction_returning_status do
      run_callbacks :destroy do
        self.class.delete_all(self.class.primary_key.to_sym => self.id)
        set_paranoid_value false
      end
    end
  else
    destroy!
  end
end

#destroy!Object



251
252
253
254
255
256
257
258
259
260
# File 'lib/rails4_acts_as_paranoid.rb', line 251

def destroy!
  return self unless check_persisted_for_delete(true)
  with_transaction_returning_status do
    run_callbacks :destroy do
      act_on_dependent_destroy_associations
      self.class.delete_all!(self.class.primary_key.to_sym => self.id)
      set_paranoid_value true
    end
  end
end

#paranoid_valueObject



247
248
249
# File 'lib/rails4_acts_as_paranoid.rb', line 247

def paranoid_value
  self.send(self.class.paranoid_column)
end

#recover(options = {}) ⇒ Object



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/rails4_acts_as_paranoid.rb', line 297

def recover(options={})
  options = {
              :recursive => self.class.primary_paranoid_column[:recover_dependent_associations],
              :recovery_window => self.class.primary_paranoid_column[:dependent_recovery_window]
            }.merge(options)

  self.class.transaction do
    run_callbacks :recover do
      recover_dependent_associations(options[:recovery_window], options) if options[:recursive]

      self.paranoid_value = self.class.non_deleted_value
      self.save
    end
  end
end

#recover_dependent_associations(window, options) ⇒ Object



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/rails4_acts_as_paranoid.rb', line 313

def recover_dependent_associations(window, options)
  self.class.dependent_associations.each do |association|
    if association.collection? && self.send(association.name).paranoid?
      self.send(association.name).unscoped do
        self.send(association.name).paranoid_deleted_around_time(paranoid_value, window).each do |object|
          object.recover(options) if object.respond_to?(:recover)
        end
      end
    elsif association.macro == :has_one && association.klass.paranoid?
      association.klass.unscoped do
        object = association.klass.paranoid_deleted_around_time(paranoid_value, window).send('find_by_'+association.foreign_key, self.id)
        object.recover(options) if object && object.respond_to?(:recover)
      end
    elsif association.klass.paranoid?
      association.klass.unscoped do
        id = self.send(association.foreign_key)
        object = association.klass.paranoid_deleted_around_time(paranoid_value, window).find_by_id(id)
        object.recover(options) if object && object.respond_to?(:recover)
      end
    end
  end
end