Module: SoftDestroyable

Extended by:
ActiveSupport::Concern
Defined in:
lib/generators/rolemodel/soft_destroyable/templates/soft_destroyable.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#assign_attributes(attrs) ⇒ Object



42
43
44
# File 'lib/generators/rolemodel/soft_destroyable/templates/soft_destroyable.rb', line 42

def assign_attributes(attrs)
  super(attrs[:_soft_destroy].present? ? attrs.slice(:_soft_destroy) : attrs)
end

#restore(timestamp = deleted_at) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/generators/rolemodel/soft_destroyable/templates/soft_destroyable.rb', line 62

def restore(timestamp = deleted_at)
  return unless deleted_at == timestamp

  run_callbacks(:restore) do
    update(deleted_at: nil)
  end
end

#restore!Object

This may have unintended consequences or issues with restoring records with associations in varying states This will only raise an exception at the top level but does not guarantee nested objects were restored and will not guarantee errors restoring nested objects are surfaced



73
74
75
# File 'lib/generators/rolemodel/soft_destroyable/templates/soft_destroyable.rb', line 73

def restore!
  restore(deleted_at) || raise_validation_errors
end

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



50
51
52
53
54
55
56
# File 'lib/generators/rolemodel/soft_destroyable/templates/soft_destroyable.rb', line 50

def soft_destroy(timestamp = Time.zone.now)
  return if soft_destroyed?

  run_callbacks(:soft_destroy) do
    update(deleted_at: timestamp)
  end
end

#soft_destroy!Object



58
59
60
# File 'lib/generators/rolemodel/soft_destroyable/templates/soft_destroyable.rb', line 58

def soft_destroy!
  soft_destroy || raise_validation_errors
end

#soft_destroyed?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/generators/rolemodel/soft_destroyable/templates/soft_destroyable.rb', line 46

def soft_destroyed?
  deleted_at.present?
end