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

Use ActiveSupport.on_load to correctly re-open ActiveRecord::Base #371

Merged
merged 1 commit into from
Dec 13, 2016
Merged
Changes from all commits
Commits
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
Use ActiveSupport.on_load to correctly re-open ActiveRecord::Base. #335
  • Loading branch information
iaankrynauw committed Dec 12, 2016
commit 49b9e68ee79d8a8e5fa04add1e93b9d69ccf9315
78 changes: 40 additions & 38 deletions lib/paranoia.rb
Original file line number Diff line number Diff line change
Expand Up @@ -205,56 +205,58 @@ def restore_associated_records
end
end

class ActiveRecord::Base
def self.acts_as_paranoid(options={})
alias_method :really_destroyed?, :destroyed?
alias_method :really_delete, :delete
alias_method :destroy_without_paranoia, :destroy

include Paranoia
class_attribute :paranoia_column, :paranoia_sentinel_value

self.paranoia_column = (options[:column] || :deleted_at).to_s
self.paranoia_sentinel_value = options.fetch(:sentinel_value) { Paranoia.default_sentinel_value }
def self.paranoia_scope
where(paranoia_column => paranoia_sentinel_value)
end
class << self; alias_method :without_deleted, :paranoia_scope end
ActiveSupport.on_load(:active_record) do
class ActiveRecord::Base
def self.acts_as_paranoid(options={})
alias_method :really_destroyed?, :destroyed?
alias_method :really_delete, :delete
alias_method :destroy_without_paranoia, :destroy

include Paranoia
class_attribute :paranoia_column, :paranoia_sentinel_value

self.paranoia_column = (options[:column] || :deleted_at).to_s
self.paranoia_sentinel_value = options.fetch(:sentinel_value) { Paranoia.default_sentinel_value }
def self.paranoia_scope
where(paranoia_column => paranoia_sentinel_value)
end
class << self; alias_method :without_deleted, :paranoia_scope end

unless options[:without_default_scope]
default_scope { paranoia_scope }
end
unless options[:without_default_scope]
default_scope { paranoia_scope }
end

before_restore {
self.class.notify_observers(:before_restore, self) if self.class.respond_to?(:notify_observers)
}
after_restore {
self.class.notify_observers(:after_restore, self) if self.class.respond_to?(:notify_observers)
}
end
before_restore {
self.class.notify_observers(:before_restore, self) if self.class.respond_to?(:notify_observers)
}
after_restore {
self.class.notify_observers(:after_restore, self) if self.class.respond_to?(:notify_observers)
}
end

# Please do not use this method in production.
# Pretty please.
def self.I_AM_THE_DESTROYER!
# TODO: actually implement spelling error fixes
# Please do not use this method in production.
# Pretty please.
def self.I_AM_THE_DESTROYER!
# TODO: actually implement spelling error fixes
puts %Q{
Sharon: "There should be a method called I_AM_THE_DESTROYER!"
Ryan: "What should this method do?"
Sharon: "It should fix all the spelling errors on the page!"
}
end
end

def self.paranoid? ; false ; end
def paranoid? ; self.class.paranoid? ; end
def self.paranoid? ; false ; end
def paranoid? ; self.class.paranoid? ; end

private
private

def paranoia_column
self.class.paranoia_column
end
def paranoia_column
self.class.paranoia_column
end

def paranoia_sentinel_value
self.class.paranoia_sentinel_value
def paranoia_sentinel_value
self.class.paranoia_sentinel_value
end
end
end

Expand Down