Skip to content

Instantly share code, notes, and snippets.

@kyledrake
Created February 22, 2012 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyledrake/1886528 to your computer and use it in GitHub Desktop.
Save kyledrake/1886528 to your computer and use it in GitHub Desktop.
Quick and dirty "paranoid delete" for Sequel
module Sequel
module ParanoidDelete
def self.included(base)
base.extend(ClassMethods)
end
# Instead of actually deleting, we just set is_deleted to true,
# and look for it with our default dataset filter.
def delete
self.is_deleted = true
save :validate => false
true
end
module ClassMethods
# There's no hook for setting default filters after inheritance (that I'm aware of),
# so this adds the filter for the first time the class' dataset is accessed for the new model.
def dataset
if @_is_deleted_filter_set.nil?
@dataset.filter! is_deleted: false
@_is_deleted_filter_set = true
end
super
end
end
end
Model.include ParanoidDelete
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment