Skip to content

Commit

Permalink
Merge branch 'core' into core
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieujobin authored Mar 23, 2022
2 parents 968591d + cd60ab2 commit fa36a63
Show file tree
Hide file tree
Showing 10 changed files with 303 additions and 76 deletions.
61 changes: 61 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.
# This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
# For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby

name: build

on: [push, pull_request]

jobs:
test:
runs-on: ubuntu-20.04
strategy:
fail-fast: false
matrix:
ruby:
- 3.1
- '3.0'
- 2.7
- 2.6
- 2.5
# - jruby-9.2.19.0
# - jruby-9.3.1.0
rails:
- '~> 5.1.0'
- '~> 5.2.0'
- '~> 6.0.0'
- '~> 6.1.0'
- '~> 7.0.0'
- 'edge'
exclude:
# Rails edge is now 7.x and requires ruby 2.7
- rails: 'edge'
ruby: 2.6
- rails: 'edge'
ruby: 2.5
- rails: '~> 7.0.0'
ruby: 2.6
- rails: '~> 7.0.0'
ruby: 2.5
# Legacy Rails with newer rubies
- rails: '~> 5.1.0'
ruby: '3.0'
- rails: '~> 5.2.0'
ruby: '3.0'
- rails: '~> 5.1.0'
ruby: 3.1
- rails: '~> 5.2.0'
ruby: 3.1

env:
RAILS: ${{ matrix.rails }}
steps:
- uses: actions/checkout@v2
- uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
- run: bundle exec rake
39 changes: 0 additions & 39 deletions .travis.yml

This file was deleted.

48 changes: 48 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,53 @@
# paranoia Changelog

## 2.5.3

* [#532](https://github.com/rubysherpas/paranoia/pull/532) Fix: correct bug when sentinel_value is not a timestamp
[Hassanin Ahmed](https://github.com/sas1ni69)
* [#531](https://github.com/rubysherpas/paranoia/pull/531) Added test case to reproduce bug introduce in v2.5.1
[Sherif Elkassaby](https://github.com/sherif-nedap)
* [#529](https://github.com/rubysherpas/paranoia/pull/529) Fix: Do not define a RSpec matcher when RSpec isn't present
[Sebastian Welther](https://github.com/swelther)

## 2.5.2

* [#526](https://github.com/rubysherpas/paranoia/pull/526) Do not include tests files in packaged gem

[Jason Fleetwood-Boldt](https://github.com/jasonfb)
* [#492](https://github.com/rubysherpas/paranoia/pull/492) Warn if acts_as_paranoid is called more than once on the same model

[Ignatius Reza](https://github.com/ignatiusreza)

## 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
adding support for Rails 7

[Mathieu Jobin](https://github.com/mathieujobin)
* [#515](https://github.com/rubysherpas/paranoia/pull/515) Switch from Travis CI to GitHub Actions

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

## 2.4.3

* [#503](https://github.com/rubysherpas/paranoia/pull/503) Bump activerecord dependency for Rails 6.1

[Jörg Schiller](https://github.com/joergschiller)

* [#483](https://github.com/rubysherpas/paranoia/pull/483) Update JRuby version to 9.2.8.0 + remove EOL Ruby 2.2

[Uwe Kubosch](https://github.com/donv)

* [#482](https://github.com/rubysherpas/paranoia/pull/482) Fix after_commit for Rails 6

[Ashwin Hegde](https://github.com/hashwin)

## 2.4.2

* [#470](https://github.com/rubysherpas/paranoia/pull/470) Add support for ActiveRecord 6.0
Expand Down
14 changes: 8 additions & 6 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ platforms :jruby do
gem 'activerecord-jdbcsqlite3-adapter'
end

platforms :rbx do
gem 'rubinius-developer_tools'
gem 'rubysl', '~> 2.0'
gem 'rubysl-test-unit'
if RUBY_ENGINE == 'rbx'
platforms :rbx do
gem 'rubinius-developer_tools'
gem 'rubysl', '~> 2.0'
gem 'rubysl-test-unit'
end
end

rails = ENV['RAILS'] || '~> 5.2.0'
rails = ENV['RAILS'] || '~> 6.0.4'

if rails == 'master'
if rails == 'edge'
gem 'rails', github: 'rails/rails'
else
gem 'rails', rails
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[![Gem Version](https://badge.fury.io/rb/paranoia.svg)](https://badge.fury.io/rb/paranoia)
[![build](https://github.com/rubysherpas/paranoia/actions/workflows/build.yml/badge.svg)](https://github.com/rubysherpas/paranoia/actions/workflows/build.yml)

**Notice:**

`paranoia` has some surprising behaviour (like overriding ActiveRecord's `delete` and `destroy`) and is not recommended for new projects. See [`discard`'s README](https://github.com/jhawthorn/discard#why-not-paranoia-or-acts_as_paranoid) for more details.
Expand Down
38 changes: 31 additions & 7 deletions lib/paranoia.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def restore(id_or_ids, opts = {})
end

def paranoia_destroy
transaction do
with_transaction_returning_status do
result = run_callbacks(:destroy) do
@_disable_counter_cache = deleted?
result = paranoia_delete
Expand All @@ -69,6 +69,7 @@ def paranoia_destroy
next unless send(association.reflection.name)
association.decrement_counters
end
@_trigger_destroy_callback = true
@_disable_counter_cache = false
result
end
Expand All @@ -83,6 +84,10 @@ def paranoia_destroy!
raise(ActiveRecord::RecordNotDestroyed.new("Failed to destroy the record", self))
end

def trigger_transactional_callbacks?
super || @_trigger_destroy_callback && paranoia_destroyed?
end

def paranoia_delete
raise ActiveRecord::ReadOnlyRecord, "#{self.class} is marked as readonly" if readonly?
if persisted?
Expand Down Expand Up @@ -126,21 +131,21 @@ 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
paranoia_column_value != paranoia_sentinel_value
end
alias :deleted? :paranoia_destroyed?

def really_destroy!
transaction do
with_transaction_returning_status do
run_callbacks(:real_destroy) do
@_disable_counter_cache = paranoia_destroyed?
dependent_reflections = self.class.reflections.select do |name, reflection|
Expand Down Expand Up @@ -227,13 +232,24 @@ def restore_associated_records(recovery_window_range = nil)
end
end

clear_association_cache if destroyed_associations.present?
if ActiveRecord.version.to_s > '7'
# Method deleted in https://github.com/rails/rails/commit/dd5886d00a2d5f31ccf504c391aad93deb014eb8
@association_cache.clear if persisted? && destroyed_associations.present?
else
clear_association_cache if destroyed_associations.present?
end
end
end

ActiveSupport.on_load(:active_record) do
class ActiveRecord::Base
def self.acts_as_paranoid(options={})
if included_modules.include?(Paranoia)
puts "[WARN] #{self.name} is calling acts_as_paranoid more than once!"

return
end

define_model_callbacks :restore, :real_destroy

alias_method :really_destroyed?, :destroyed?
Expand Down Expand Up @@ -282,9 +298,17 @@ def paranoia_column
self.class.paranoia_column
end

def paranoia_column_value
send(paranoia_column)
end

def paranoia_sentinel_value
self.class.paranoia_sentinel_value
end

def deletion_time
paranoia_column_value.acts_like?(:time) ? paranoia_column_value : deleted_at
end
end
end

Expand Down Expand Up @@ -313,7 +337,7 @@ class AssociationNotSoftDestroyedValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
# if association is soft destroyed, add an error
if value.present? && value.paranoia_destroyed?
record.errors[attribute] << 'has been soft-deleted'
record.errors.add(attribute, 'has been soft-deleted')
end
end
end
Expand Down
37 changes: 20 additions & 17 deletions lib/paranoia/rspec.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
require 'rspec/expectations'
if defined?(RSpec)
require 'rspec/expectations'

# Validate the subject's class did call "acts_as_paranoid"
RSpec::Matchers.define :act_as_paranoid do
match { |subject| subject.class.ancestors.include?(Paranoia) }
# Validate the subject's class did call "acts_as_paranoid"
RSpec::Matchers.define :act_as_paranoid do
match { |subject| subject.class.ancestors.include?(Paranoia) }

failure_message_proc = lambda do
"expected #{subject.class} to use `acts_as_paranoid`"
end
failure_message_proc = lambda do
"expected #{subject.class} to use `acts_as_paranoid`"
end

failure_message_when_negated_proc = lambda do
"expected #{subject.class} not to use `acts_as_paranoid`"
end
failure_message_when_negated_proc = lambda do
"expected #{subject.class} not to use `acts_as_paranoid`"
end

if respond_to?(:failure_message_when_negated)
failure_message(&failure_message_proc)
failure_message_when_negated(&failure_message_when_negated_proc)
else
# RSpec 2 compatibility:
failure_message_for_should(&failure_message_proc)
failure_message_for_should_not(&failure_message_when_negated_proc)
if respond_to?(:failure_message_when_negated)
failure_message(&failure_message_proc)
failure_message_when_negated(&failure_message_when_negated_proc)
else
# RSpec 2 compatibility:
failure_message_for_should(&failure_message_proc)
failure_message_for_should_not(&failure_message_when_negated_proc)
end
end

end
2 changes: 1 addition & 1 deletion lib/paranoia/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Paranoia
VERSION = '2.4.2'.freeze
VERSION = '2.5.3'.freeze
end
13 changes: 9 additions & 4 deletions paranoia.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Gem::Specification.new do |s|
s.license = 'MIT'
s.summary = "Paranoia is a re-implementation of acts_as_paranoid for Rails 3, 4, and 5, using much, much, much less code."
s.description = <<-DSC
Paranoia is a re-implementation of acts_as_paranoid for Rails 3, 4, and 5,
Paranoia is a re-implementation of acts_as_paranoid for Rails 4, 5, 6, and 7,
using much, much, much less code. You would use either plugin / gem if you
wished that when you called destroy on an Active Record object that it
didn't actually destroy it, but just "hid" the record. Paranoia does this
Expand All @@ -22,14 +22,19 @@ Gem::Specification.new do |s|

s.required_rubygems_version = ">= 1.3.6"

s.required_ruby_version = '>= 2.0'
s.required_ruby_version = '>= 2.5'

s.add_dependency 'activerecord', '>= 4.0', '< 6.1'
s.add_dependency 'activerecord', '>= 5.1', '< 7.1'

s.add_development_dependency "bundler", ">= 1.0.0"
s.add_development_dependency "rake"

s.files = `git ls-files`.split("\n")

s.files = Dir.chdir(File.expand_path('..', __FILE__)) do
files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)}) }
files
end

s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
s.require_path = 'lib'
end
Loading

0 comments on commit fa36a63

Please sign in to comment.