Skip to content

Instantly share code, notes, and snippets.

@ivobenedito
Created July 11, 2016 15:11
Show Gist options
  • Select an option

  • Save ivobenedito/cff6ad05a0b7dcd35ebfd536402fa3f0 to your computer and use it in GitHub Desktop.

Select an option

Save ivobenedito/cff6ad05a0b7dcd35ebfd536402fa3f0 to your computer and use it in GitHub Desktop.
module Trashable
extend ActiveSupport::Concern
included do
default_scope { where(deleted_at: nil) }
end
module ClassMethods
def trashed
unscope(where: :deleted_at).where.not(deleted_at: nil)
end
def with_trashed
unscope(where: :deleted_at)
end
end
def trashed?
deleted_at.present?
end
def trash!
update(deleted_at: Time.now)
end
def restore!
update(deleted_at: nil)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment