Module: SoftDestroyable::ClassMethods

Defined in:
lib/generators/rolemodel/soft_destroyable/templates/soft_destroyable.rb

Instance Method Summary collapse

Instance Method Details

#cascade_soft_destroy(associations) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/generators/rolemodel/soft_destroyable/templates/soft_destroyable.rb', line 28

def cascade_soft_destroy(associations)
  Array.wrap(associations).each do |association_name|
    define_method("soft_destroy_#{association_name}") do
      send(association_name).soft_destroy(deleted_at)
    end
    after_soft_destroy("soft_destroy_#{association_name}".to_sym)
    define_method("restore_#{association_name}") do
      send(association_name).restore(deleted_at)
    end
    before_restore("restore_#{association_name}".to_sym)
  end
end

#default_scope(scope = nil) ⇒ Object



14
15
16
17
18
# File 'lib/generators/rolemodel/soft_destroyable/templates/soft_destroyable.rb', line 14

def default_scope(scope = nil)
  unless scope.nil? && !block_given?
    raise "Default scopes should not be used with soft destroyable - in class #{name}"
  end
end

#restore(timestamp) ⇒ Object



24
25
26
# File 'lib/generators/rolemodel/soft_destroyable/templates/soft_destroyable.rb', line 24

def restore(timestamp)
  only_deleted.each { |o| o.restore(timestamp) }
end

#soft_destroy(timestamp = Time.zone.now) ⇒ Object



20
21
22
# File 'lib/generators/rolemodel/soft_destroyable/templates/soft_destroyable.rb', line 20

def soft_destroy(timestamp = Time.zone.now)
  kept.each { |o| o.soft_destroy(timestamp) }
end