Skip to content

Commit 26a5d16

Browse files
Added a test with active_storage (issue ActsAsParanoid#103)
1 parent bbb1dfc commit 26a5d16

6 files changed

+88
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ Gemfile.lock
55
gemfiles/*.lock
66
.idea/
77
.ruby-version
8+
test/tmp

gemfiles/active_record_52.gemfile

+2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
source 'https://rubygems.org'
22

3+
gem 'activejob', '~> 5.2.0', require: 'active_job'
34
gem 'activerecord', '~> 5.2.0', require: 'active_record'
45
gem 'activesupport', '~> 5.2.0', require: 'active_support'
6+
gem 'activestorage', '~> 5.2.0'
57

68
# Development dependencies
79
group :development do

gemfiles/active_record_edge.gemfile

+2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
source 'https://rubygems.org'
22

33
github 'rails/rails' do
4+
gem 'activejob', require: 'active_job'
45
gem 'activerecord', require: 'active_record'
56
gem 'activesupport', require: 'active_support'
7+
gem 'activestorage'
68
end
79

810
# Development dependencies

test/fixtures/hello.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello, world!

test/test_active_storage.rb

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require 'test_helper'
2+
3+
class ParanoidActiveStorageTest < ParanoidBaseTest
4+
def create_file_blob(filename: "hello.txt", content_type: "text/plain", metadata: nil)
5+
ActiveStorage::Blob.create_after_upload! io: file_fixture(filename).open, filename: filename, content_type: content_type, metadata: metadata
6+
end
7+
8+
def test_paranoid_active_storage
9+
unless ENABLE_ACTIVE_STORAGE
10+
skip 'ActiveStorage is only available for Rails >= 5.2'
11+
end
12+
pt = ParanoidTime.create(:main_file => create_file_blob, :files => [create_file_blob, create_file_blob], :undependent_main_file => create_file_blob, :undependent_files => [create_file_blob, create_file_blob])
13+
pt.destroy
14+
end
15+
end

test/test_helper.rb

+67
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,34 @@
1010
require 'acts_as_paranoid'
1111
require 'minitest/autorun'
1212

13+
ENABLE_ACTIVE_STORAGE = ActiveRecord::VERSION::MAJOR > 5 || (ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR >= 2)
14+
if ENABLE_ACTIVE_STORAGE
15+
# load ActiveStorage
16+
require 'global_id'
17+
ActiveRecord::Base.include(GlobalID::Identification)
18+
GlobalID.app = 'ActsAsParanoid'
19+
20+
ActiveJob::Base.queue_adapter = :test
21+
22+
require 'active_support/cache'
23+
24+
require 'active_storage'
25+
require 'active_storage/attached'
26+
require 'active_storage/service/disk_service'
27+
if ActiveRecord::VERSION::MAJOR >= 6
28+
require 'active_storage/reflection'
29+
ActiveRecord::Base.include(ActiveStorage::Reflection::ActiveRecordExtensions)
30+
ActiveRecord::Reflection.singleton_class.prepend(ActiveStorage::Reflection::ReflectionExtension)
31+
ActiveRecord::Base.include(ActiveStorage::Attached::Model)
32+
else
33+
ActiveRecord::Base.extend(ActiveStorage::Attached::Macros)
34+
end
35+
$: << "#{Gem.loaded_specs['activestorage'].full_gem_path}/app/models/"
36+
Dir.glob("#{Gem.loaded_specs['activestorage'].full_gem_path}/app/models/active_storage/*").each{|f| require f}
37+
Dir.glob("#{Gem.loaded_specs['activestorage'].full_gem_path}/app/jobs/active_storage/*").each{|f| require f}
38+
ActiveStorage::Blob.service = ActiveStorage::Service::DiskService.new(root: 'test/tmp')
39+
end
40+
1341
# Silence deprecation halfway through the test
1442
I18n.enforce_available_locales = true
1543

@@ -221,6 +249,29 @@ def setup_db
221249

222250
timestamps t
223251
end
252+
253+
if ENABLE_ACTIVE_STORAGE
254+
create_table :active_storage_attachments do |t|
255+
t.string :name, :null => false
256+
t.string :record_type, :null => false
257+
t.bigint :record_id, :null => false
258+
t.bigint :blob_id, :null => false
259+
t.datetime :created_at, :null => false
260+
t.index [:blob_id], :name => "index_active_storage_attachments_on_blob_id"
261+
t.index [:record_type, :record_id, :name, :blob_id], :name => "index_active_storage_attachments_uniqueness", :unique => true
262+
end
263+
264+
create_table :active_storage_blobs do |t|
265+
t.string :key, :null => false
266+
t.string :filename, :null => false
267+
t.string :content_type
268+
t.text :metadata
269+
t.bigint :byte_size, :null => false
270+
t.string :checksum, :null => false
271+
t.datetime :created_at, :null => false
272+
t.index [:key], :name => "index_active_storage_blobs_on_key", :unique => true
273+
end
274+
end
224275
end
225276
end
226277

@@ -238,6 +289,10 @@ def teardown_db
238289
tables.each { |table| ActiveRecord::Base.connection.drop_table(table) }
239290
end
240291

292+
def clean_active_storage_attachments
293+
Dir.glob('test/tmp/*').each{|f| FileUtils.rm_r(f)}
294+
end
295+
241296
class ParanoidTime < ActiveRecord::Base
242297
acts_as_paranoid
243298

@@ -251,6 +306,13 @@ class ParanoidTime < ActiveRecord::Base
251306
has_one :has_one_not_paranoid, :dependent => :destroy
252307

253308
belongs_to :not_paranoid, :dependent => :destroy
309+
310+
if ENABLE_ACTIVE_STORAGE
311+
has_one_attached :main_file
312+
has_many_attached :files
313+
has_one_attached :undependent_main_file, :dependent => false
314+
has_many_attached :undependent_files, :dependent => false
315+
end
254316
end
255317

256318
class ParanoidBoolean < ActiveRecord::Base
@@ -436,6 +498,10 @@ class ParanoidHasManyAsParent < ActiveRecord::Base
436498
end
437499

438500
class ParanoidBaseTest < ActiveSupport::TestCase
501+
if ENABLE_ACTIVE_STORAGE
502+
self.file_fixture_path = 'test/fixtures'
503+
end
504+
439505
def setup
440506
setup_db
441507

@@ -451,6 +517,7 @@ def setup
451517

452518
def teardown
453519
teardown_db
520+
clean_active_storage_attachments
454521
end
455522

456523
def assert_empty(collection)

0 commit comments

Comments
 (0)