• Stars
    star
    349
  • Rank 121,528 (Top 3 %)
  • Language
    Ruby
  • Created over 11 years ago
  • Updated over 7 years ago

Reviews

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

Repository Details

ActiveRecord Mixin for Safe Destroys

DestroyedAt

Build Status Dependency Status Code Climate

Looking for help?

If it is a bug please open an issue on GitHub.

Installation

Add the destroyed_at gem to your Gemfile

gem 'destroyed_at'

You can either mixin the modules on a case-by-case basis:

class User < ActiveRecord::Base
  include DestroyedAt
end

or make the changes globally:

class ActiveRecord::Base
  include DestroyedAt
end

Please note you will need to make a migration

Each model's table that is expected to have this behavior must have a destroyed_at column of type DateTime.

It is recommended that you add an index on the model's destroyed_at column, so that your database does not have to do a table scan for every query. Only Postgres' query indexes will be of any benefit:

CREATE INDEX ON users WHERE destroyed_at IS NULL;
CREATE INDEX ON users WHERE destroyed_at IS NOT NULL;

Usage

Allows you to "destroy" an object without deleting the record or associated records.

Destroying

Overides #destroy to set #destroyed_at to the current time on an object. The default scope of the class is then set to return objects that have not been destroyed (i.e., have nil for their destroyed_at value).

#destroyed? will be true when your model is destroyed; it will be false when your model has been restored.

class User < ActiveRecord::Base
  include DestroyedAt
end

user = User.create
user.destroy 
# => true
user.destroyed_at
# => <DateTime>

Warning: Dependent relations with destroy

Be careful when destroying models that have dependent: :destroy. This will delete, not destroy, the associated model if said model does not include DestroyedAt.

Restoring

When you'd like to "restore" a record, call the #restore method on the instance. This will set its #destroyed_at value to nil, thereby including it in the default scope of the class again.

To include this functionality on has_many through relationships, be sure to include DestroyedAt on the through model, as well as the parent model.

class User < ActiveRecord::Base
  include DestroyedAt
end

user = User.create
user.destroy
user.restore
# => true
user.destroyed_at
# => nil

Destroyed Scope

When you include the DestroyedAt module, it sets a default scope of where(destroyed_at: nil). It also defines a scope called .destroyed which removes the default scope and finds the records of a relation where.not(destroyed_at: nil). This is different than calling unscoped on the class/relation. Where unscoped will remove all scoping from a relation, .destroyed only removes the default scope generated by the DestroyedAt module, making it safe to call on relations.

Callbacks

before_restore, after_restore and around_restore callbacks are added to your model. They work similarly to the before_destroy, after_destroy and around_restore callbacks.

class User < ActiveRecord::Base
  before_restore :before_restore_action
  after_restore  :after_restore_action
  around_restore :around_restore_action
  
  private
  
  def before_restore_action
    ...
  end
  
  def after_restore_action
    ...
  end

  def around_restore_action
    # before around
    yield # restoring...
    # after around
end

Validations

If you are using the uniqueness validator you will need to run it as:

validates :email, uniqueness: { conditions: -> { where(destroyed_at: nil) } }

Rails will by default not include default_scopes when querying for uniqueness. Rather than monkey patching the validator we believe this is the best solution.

Authors

We are very thankful for the many contributors

Versioning

This gem follows Semantic Versioning

Want to help?

Please do! We are always looking to improve this gem. Please see our Contribution Guidelines on how to properly submit issues and pull requests.

Legal

DockYard Inc. Β© 2014

@dockyard

Licensed under the MIT license

More Repositories

1

client_side_validations

Client Side Validations made easy for Ruby on Rails
Ruby
2,686
star
2

ember-validations

Validations for Ember Objects
JavaScript
832
star
3

postgres_ext

Adds support for missing PostgreSQL data types to ActiveRecord
Ruby
643
star
4

ember-easy-form

Easily build semantic forms in Ember
JavaScript
565
star
5

party_foul

Use GitHub to track your application errors!
Ruby
517
star
6

capybara-email

Test your ActionMailer and Mailer messages with Capybara
Ruby
339
star
7

postgres_ext-serializers

Ruby
324
star
8

client_side_validations-simple_form

Simple Form plugin for ClientSideValidations
JavaScript
254
star
9

ember-appkit-rails

Ember Appkit for Rails
Ruby
238
star
10

ember-suave

Make your Ember App Stylish
JavaScript
179
star
11

ember-one-way-controls

Native one way input
JavaScript
176
star
12

ember-data-route

Common teardown scenario for ember routes backed by a data model
JavaScript
120
star
13

ember-cli-i18n

Simple Internationalization support for ember-cli apps
JavaScript
112
star
14

dismissible_helpers

Ruby
96
star
15

es6_module_transpiler-rails

Transpile ES6 Modules in the Rails Asset Pipeline
JavaScript
87
star
16

capybara-extensions

Complements Capybara with additional finders and matchers.
Ruby
66
star
17

ruby-context_validations

Context Aware Validations for Rails
Ruby
65
star
18

pg_array_parser

Ruby
61
star
19

pages

Easy pages in Rails
Ruby
49
star
20

ember-skeleton

Show fast-loading temporary images in place of an eventual slow-loading image
JavaScript
41
star
21

ember-cli-proxy-fixtures

Ember CLI Proxy Fixtures
JavaScript
38
star
22

client_side_validations-mongoid

Mongoid plugin for ClientSideValidations
Ruby
28
star
23

ruby-easy_auth

Dead simple drop-in identity based Rails authentication
Ruby
28
star
24

ember-admin-bootstrap

Ember Admin with a Twitter Bootstrap Theme
CSS
25
star
25

client_side_validations-formtastic

Formtastic plugin for ClientSideValidations
Ruby
18
star
26

ember-wuphf

JavaScript
17
star
27

postgres_ext-postgis

Ruby
14
star
28

ember-cli-one-script

This addon combines your `vendor.js` and `<your-app-name>.js` into a single file called `app.js`
JavaScript
14
star
29

client_side_validations-turbolinks

Turbolinks Plugin for ClientSideValidations
Ruby
9
star
30

fixtory

Not quite fixtures, not quite factories
Ruby
9
star
31

ember-new-modules-shim

JavaScript
6
star
32

client_side_validations-backbone

Backbone plugin for ClientSideValidations
JavaScript
6
star
33

ember-cli-test-interactions

Ember acceptance test helpers.
JavaScript
3
star
34

minitest-moar

Moar Minitest Pluzsh!
Ruby
3
star
35

ruby-easy_auth-password

Password plugin for EasyAuth
Ruby
3
star
36

mail_congress

Ruby
2
star
37

ruby-easy_auth-oauth2

Ruby
1
star
38

ruby-easy_auth-twitter

Ruby
1
star
39

ruby-easy_auth-linked_in

Ruby
1
star
40

comet

Elixir
1
star