Skip to content

Instantly share code, notes, and snippets.

@gongo
Last active April 24, 2024 00:39
Show Gist options
  • Save gongo/f3bcff91a1773b1640ca97f9320600f4 to your computer and use it in GitHub Desktop.
Save gongo/f3bcff91a1773b1640ca97f9320600f4 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
# from https://github.com/rails/rails/blob/v7.1.3.2/guides/bug_report_templates/active_storage.rb
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
gem "rails"
gem "sqlite3", '~> 1.4'
end
require "active_record/railtie"
require "active_storage/engine"
require "tmpdir"
class TestApp < Rails::Application
config.root = __dir__
config.logger = Logger.new($stdout)
config.eager_load = false
Rails.logger = config.logger
config.active_storage.service = :local
config.active_storage.service_configurations = {
local: { root: Dir.tmpdir, service: "Disk" }
}
end
ENV["DATABASE_URL"] = "sqlite3::memory:"
Rails.application.initialize!
require ActiveStorage::Engine.root.join("db/migrate/20170806125915_create_active_storage_tables.rb").to_s
ActiveRecord::Schema.define do
CreateActiveStorageTables.new.change
create_table :users, force: true
end
class User < ActiveRecord::Base
has_one_attached :profile
end
user = User.create!
ActiveRecord::Base.transaction do
user.profile.attach(
content_type: 'text/plain',
filename: 'dummy.txt',
io: ::StringIO.new('dummy'),
)
user.profile.download # => raise ActiveStorage::FileNotFoundError
end
user.profile.download # => "dummy"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment