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

#execute_soft_delete(method) ⇒ Object



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

def execute_soft_delete(method, ...)
  case association.options[:dependent]
  when :nullify
    dependency.update_all(association.foreign_key => nil)
  when :delete_all
    dependency.update_all(dependency.mark_as_soft_deleted_sql)
    true
  else
    dependencies.all? { |dep| dep.send(method, ...) }
  end
end

#soft_undelete!(limit) ⇒ Object



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

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