-
Notifications
You must be signed in to change notification settings - Fork 0
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
Add default recovery window option to acts as paranoid #4
base: core
Are you sure you want to change the base?
Changes from all commits
3af4d74
5736b02
23e9e1a
e120e68
da2dcf2
acee95b
64b47db
14780a0
cf512ba
de32e53
388587b
811218c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
module Paranoia | ||
@@default_sentinel_value = nil | ||
@@default_recovery_window = nil | ||
|
||
# Change default_sentinel_value in a rails initializer | ||
def self.default_sentinel_value=(val) | ||
|
@@ -12,6 +13,14 @@ def self.default_sentinel_value | |
@@default_sentinel_value | ||
end | ||
|
||
def self.default_recovery_window=(val) | ||
@@default_recovery_window = val | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This won't work in a multithreaded environment. Use thread-local variables:
|
||
end | ||
|
||
def self.default_recovery_window | ||
@@default_recovery_window | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use thread-local variables to enable usage in threaded code (eg: within a sidekiq job):
|
||
end | ||
|
||
def self.included(klazz) | ||
klazz.extend Query | ||
klazz.extend Callbacks | ||
|
@@ -132,6 +141,7 @@ def restore!(opts = {}) | |
|
||
def get_recovery_window_range(opts) | ||
return opts[:recovery_window_range] if opts[:recovery_window_range] | ||
opts[:recovery_window] = paranoia_recovery_window if opts[:recovery_window].blank? && paranoia_recovery_window.present? | ||
return unless opts[:recovery_window] | ||
(deleted_at - opts[:recovery_window]..deleted_at + opts[:recovery_window]) | ||
end | ||
|
@@ -246,10 +256,12 @@ def self.acts_as_paranoid(options={}) | |
alias_method :destroy_without_paranoia, :destroy | ||
|
||
include Paranoia | ||
class_attribute :paranoia_column, :paranoia_sentinel_value | ||
class_attribute :paranoia_column, :paranoia_sentinel_value, :paranoia_recovery_window | ||
|
||
self.paranoia_column = (options[:column] || :deleted_at).to_s | ||
self.paranoia_sentinel_value = options.fetch(:sentinel_value) { Paranoia.default_sentinel_value } | ||
self.paranoia_recovery_window = options.fetch(:recovery_window) { Paranoia.default_recovery_window } | ||
|
||
def self.paranoia_scope | ||
where(paranoia_column => paranoia_sentinel_value) | ||
end | ||
|
@@ -290,6 +302,10 @@ def paranoia_column | |
def paranoia_sentinel_value | ||
self.class.paranoia_sentinel_value | ||
end | ||
|
||
def paranoia_recovery_window | ||
self.class.paranoia_recovery_window | ||
end | ||
end | ||
end | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does
lib/paranoia.rb
ever get run in a thread? If so, the use of class variables will not work. Each thread can independently set the class variable, but they all share the same class variable.In a multithreaded environment, such as in sidekiq, use thread-local variables:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I understand this concern. My thoughts:
This is following convention in the existing gem (class var getter/setter) https://github.com/rubysherpas/paranoia/blob/core/lib/paranoia.rb
We are hoping to get this merged upstream (hence wanting to follow their convention)
The class variables are only ever set in a rails initializer.
In practice these variables are more than thread safe — they are just "hooks" to give gem users the ability to set pseudo-constants for the gem once, when the app loads.
But in theory, you are 100% right, it is not thread-safe if someone chose to dynamically change these values. I personally can't imagine it ever being an issue. If you feel strongly about it we could re-strategize about what to do re: upstream.