Skip to content

Instantly share code, notes, and snippets.

@Able1991
Created November 25, 2019 08:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Able1991/8291b9ba95fd5f1d1adb6d6c751932a1 to your computer and use it in GitHub Desktop.
Save Able1991/8291b9ba95fd5f1d1adb6d6c751932a1 to your computer and use it in GitHub Desktop.
Paranoia gem bug
begin
require "bundler/inline"
rescue LoadError => e
warn "Bundler version 1.10 or later is required. Please update your Bundler"
raise e
end
gemfile(true) do
source "https://rubygems.org"
gem "rails", "5.2.3" # use correct rails version
gem "sqlite3"#, "1.3.6" # use another DB if necessary
gem "paranoia"
end
require "active_record"
require "minitest/autorun"
require "logger"
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
ActiveRecord::Base.logger = Logger.new(STDOUT)
# create your tables here
ActiveRecord::Schema.define do
create_table :books, :force => true do |t|
t.integer :library_id
t.string :name
t.datetime :deleted_at
t.timestamps null: false
end
create_table :libraries, :force => true do |t|
t.string :name
t.datetime :deleted_at
t.timestamps null: false
end
end
class Library < ActiveRecord::Base
has_many :books, :dependent => :destroy, :inverse_of => :library
has_many :library_users
acts_as_paranoid
before_destroy :validate_delete
def validate_delete
self.errors.add(:base, :not_available_destroy)
throw(:abort) if self.errors.any?
false
end
end
class Book < ActiveRecord::Base
belongs_to :library
acts_as_paranoid
end
class ArticleTest < ActiveSupport::TestCase
def test_create
lib = Library.create!(:name => "Test library")
book = lib.books.create!(:name => "Test book")
lib.destroy
assert_nil(lib.reload.deleted_at, "Library must be not deleted")
assert_nil(book.reload.deleted_at, "Book must be not deleted")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment