• Stars
    star
    1,993
  • Rank 22,272 (Top 0.5 %)
  • Language
    Ruby
  • License
    MIT License
  • Created about 7 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

πŸƒπŸ—‘ Soft deletes for ActiveRecord done right

Discard Test

Soft deletes for ActiveRecord done right.

What does this do?

A simple ActiveRecord mixin to add conventions for flagging records as discarded.

Installation

Add this line to your application's Gemfile:

gem 'discard', '~> 1.2'

And then execute:

$ bundle

Usage

Declare a record as discardable

Declare the record as being discardable

class Post < ActiveRecord::Base
  include Discard::Model
end

You can either generate a migration using:

rails generate migration add_discarded_at_to_posts discarded_at:datetime:index

or create one yourself like the one below:

class AddDiscardToPosts < ActiveRecord::Migration[5.0]
  def change
    add_column :posts, :discarded_at, :datetime
    add_index :posts, :discarded_at
  end
end

Discard a record

Post.all             # => [#<Post id: 1, ...>]
Post.kept            # => [#<Post id: 1, ...>]
Post.discarded       # => []

post = Post.first   # => #<Post id: 1, ...>
post.discard        # => true
post.discard!       # => Discard::RecordNotDiscarded: Failed to discard the record
post.discarded?     # => true
post.undiscarded?   # => false
post.kept?          # => false
post.discarded_at   # => 2017-04-18 18:49:49 -0700

Post.all             # => [#<Post id: 1, ...>]
Post.kept            # => []
Post.discarded       # => [#<Post id: 1, ...>]

From a controller

Controller actions need a small modification to discard records instead of deleting them. Just replace destroy with discard.

def destroy
  @post.discard
  redirect_to users_url, notice: "Post removed"
end

Undiscard a record

post = Post.first   # => #<Post id: 1, ...>
post.undiscard      # => true
post.undiscard!     # => Discard::RecordNotUndiscarded: Failed to undiscard the record
post.discarded_at   # => nil

From a controller

def update
  @post.undiscard
  redirect_to users_url, notice: "Post undiscarded"
end

Working with associations

Under paranoia, soft deleting a record will destroy any dependent: :destroy associations. Probably not what you want! This leads to all dependent records also needing to be acts_as_paranoid, which makes restoring awkward: paranoia handles this by restoring any records which have their deleted_at set to a similar timestamp. Also, it doesn't always make sense to mark these records as deleted, it depends on the application.

A better approach is to simply mark the one record as discarded, and use SQL joins to restrict finding these if that's desired.

For example, in a blog comment system, with Posts and Comments, you might want to discard the records independently. A user's comment history could include comments on deleted posts.

Post.kept # SELECT * FROM posts WHERE discarded_at IS NULL
Comment.kept # SELECT * FROM comments WHERE discarded_at IS NULL

Or you could decide that comments are dependent on their posts not being discarded. Just override the kept scope on the Comment model.

class Comment < ActiveRecord::Base
  belongs_to :post

  include Discard::Model
  scope :kept, -> { undiscarded.joins(:post).merge(Post.kept) }

  def kept?
    undiscarded? && post.kept?
  end
end

Comment.kept
# SELECT * FROM comments
#    INNER JOIN posts ON comments.post_id = posts.id
# WHERE
#    comments.discarded_at IS NULL AND
#       posts.discarded_at IS NULL

SQL databases are very good at this, and performance should not be an issue.

In both of these cases restoring either of these records will do right thing!

Default scope

It's usually undesirable to add a default scope. It will take more effort to work around and will cause more headaches. If you know you need a default scope, it's easy to add yourself ❀.

class Post < ActiveRecord::Base
  include Discard::Model
  default_scope -> { kept }
end

Post.all                       # Only kept posts
Post.with_discarded            # All Posts
Post.with_discarded.discarded  # Only discarded posts

Custom column

If you're migrating from paranoia, you might want to continue using the same column.

class Post < ActiveRecord::Base
  include Discard::Model
  self.discard_column = :deleted_at
end

Callbacks

Callbacks can be run before, after, or around the discard and undiscard operations. A likely use is discarding or deleting associated records (but see "Working with associations" for an alternative).

class Comment < ActiveRecord::Base
  include Discard::Model
end

class Post < ActiveRecord::Base
  include Discard::Model

  has_many :comments

  after_discard do
    comments.discard_all
  end

  after_undiscard do
    comments.undiscard_all
  end
end

Warning: Please note that callbacks for save and update are run when discarding/undiscarding a record

Performance tuning

discard_all and undiscard_all is intended to behave like destroy_all which has callbacks, validations, and does one query per record. If performance is a big concern, you may consider replacing it with:

scope.update_all(discarded_at: Time.current) or scope.update_all(discarded_at: nil)

Working with Devise

A common use case is to apply discard to a User record. Even though a user has been discarded they can still login and continue their session. If you are using Devise and wish for discarded users to be unable to login and stop their session you can override Devise's method.

class User < ActiveRecord::Base
  def active_for_authentication?
    super && !discarded?
  end
end

Non-features

  • Special handling of AR counter cache columns - The counter cache counts the total number of records, both kept and discarded.
  • Recursive discards (like AR's dependent: destroy) - This can be avoided using queries (See "Working with associations") or emulated using callbacks.
  • Recursive restores - This concept is fundamentally broken, but not necessary if the recursive discards are avoided.

Extensions

Discard provides the smallest subset of soft-deletion features that we think are useful to all users of the gem. We welcome the addition of gems that work with Discard to provide additional features.

Why not paranoia or acts_as_paranoid?

I've worked with and have helped maintain paranoia for a while. I'm convinced it does the wrong thing for most cases.

Paranoia and acts_as_paranoid both attempt to emulate deletes by setting a column and adding a default scope on the model. This requires some ActiveRecord hackery, and leads to some surprising and awkward behaviour.

  • A default scope is added to hide soft-deleted records, which necessitates adding .with_deleted to associations or anywhere soft-deleted records should be found. 😞
    • Adding belongs_to :child, -> { with_deleted } helps, but doesn't work for joins and eager-loading before Rails 5.2
  • delete is overridden (really_delete will actually delete the record) πŸ˜’
  • destroy is overridden (really_destroy will actually delete the record) πŸ˜”
  • dependent: :destroy associations are deleted when performing soft-destroys 😱
    • requiring any dependent records to also be acts_as_paranoid to avoid losing data. 😬

There are some use cases where these behaviours make sense: if you really did want to almost delete the record. More often developers are just looking to hide some records, or mark them as inactive.

Discard takes a different approach. It doesn't override any ActiveRecord methods and instead simply provides convenience methods and scopes for discarding (hiding), restoring, and querying records.

You can find more information about the history and purpose of Discard in this blog post.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

Contributing

Please consider filing an issue with the details of any features you'd like to see before implementing them. Discard is feature-complete and we are only interested in adding additional features that won't require substantial maintenance burden and that will benefit all users of the gem. We encourage anyone that needs additional or different behaviour to either create their own gem that builds off of discard or implement a new package with the different behaviour.

Discard is very simple and we like it that way. Creating your own clone or fork with slightly different behaviour may not be that much work!

If you find a bug in discard, please report it! We try to keep up with any issues and keep the gem running smoothly for everyone! You can report issues here.

License

The gem is available as open source under the terms of the MIT License.

Acknowledgments

  • Ben Morgan who has done a great job maintaining paranoia
  • Ryan Bigg, the original author of paranoia (and many things), as a simpler replacement of acts_as_paranoid
  • All paranoia users and contributors

More Repositories

1

fzy

πŸ” A simple, fast fuzzy finder for the terminal
C
2,821
star
2

vernier

πŸ“ next generation CRuby profiler
Ruby
514
star
3

curl-to-ruby

⬇️ Convert a curl command into ruby's net/http
JavaScript
237
star
4

fzy.js

A javascript port of fzy's scoring algorithm. As seen on GitHub.com!
JavaScript
147
star
5

pub_grub

A ruby port of the PubGrub version solver
Ruby
146
star
6

meh

πŸŽ‘ a simple, minimalist, super fast image viewer using raw XLib
C
142
star
7

actionview_precompiler

Precompiles ActionView templates at app boot for Rails 6+
Ruby
137
star
8

dkim

βœ‰οΈ Pure Ruby DKIM signature library with Rails integration
Ruby
89
star
9

execjs-fastnode

⚑ A faster Node.JS integration for Ruby/Rails ExecJS
Ruby
51
star
10

vecx

πŸ‘Ύ vecx vectrex emulator - sdl port
C
50
star
11

sheap

Interactive Ruby Heap Snapshot analyzer
Ruby
40
star
12

modelh

Replacement Model M controller board
HTML
39
star
13

rapidjson-ruby

A fast JSON library for Ruby
Ruby
34
star
14

tvart

Upload art to a Samsung "The Frame" TV
JavaScript
28
star
15

clouddns

☁️ A ruby DSL for managing DNS
Ruby
20
star
16

piink

🐷 Raspberry Pi e-paper display in Ruby
Ruby
19
star
17

fzy.rb

A ruby port of fzy's scoring algorithm.
C
12
star
18

ttytest

An acceptance test framework for interactive console applications
Ruby
11
star
19

fast_sqlite

Speeds up tests using sqlite ⚑
Ruby
10
star
20

sdlmap

🌍 A SDL + libcurl OpenStreetMap viewer
C++
10
star
21

dotfiles

my configs
Lua
10
star
22

ESPresso

β˜•οΈ Turning code into coffee: ESP32 espresso machine controller
C
9
star
23

bundler-explain

Gives better explanations of conflicts when running bundle update
Ruby
8
star
24

github_fast_changelog

Generate CHANGELOGs using github's v4 GraphQL API
Ruby
8
star
25

jokerss

🀑 Ruby/Rails feed reader designed for self-hosting
Ruby
8
star
26

roaring-ruby

Roaring compressed bitmaps for Ruby
Ruby
7
star
27

plugin.video.gomtv.net

πŸ“Ί GOMtv.net video addon for XBMC
Python
7
star
28

bccovidpod

BC COVID-19 Updates from Dr. Bonnie Henry - Converted to podcast
HTML
6
star
29

galette

πŸ’ŽπŸ₯ž Experimental dependency resolution algorithm for ruby gems
Ruby
5
star
30

arcana

πŸ§™β€β™‚οΈ pure-ruby file/libmagic implementation. VERY INCOMPLETE
Ruby
5
star
31

dynamic_locals

A Ruby to Ruby transpiler allowing dynamic local variables
Ruby
5
star
32

fullwidth

πŸ‡―πŸ‡΅ Convert ASCII to equivalent ο½†ο½•ο½Œο½Œο½—ο½‰ο½„ο½”ο½ˆ characters
Ruby
5
star
33

json_escape

Ruby
5
star
34

hawthos

C
4
star
35

mimemagic_stub

Ruby
4
star
36

c2dm-ruby

ruby interface to google android's Cloud to Device Messaging service
4
star
37

mpvsrv

Web interface and API for remote control of mpv
JavaScript
4
star
38

qrcli

Generates QR codes on the command line using ANSI terminal colors
Ruby
3
star
39

rds_slow_log

Ruby script to dump the slow query log from the mysql.slow_log on Amazon RDS instances
Ruby
3
star
40

noticat

Simple notification daemon and clock for dwm
3
star
41

levelfind

list directories and files in a level order traversal
C++
3
star
42

snek

Ruby
3
star
43

giff

πŸ’Ž Compare two .gem files
Ruby
2
star
44

st

My st configuration
C
2
star
45

bundler_wtf

Ruby
2
star
46

fzy-fpm-cookery

Shell
2
star
47

cruby_crash_info

Ruby
2
star
48

fzy-demo

Demo page for fzy.js
JavaScript
2
star
49

hawthjit

An experimental pure-Ruby JIT compiler
Ruby
2
star
50

circdraw

🎨 draws circles on things
C++
2
star
51

homebrew-fzy

Ruby
2
star
52

asmjit-ruby

Ruby wrapper for AsmJit: a lightweight library for machine code generation
C++
2
star
53

visdiff-ruby

Compare screenshots from ruby with visdiff.com
Ruby
2
star
54

rabl_to_jbuilder

Convert rabl templates to jbuilder syntax
Ruby
2
star
55

watchmaker

⌚ Runs `make` when files are changed.
Go
2
star
56

dwm

My dwm config
C
2
star
57

hsh

TIME MACHINE A barely functional shell I wrote for university in 2009
C
2
star
58

las2heightmap

Converts LAS lidar data to a PNG heightmap (with a weird encoding)
C++
2
star
59

adventofcode2017

Ruby
2
star
60

site-example

Build system for my personal blog at https://www.johnhawthorn.com/
Ruby
2
star
61

clocksay

⏰ A dumb thing for a "smart" alarm clock
Go
2
star
62

hawthrss

Ruby
2
star
63

riama

View only the questions and answers from reddit.com/r/iama posts.
Ruby
2
star
64

hawthfrag

Simple filesystem agnostic online file defragmenter for linux
C
2
star
65

hawthtest

Ruby
1
star
66

svgcal

svgcal
HTML
1
star
67

dwmstatus

My status script for dwm. Probably not useful unless you are me. If you are me, hello!
C
1
star
68

capybara-profile

Ruby
1
star
69

cool-repo

Ruby
1
star
70

hawth-rails-template

My template for creating rails applications
Ruby
1
star
71

doscat

Converts code page 437 with ANSI escapes to UTF-8
C
1
star
72

led_test_formatter

Display test runner progress on LED strips
Ruby
1
star
73

muffins

πŸͺ TIME CAPSULE: A game prototype from 2007 by @jhawthorn and @jarednorman
Python
1
star
74

magnetman

TIME CAPSULE: November 2006 72Hour Game Development Competition entry (modern rewrite)
JavaScript
1
star
75

rtrace

πŸ”΅ Time capsule! A ray tracer I wrote in 2009
C++
1
star
76

uncletbag

🍌 Search for quotes from arrested development
JavaScript
1
star
77

jhawthorn

1
star
78

crowfriend

Yet another twitter <-> IRC bridge. For openhack!
Ruby
1
star
79

rbdbg

A ptrace-based debugger written in Ruby
Ruby
1
star
80

ruby-capi

testing out publishing doxygen to gh-pages
CSS
1
star
81

git-pivotal

A composable integration between git and pivotal tracker
Ruby
1
star
82

toksay

Ruby
1
star
83

futurasky

⌚ A pebble watchface based on futuraweather powered by forecast.io
C
1
star
84

lune

🌘 An experimental alternative lua syntax
Lua
1
star
85

colbert

CSS
1
star
86

adventofcode2018

Ruby
1
star
87

quoth

1
star
88

155pod.com

HTML
1
star
89

rapidash

A minimal javascript dashboard
CoffeeScript
1
star
90

colbert-generator

Ruby
1
star
91

mpvctl

mpc, but for mpv
Ruby
1
star
92

garagedoo.rb

Ruby
1
star
93

untappd-slack

A slack app which posts untappd check-ins
JavaScript
1
star
94

cowefficient

Test of Ruby's CoW behaviour
Ruby
1
star
95

v60_drain

A drainage stand for a Hario v60
OpenSCAD
1
star