Class: SoftDeletion::Dependency

Inherits:
Object
  • Object
show all
Defined in:
lib/soft_deletion/dependency.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record, association_name) ⇒ Dependency

Returns a new instance of Dependency.



5
6
7
8
# File 'lib/soft_deletion/dependency.rb', line 5

def initialize(record, association_name)
  @record = record
  @association_name = association_name
end

Instance Attribute Details

#association_nameObject (readonly)

Returns the value of attribute association_name.



3
4
5
# File 'lib/soft_deletion/dependency.rb', line 3

def association_name
  @association_name
end

#recordObject (readonly)

Returns the value of attribute record.



3
4
5
# File 'lib/soft_deletion/dependency.rb', line 3

def record
  @record
end

Instance Method Details

#soft_delete!Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/soft_deletion/dependency.rb', line 10

def soft_delete!
  case association.options[:dependent]
  when :nullify
    nullify_dependencies
  when :delete_all
    dependency.update_all(:deleted_at => Time.now)
  else
    dependencies.each(&:soft_delete!)
  end
end

#soft_undelete!(limit) ⇒ Object



21
22
23
24
25
# File 'lib/soft_deletion/dependency.rb', line 21

def soft_undelete!(limit)
  klass.with_deleted do
    dependencies.reject { |m| m.deleted_at < limit }.each(&:soft_undelete!)
  end
end