ActiveRecord Mixin for Safe Destroys
If it is a bug please open an issue on GitHub.
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 true
user.destroyed_at
# => Be careful when destroying models that have dependent: :destroy. This will delete, not destroy, the associated model if
said model does not include DestroyedAt.
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 true
user.destroyed_at
# => nilWhen 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.
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 { 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.
We are very thankful for the many contributors
This gem follows Semantic Versioning
Please do! We are always looking to improve this gem. Please see our Contribution Guidelines on how to properly submit issues and pull requests.
DockYard Inc. © 2014
No open issues yet, or sync has not completed.