Skip to content

Commit

Permalink
gets deletion time from paranoia_column (#481)
Browse files Browse the repository at this point in the history
  • Loading branch information
sas1ni69 committed Jan 19, 2022
1 parent 3b5a1a0 commit b6b5167
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
@@ -1,5 +1,11 @@
# paranoia Changelog

## 2.5.1

* [#481](https://github.com/rubysherpas/paranoia/pull/481) Replaces hard coded `deleted_at` with `paranoia_column`.

[Hassanin Ahmed](https://github.com/sas1ni69)

## 2.5.0

* [#516](https://github.com/rubysherpas/paranoia/pull/516) Add support for ActiveRecord 7.0, drop support for EOL Ruby < 2.5 and Rails < 5.1
Expand All @@ -10,7 +16,6 @@

[Shinichi Maeshima](https://github.com/willnet)


## 2.4.3

* [#503](https://github.com/rubysherpas/paranoia/pull/503) Bump activerecord dependency for Rails 6.1
Expand Down
10 changes: 7 additions & 3 deletions lib/paranoia.rb
Expand Up @@ -129,16 +129,16 @@ def restore!(opts = {})
def get_recovery_window_range(opts)
return opts[:recovery_window_range] if opts[:recovery_window_range]
return unless opts[:recovery_window]
(deleted_at - opts[:recovery_window]..deleted_at + opts[:recovery_window])
(deletion_time - opts[:recovery_window]..deletion_time + opts[:recovery_window])
end

def within_recovery_window?(recovery_window_range)
return true unless recovery_window_range
recovery_window_range.cover?(deleted_at)
recovery_window_range.cover?(deletion_time)
end

def paranoia_destroyed?
send(paranoia_column) != paranoia_sentinel_value
deletion_time != paranoia_sentinel_value
end
alias :deleted? :paranoia_destroyed?

Expand Down Expand Up @@ -293,6 +293,10 @@ def paranoia_column
def paranoia_sentinel_value
self.class.paranoia_sentinel_value
end

def deletion_time
send(paranoia_column)
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/paranoia/version.rb
@@ -1,3 +1,3 @@
module Paranoia
VERSION = '2.5.0'.freeze
VERSION = '2.5.1'.freeze
end
16 changes: 16 additions & 0 deletions test/paranoia_test.rb
Expand Up @@ -245,6 +245,22 @@ def test_destroy_behavior_for_custom_column_models
assert_equal 1, model.class.deleted.count
end

def test_destroy_behavior_for_custom_column_models_with_recovery_options
model = CustomColumnModel.new
model.save!

assert_nil model.destroyed_at

model.destroy

assert_equal false, model.destroyed_at.nil?
assert model.paranoia_destroyed?

model.restore!(recovery_window: 2.minutes)

assert_equal 1, model.class.count
end

def test_default_sentinel_value
assert_nil ParanoidModel.paranoia_sentinel_value
end
Expand Down

0 comments on commit b6b5167

Please sign in to comment.