10
10
require 'acts_as_paranoid'
11
11
require 'minitest/autorun'
12
12
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
+
13
41
# Silence deprecation halfway through the test
14
42
I18n . enforce_available_locales = true
15
43
@@ -221,6 +249,29 @@ def setup_db
221
249
222
250
timestamps t
223
251
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
224
275
end
225
276
end
226
277
@@ -238,6 +289,10 @@ def teardown_db
238
289
tables . each { |table | ActiveRecord ::Base . connection . drop_table ( table ) }
239
290
end
240
291
292
+ def clean_active_storage_attachments
293
+ Dir . glob ( 'test/tmp/*' ) . each { |f | FileUtils . rm_r ( f ) }
294
+ end
295
+
241
296
class ParanoidTime < ActiveRecord ::Base
242
297
acts_as_paranoid
243
298
@@ -251,6 +306,13 @@ class ParanoidTime < ActiveRecord::Base
251
306
has_one :has_one_not_paranoid , :dependent => :destroy
252
307
253
308
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
254
316
end
255
317
256
318
class ParanoidBoolean < ActiveRecord ::Base
@@ -436,6 +498,10 @@ class ParanoidHasManyAsParent < ActiveRecord::Base
436
498
end
437
499
438
500
class ParanoidBaseTest < ActiveSupport ::TestCase
501
+ if ENABLE_ACTIVE_STORAGE
502
+ self . file_fixture_path = 'test/fixtures'
503
+ end
504
+
439
505
def setup
440
506
setup_db
441
507
@@ -451,6 +517,7 @@ def setup
451
517
452
518
def teardown
453
519
teardown_db
520
+ clean_active_storage_attachments
454
521
end
455
522
456
523
def assert_empty ( collection )
0 commit comments