Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Opengov branch #4

Open
wants to merge 115 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
115 commits
Select commit Hold shift + click to select a range
60c236e
Fixing cascading recover to recover the main model first and then its…
Aug 6, 2013
0c692d2
Remove Gemfile.lock from repository
Feb 24, 2014
9e6ee17
Resolve warning about rake/rdoctask
Feb 24, 2014
0d5956f
Move test dependencies into gemspec as development dependencies
Feb 24, 2014
d6d4699
Support activerecord >= 4.0.0
Feb 24, 2014
631ffaf
bump gem to 0.5.0
Feb 24, 2014
587074b
Update gemspec summary and description to remove dependency mention.
Feb 24, 2014
9de04ab
Add gemfile support for testing against various versions of
Feb 24, 2014
c24aefd
Fix trailing spaces on files.
Mar 7, 2014
1f45d2d
Merge pull request #3 from andremedeiros/whitespace-fix
Mar 7, 2014
f4fc7ba
Modernized the gem, and a few other tweaks.
Mar 7, 2014
5a47cdf
Support AR from 3.0 onwards.
Mar 7, 2014
bd2d0d7
Merge pull request #1 from andremedeiros/modernize-gem
Mar 7, 2014
86ecbcd
Added gemfiles to test AR 3.1 and 3.2
Mar 7, 2014
d5a831f
Make Travis test only the versions that we actually support
Mar 7, 2014
95ecde7
Lock sqlite3 version so tests run with rbx
Mar 7, 2014
e2ad638
Silence more deprecations
Mar 7, 2014
c36e3ed
Documentation improvements
Mar 7, 2014
1bb293a
Move the AR dependency back to 3.1
Mar 7, 2014
7c543aa
Merge pull request #4 from andremedeiros/master
Mar 12, 2014
96db0a7
Merge branch 'rails3.2' of github.com:goncalossilva/acts_as_paranoid …
Mar 13, 2014
48ff312
Add Zachary to the authors and change the gem homepage.
Apr 5, 2014
67c6923
Merge pull request #5 from andremedeiros/master
May 12, 2014
5a23726
Test Ruby 2.1.x [ci skip]
May 12, 2014
879c66e
Delete duplicate description content
May 12, 2014
e7da594
Use original 0.4.x ActiveRecord dependency, we shouldn't change this
May 12, 2014
b8ebaef
Preparing 0.4.x patch level release first
May 12, 2014
2e9fd78
For this release we don't need AR 4+ gemfiles
May 12, 2014
e9cf44b
Use pessimistic dependency requirement
May 12, 2014
7fb0b5c
Use gemspec for remaining dependencies
May 12, 2014
fa21bfa
Fix typo in gemspec authors
May 12, 2014
0a66132
Add André Medeiros for his help!
May 12, 2014
5a1bafd
Enable 1.8.7 for Rails 3.x
May 12, 2014
ee96785
Allow testing of AR 3.0.0 since this patch level supports all Rails 3…
May 12, 2014
6714ab9
Revert "Allow testing of AR 3.0.0 since this patch level supports all…
May 12, 2014
f13cc2f
Correction, this patch level release will only support Rails 3.2.x
May 12, 2014
546267e
Markdown syntax
May 12, 2014
b1b61c1
Rails 3.2 still supported Ruby 1.8, so we should do our best.
May 12, 2014
7b9b0d7
Make sure we use latest sqlite3 1.3.x when testing
May 12, 2014
61b26a5
Move gemspec after platform dependencies
May 12, 2014
a944af8
Move Credits below Acknowledgements [ci skip]
May 12, 2014
2fcc4c6
Update Copyright notice [ci skip]
May 12, 2014
990a9c7
Add rdoc as development dependency to let Ruby 1.8 tests run
May 12, 2014
17cf61a
Add minitest as development dependency to let tests run on Ruby 1.8
May 12, 2014
37725d8
Remove ./gemfiles since we only test one version of Rails
May 12, 2014
5e6bc34
Mark as pre-release
May 12, 2014
fbcbdd4
Master is now 0.5.x
May 12, 2014
b583c3c
Update README.markdown
Aug 28, 2013
17617a4
Merge andremedeiros/acts_as_paranoid@f77770e manually
May 12, 2014
d2748d6
Remove init.rb
Aug 11, 2014
c1e4004
Specify branch support for master, add Gonçalo to credits.
Aug 11, 2014
f53f13b
See `LICENSE`.
Aug 11, 2014
7cb5ccd
Update homepage
Aug 11, 2014
ed2a82e
Fixed minor spelling mistake
binarycleric Aug 21, 2014
52c5166
Merge pull request #15 from binarycleric/spelling_fixup
Aug 22, 2014
75a4662
rm 1.8 support, test against ruby-head
Aug 25, 2014
2b5fa82
Target AR 4.x
Aug 25, 2014
6f7fa03
This branch targets 4.x
Aug 25, 2014
18d9ec4
Thank you @technoweenie!! <3 <3
Aug 25, 2014
f672e87
Update repository homepage to org
Aug 25, 2014
6bb32b2
build travis
Aug 25, 2014
7057280
Initial test matrix setup for AR 4+ on travis
Aug 25, 2014
82845ba
Fix typo in version iteration of Rakefile
Aug 25, 2014
5bf262e
belongs_to_with_deleted should pass scope to belongs_to_without_deleted
yuki24 Mar 19, 2013
e796b9f
don't save when destroying a non persisted object
pelargir May 11, 2013
ec9c311
Rails 4.0.0 compatibility
byroot Feb 26, 2013
d5c17ad
Merged in @byroot's Rails 4.0.0 support, thank you! <3 <3 <3
Aug 26, 2014
c6d7c28
Add travis script for test
Aug 26, 2014
115f762
bump to beta1
Aug 26, 2014
d7929c0
result is a hash
Aug 29, 2014
9fdd810
Don't eager load logger, we no longer support 1.8.7 anyways
Aug 29, 2014
6142210
Use unscoped instead of with_default_scope
Aug 29, 2014
eef6a4f
serialized_attributes has been deprecated in rails/rails#15704
Aug 29, 2014
ddcd191
Use timestamps with null: false
Aug 29, 2014
e014094
Use minitest version compatible with other dependencies
mvz Nov 18, 2014
fd4e16e
Use #column_types instead of #type_for_attribute
mvz Nov 18, 2014
e8f7fbb
Do not mangle belongs_to result
mvz Nov 18, 2014
a8953b5
Set with_deleted option
mvz Nov 18, 2014
3bf46c2
Fix with_deleted
mvz Nov 18, 2014
3c12279
Soft destroy dependents
mvz Nov 19, 2014
421dd3d
Fix dependent recoverable associations scope
mvz Nov 19, 2014
37a9e33
Fix test to show dependent removal behavior
mvz Nov 19, 2014
6d9981e
Use destroy_fully! where needed
mvz Nov 19, 2014
97d3b00
Allow broad range of minitest versions
mvz Nov 19, 2014
9c70f8c
Munge result of belongs_to differently for AR 4.0
mvz Nov 19, 2014
01bdce8
Fix #without_paranoid_default_scope on AR 4.0
mvz Nov 19, 2014
ae6598c
Make #validate_each work with AR 4.2
mvz Nov 19, 2014
784a874
Fix tests to allow reflections hash keys to be strings
mvz Nov 19, 2014
be7e251
Fix AR 4.2 deprecation warnings in tests
mvz Nov 19, 2014
fa16de9
Update_all for delete_all recovery
Aug 6, 2014
a48781f
removed extraneous "then" from case statement
pavlos Apr 14, 2015
273e741
reimlemented Chris Chiu's changes "Added support for only propagating…
pavlos Apr 14, 2015
07bb0cd
reimplemented Chris Chiu's change "Halt execution of recovery if call…
pavlos Apr 14, 2015
7891ea6
fixed string based terminator deprecation warning
pavlos Apr 14, 2015
316e98a
reimplemented Chris Chiu's change "Fixing cascading recover to recove…
pavlos Apr 14, 2015
c2d914e
fix delete order
pavlos Jun 11, 2015
5932cfe
destroy_dependent_associations! calls destroy_fully!
pavlos Jun 11, 2015
97ef835
merge
Feb 19, 2016
c3a41d5
comply to pavlos
Feb 22, 2016
5fdff4d
Adding new soft_delete callback
skrishnegowda Jan 26, 2017
8d3e7e5
README.md changes
skrishnegowda Jan 27, 2017
458d725
Merge pull request #3 from skrishnegowda/soft-delete-additions
jpterry Feb 1, 2017
7865bae
Adding scoping to dependent relationships
Feb 5, 2017
6074cf0
Merge pull request #5 from christopherchiu/dependent_Scope
christopherchiu Feb 5, 2017
a4386f6
Added without_default_scope option
MSeal Apr 26, 2017
656fd28
Added without_deleted. Allowed for symbol options. Added more test co…
MSeal Apr 26, 2017
b871623
Added more scope tests
MSeal Apr 26, 2017
5d05fed
Removed composite key support (no specs). Consolidated some repeated …
MSeal May 11, 2017
7a0918e
Added 1.9.3 and jruby to passing builds
MSeal May 11, 2017
8afb100
Merge pull request #6 from MSeal/default_scope
MSeal May 11, 2017
ee635a1
[PLTF-693] Upgrade to V0.5.0.rc1 (#7)
robert-hromej Jul 30, 2021
6e81910
Upgrade to v0.5.0 and cherry pick bugfix commits for [PLTF-5041] (#8)
robert-hromej Aug 4, 2021
777d5a0
Fix define_callbacks for rails5 (#9)
robert-hromej Aug 9, 2021
f0b3be5
Bump up the rails version to 5.1.X (#10)
robert-hromej Nov 5, 2021
ac48a43
Fix deprecation warning using alias_method_chain (#11)
robert-hromej Nov 29, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pkg
.bundle
.DS_Store
Gemfile.lock
gemfiles/*.lock
37 changes: 34 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,37 @@
language: ruby

before_install:
- rvm get head
- gem install bundler

script: "bundle exec rake test"
sudo: required
rvm:
- 1.9.2
- 1.9.3
- jruby-19mode
- rbx-19mode
- 2.0.0
- 2.1.9
- 2.2.5
- 2.3.1
- ruby-head
- jruby
- jruby-9.1.0.0
- rbx
gemfile:
- gemfiles/active_record_40.gemfile
- gemfiles/active_record_41.gemfile
- gemfiles/active_record_42.gemfile
- gemfiles/active_record_50.gemfile
matrix:
allow_failures:
- rvm: ruby-head
- rvm: rbx
- rvm: jruby
- rvm: jruby-9.1.0.0
fast_finish: true
exclude:
- rvm: 1.9.3
gemfile: gemfiles/active_record_50.gemfile
- rvm: 2.0.0
gemfile: gemfiles/active_record_50.gemfile
- rvm: 2.1.9
gemfile: gemfiles/active_record_50.gemfile
23 changes: 8 additions & 15 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
source "http://rubygems.org"
source "https://rubygems.org"

gem "activerecord", "~>3.2"
# Older bundler has a known error which prevents bundle install on ruby 1.9
gem "bundler", ">=1.12.0"

# Development dependencies
gem "rake"
gem "activesupport", "~>3.2"

platforms :ruby do
gem "sqlite3"
end

platforms :jruby do
gem "activerecord-jdbcsqlite3-adapter"
# Development dependencies
group :development do
gem "sqlite3", :platforms => [:ruby]
gem "activerecord-jdbcsqlite3-adapter", :platforms => [:jruby]
end

group :test do
gem "minitest"
gem "autotest-growl"
end
gemspec
41 changes: 0 additions & 41 deletions Gemfile.lock

This file was deleted.

2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2010 Gonçalo Silva
Copyright (c) 2014 Zachary Scott, Gonçalo Silva, Rick Olson

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down
134 changes: 92 additions & 42 deletions README.markdown → README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
# ActsAsParanoid

A simple plugin which hides records instead of deleting them, being able to recover them.
[![Build Status](https://travis-ci.org/OpenGov/acts_as_paranoid.png?branch=opengov-master)](https://travis-ci.org/OpenGov/acts_as_paranoid)

**This branch targets Rails 3.2.** If you're working with another version, switch to the corresponding branch.
A Rails plugin to add soft delete.

## Credits
This gem can be used to hide records instead of deleting them, making them recoverable later.

This plugin was inspired by [acts_as_paranoid](http://github.com/technoweenie/acts_as_paranoid) and [acts_as_active](http://github.com/fernandoluizao/acts_as_active).
## Support

While porting it to Rails 3, I decided to apply the ideas behind those plugins to an unified solution while removing a **lot** of the complexity found in them. I eventually ended up writing a new plugin from scratch.
**This branch targets Rails 4.x. and 5.x**

If you're working with another version, switch to the corresponding branch, or require an older version of the `acts_as_paranoid` gem.

## Usage

Expand All @@ -35,12 +37,18 @@ The values shown are the defaults. While *column* can be anything (as long as it

If your column type is a `string`, you can also specify which value to use when marking an object as deleted by passing `:deleted_value` (default is "deleted"). Any records with a non-matching value in this column will be treated normally (ie: not deleted).

If your column type is a `boolean`, it is possible to specify `allow_nulls` option which is `true` by default. When set to `false`, entities that have `false` value in this column will be considered not deleted, and those which have `true` will be considered deleted. When `true` everything that has a not-null value will be considered deleted.

### Filtering

If a record is deleted by ActsAsParanoid, it won't be retrieved when accessing the database. So, `Paranoiac.all` will **not** include the deleted_records. if you want to access them, you have 2 choices:
If a record is deleted by ActsAsParanoid, it won't be retrieved when accessing the database.

So, `Paranoiac.all` will **not** include the **deleted records**.

When you want to access them, you have 2 choices:

```ruby
Paranoiac.only_deleted # retrieves the deleted records
Paranoiac.only_deleted # retrieves only the deleted records
Paranoiac.with_deleted # retrieves all records, deleted or not
```

Expand All @@ -65,7 +73,13 @@ paranoiac.destroy!
Paranoiac.delete_all!(conditions)
```

You can also permanently delete a record by calling `destroy` or `delete_all` on it **twice**. If a record was already deleted (hidden by ActsAsParanoid) and you delete it again, it will be removed from the database. Take this example:
You can also permanently delete a record by calling `destroy_fully!` on the object.

Alternatively you can permanently delete a record by calling `destroy` or `delete_all` on the object **twice**.

If a record was already deleted (hidden by `ActsAsParanoid`) and you delete it again, it will be removed from the database.

Take this example:

```ruby
p = Paranoiac.first
Expand All @@ -81,7 +95,9 @@ Recovery is easy. Just invoke `recover` on it, like this:
Paranoiac.only_deleted.where("name = ?", "not dead yet").first.recover
```

All associations marked as `:dependent => :destroy` are also recursively recovered. If you would like to disable this behavior, you can call `recover` with the `recursive` option:
All associations marked as `:dependent => :destroy` are also recursively recovered.

If you would like to disable this behavior, you can call `recover` with the `recursive` option:

```ruby
Paranoiac.only_deleted.where("name = ?", "not dead yet").first.recover(:recursive => false)
Expand All @@ -95,7 +111,13 @@ class Paranoiac < ActiveRecord::Base
end
```

By default, dependent records will be recovered if they were deleted within 2 minutes of the object upon which they depend. This restores the objects to the state before the recursive deletion without restoring other objects that were deleted earlier. The behavior is only available when both parent and dependant are using timestamp fields to mark deletion, which is the default behavior. This window can be changed with the `dependent_recovery_window` option:
By default, dependent records will be recovered if they were deleted within 2 minutes of the object upon which they depend.

This restores the objects to the state before the recursive deletion without restoring other objects that were deleted earlier.

The behavior is only available when both parent and dependant are using timestamp fields to mark deletion, which is the default behavior.

This window can be changed with the `dependent_recovery_window` option:

```ruby
class Paranoiac < ActiveRecord::Base
Expand Down Expand Up @@ -123,9 +145,9 @@ ActiveRecord's built-in uniqueness validation does not account for records delet

```ruby
class Paranoiac < ActiveRecord::Base
acts_as_paranoid
validates_as_paranoid
validates_uniqueness_of_without_deleted :name
acts_as_paranoid
validates_as_paranoid
validates_uniqueness_of_without_deleted :name
end

p1 = Paranoiac.create(:name => 'foo')
Expand All @@ -148,13 +170,15 @@ Paranoiac.with_deleted.first.deleted? #=> true

### Scopes

As you've probably guessed, `with_deleted` and `only_deleted` are scopes. You can, however, chain them freely with other scopes you might have. This
As you've probably guessed, `with_deleted` and `only_deleted` are scopes. You can, however, chain them freely with other scopes you might have.

For example:

```ruby
Paranoiac.pretty.with_deleted
```

is exactly the same as
This is exactly the same as:

```ruby
Paranoiac.with_deleted.pretty
Expand All @@ -164,8 +188,8 @@ You can work freely with scopes and it will just work:

```ruby
class Paranoiac < ActiveRecord::Base
acts_as_paranoid
scope :pretty, where(:pretty => true)
acts_as_paranoid
scope :pretty, where(:pretty => true)
end

Paranoiac.create(:pretty => true)
Expand All @@ -183,17 +207,21 @@ Paranoiac.pretty.only_deleted.count #=> 1

### Associations

Associations are also supported. From the simplest behaviors you'd expect to more nifty things like the ones mentioned previously or the usage of the `:with_deleted` option with `belongs_to`
Associations are also supported.

From the simplest behaviors you'd expect to more nifty things like the ones mentioned previously or the usage of the `:with_deleted` option with `belongs_to`

```ruby
class Parent < ActiveRecord::Base
has_many :children, :class_name => "ParanoiacChild"
has_many :children, :class_name => "ParanoiacChild"
end

class ParanoiacChild < ActiveRecord::Base
belongs_to :parent
belongs_to :parent_including_deleted, :class_name => "Parent", :with_deleted => true
# You cannot name association *_with_deleted
acts_as_paranoid
belongs_to :parent

# You may need to provide a foreign_key like this
belongs_to :parent_including_deleted, :class_name => "Parent", :foreign_key => 'parent_id', :with_deleted => true
end

parent = Parent.first
Expand All @@ -204,6 +232,32 @@ child.parent #=> nil
child.parent_including_deleted #=> Parent (it works!)
```

### Creating Custom Callbacks

Define custom callbacks in lib/acts_as_paranoid/core.rb. Depending on the :after , :before callbacks required, use set_callbacks as shown below.

```ruby
def self.extended(base)
base.define_callbacks :soft_destroy, terminator: lambda { |target, result| result == false }
end

def before_soft_destroy(method)
set_callback :soft_destroy, :before, method
end

def after_soft_destroy(method)
set_callback :soft_destroy, :after, method
end
```
usage in acts_as_paranoid class
```ruby
before_soft_destroy :peform_before_soft_destroy
after_soft_destroy :peform_after_soft__destroy
```
### Custom callbacks
- `before_soft_destroy` : This is executed before soft_destroy happens
- `before_soft_destroy` : This is executed after marking the record to be soft destroyed

## Caveats

Watch out for these caveats:
Expand All @@ -215,37 +269,33 @@ Watch out for these caveats:
- `unscoped` will return all records, deleted or not
- Assignment of `self.table_name` in a model must come before `acts_as_paranoid` or you will get SQL errors.

# Support

This gem supports the most recent versions of Rails and Ruby.

## Rails

For Rails 3.2 check the README at the [rails3.2](https://github.com/goncalossilva/rails3_acts_as_paranoid/tree/rails3.2) branch and add this to your Gemfile:

gem "acts_as_paranoid", "~>0.4.0"

For Rails 3.1 check the README at the [rails3.1](https://github.com/goncalossilva/rails3_acts_as_paranoid/tree/rails3.1) branch and add this to your Gemfile:

gem "rails3_acts_as_paranoid", "~>0.1.4"

For Rails 3.0 check the README at the [rails3.0](https://github.com/goncalossilva/rails3_acts_as_paranoid/tree/rails3.0) branch and add this to your Gemfile:

gem "rails3_acts_as_paranoid", "~>0.0.9"
# Specs
To run specs for this repository, first get bundler and install dependencies
```
bundle install
```

If it complains about sqlite3 you may need to install the sqlite3 headers. Using `gem install sqlite3` should suggest how to do this.

## Ruby
```
bundle exec rake test
```

This gem is tested on Ruby 1.9, JRuby and Rubinius (both in 1.9 mode). It *might* work fine in 1.8, but it's not officially supported.
And all the specs should be green.

# Acknowledgements

* To [Rick Olson](https://github.com/technoweenie) for creating acts_as_paranoid
* To [cheerfulstoic](https://github.com/cheerfulstoic) for adding recursive recovery
* To [Jonathan Vaught](https://github.com/gravelpup) for adding paranoid validations
* To [Geoffrey Hichborn](https://github.com/phene) for improving the overral code quality and adding support for after_commit
* To [flah00](https://github.com/flah00) for adding support for STI-based associations (with :dependent)
* To [vikramdhillon](https://github.com/vikramdhillon) for the idea and initial implementation of support for string column type
* To [Craig Walker](https://github.com/softcraft-development) for Rails 3.1 support and fixing various pending issues
* To [Charles G.](https://github.com/chuckg) for Rails 3.2 support and for making a desperately needed global code refactoring
* To [Gonçalo Silva](https://github.com/goncalossilva) for supporting this gem prior to v0.4.3
* To [Jean Boussier](https://github.com/byroot) for initial Rails 4.0.0 support
* To [Matijs van Zuijlen](https://github.com/mvz) for Rails 4.1 and 4.2 support
* To [Andrey Ponomarenko](https://github.com/sjke) for Rails 5 support

Copyright © 2010 Gonçalo Silva, released under the MIT license
See `LICENSE`.
Loading