Skip to content

Commit

Permalink
Get active storage mostly working for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jturkel committed Apr 18, 2020
1 parent 6836824 commit c25af45
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 18 deletions.
3 changes: 3 additions & 0 deletions Appraisals
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ end
appraise 'rails-5.2' do
gem 'activerecord', '5.2.3'
gem 'activesupport', '5.2.3'
gem 'activestorage', '5.2.3'
end

appraise 'rails-6.0' do
gem 'activerecord', '6.0.0'
gem 'activesupport', '6.0.0'
gem 'activestorage', '6.0.0'
end

appraise 'rails-edge' do
gem 'activerecord', github: 'rails/rails'
gem 'activesupport', github: 'rails/rails'
gem 'activestorage', github: 'rails/rails'
end
1 change: 1 addition & 0 deletions gemfiles/rails_5.2.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ source "https://rubygems.org"

gem "activerecord", "5.2.3"
gem "activesupport", "5.2.3"
gem "activestorage", "5.2.3"

gemspec path: "../"
1 change: 1 addition & 0 deletions gemfiles/rails_6.0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ source "https://rubygems.org"

gem "activerecord", "6.0.0"
gem "activesupport", "6.0.0"
gem "activestorage", "6.0.0"

gemspec path: "../"
1 change: 1 addition & 0 deletions gemfiles/rails_edge.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ source "https://rubygems.org"

gem "activerecord", git: "https://github.com/rails/rails.git"
gem "activesupport", git: "https://github.com/rails/rails.git"
gem "activestorage", git: "https://github.com/rails/rails.git"

gemspec path: "../"
1 change: 0 additions & 1 deletion goldiloader.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ Gem::Specification.new do |spec|
spec.add_dependency 'activerecord', '>= 4.2', '< 6.2'
spec.add_dependency 'activesupport', '>= 4.2', '< 6.2'

spec.add_development_dependency 'activestorage', '>= 4.2', '< 6.1'
spec.add_development_dependency 'appraisal'
spec.add_development_dependency 'benchmark-ips'
spec.add_development_dependency 'coveralls'
Expand Down
8 changes: 8 additions & 0 deletions lib/goldiloader/compatibility.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ def self.rails_4?
::ActiveRecord::VERSION::MAJOR == 4
end

def self.rails_5_2?
::ActiveRecord::VERSION::MAJOR == 5 && ::ActiveRecord::VERSION::MINOR == 2
end

def self.rails_5_2_or_greater?
::ActiveRecord::VERSION::MAJOR >= 6 || rails_5_2?
end

# See https://github.com/rails/rails/pull/32375
def self.destroyed_model_associations_eager_loadable?
!RAILS_5_2_0
Expand Down
2 changes: 1 addition & 1 deletion spec/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@

t.datetime :created_at, null: false

t.index [:record_type, :record_id, :name, :blob_id], name: "index_active_storage_attachments_uniqueness", unique: true
t.index [:record_type, :record_id, :name, :blob_id], name: 'index_active_storage_attachments_uniqueness', unique: true
t.foreign_key :active_storage_blobs, column: :blob_id
end
end
Expand Down
19 changes: 14 additions & 5 deletions spec/goldiloader/goldiloader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -767,11 +767,20 @@
end
end

describe "active storage" do
specify do
User.first!.avatar.attach(io: File.open('README.md'), filename: 'profile.jpeg', content_type: 'application/octet-stream')
User.last!.avatar.attach(io: File.open('README.md'), filename: 'profile.jpeg', content_type: 'application/octet-stream')
User.all.map(&:avatar)
if defined?(ActiveStorage)
describe "active storage" do
it "works for has_one_attached associations" do
User.first!.avatar.attach(io: File.open('README.md'), filename: 'profile.jpeg', content_type: 'application/octet-stream')
User.last!.avatar.attach(io: File.open('README.md'), filename: 'profile.jpeg', content_type: 'application/octet-stream')
User.all.map(&:avatar).map(&:download)
end

it "works for has_many_attached associations" do
Post.find_each do |post|
post.images.attach(io: File.open('README.md'), filename: 'profile.jpeg', content_type: 'application/octet-stream')
end
Post.all.map(&:images).flat_map(&:to_a).map(&:download)
end
end
end
end
48 changes: 37 additions & 11 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,43 @@
ActiveRecord::Base.logger.level = Logger::DEBUG
ActiveRecord::Migration.verbose = false

# TODO: If rails 5.2
require 'active_storage'
require "active_storage/attached"
ActiveRecord::Base.extend(ActiveStorage::Attached::Macros)
ActiveStorage::Blob.service = ActiveStorage::Service::DiskService.new(root: Pathname("tmp/storage"))
ActiveStorage.logger = ActiveSupport::Logger.new(nil)
ActiveStorage.verifier = ActiveSupport::MessageVerifier.new("Testing")

# TODO: If Rails 6
# ActiveRecord.include(ActiveStorage::Reflection::ActiveRecordExtensions)
# ActiveRecord::Reflection.singleton_class.prepend(ActiveStorage::Reflection::ReflectionExtension)
if Goldiloader::Compatibility.rails_5_2_or_greater?
# Copy initialization from activestorage/lib/active_storage/engine.rb
require 'active_storage'
require 'active_storage/attached'
require 'active_storage/service/disk_service'

ActiveStorage.logger = ActiveRecord::Base.logger

# Stub ActiveStorage::AnalyzeJob to avoid a dependency on ActiveJob
module ActiveStorage
class AnalyzeJob
def self.perform_later(blob)
blob.analyze
end
end
end

if Goldiloader::Compatibility.rails_5_2?
ActiveRecord::Base.extend(ActiveStorage::Attached::Macros)
else
require 'active_storage/reflection'

ActiveRecord::Base.include(ActiveStorage::Attached::Model)
ActiveRecord::Base.include(ActiveStorage::Reflection::ActiveRecordExtensions)
ActiveRecord::Reflection.singleton_class.prepend(ActiveStorage::Reflection::ReflectionExtension)
end

# Add the ActiveStore engine files to the load path
active_storage_version_file = Gem.find_files('active_storage/version.rb').first
$LOAD_PATH.unshift(File.expand_path('../../../app/models', active_storage_version_file))

require 'active_storage/attachment'
require 'active_storage/blob'
require 'active_storage/filename'

ActiveStorage::Blob.service = ActiveStorage::Service::DiskService.new(root: Pathname('tmp/storage'))
end

db_adapter = ENV.fetch('ADAPTER', 'sqlite3')
db_config = YAML.safe_load(File.read('spec/db/database.yml'))
Expand Down

0 comments on commit c25af45

Please sign in to comment.