Skip to content

Instantly share code, notes, and snippets.

@willnet
Created September 3, 2024 06:55
Show Gist options
  • Save willnet/e4621076bbaa1a29efda6f74f5343e0f to your computer and use it in GitHub Desktop.
Save willnet/e4621076bbaa1a29efda6f74f5343e0f to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
gem "rails", "7.0.8.4"
# If you want to test against edge Rails replace the previous line with this:
# gem "rails", github: "rails/rails", branch: "main"
gem "shrine", "3.6.0"
gem "sqlite3", "~> 1.4"
gem "save_chain_inspector"
gem 'debug'
end
require "active_record/railtie"
require "tmpdir"
require "shrine"
require "shrine/storage/file_system"
Shrine.storages = {
cache: Shrine::Storage::FileSystem.new("public", prefix: "uploads/cache"), # temporary
store: Shrine::Storage::FileSystem.new("public", prefix: "uploads"), # permanent
}
Shrine.plugin :activerecord
Shrine.plugin :cached_attachment_data
Shrine.plugin :restore_cached_data
class TestApp < Rails::Application
config.load_defaults Rails::VERSION::STRING.to_f
config.root = __dir__
config.hosts << "example.org"
config.eager_load = false
config.session_store :cookie_store, key: "cookie_store_key"
config.secret_key_base = "secret_key_base"
config.logger = Logger.new($stdout)
Rails.logger = config.logger
end
ENV["DATABASE_URL"] = "sqlite3::memory:"
Rails.application.initialize!
ActiveRecord::Schema.define do
create_table :users, force: true do |t|
t.text :image_data
end
end
class ImageUploader < Shrine
end
class User < ActiveRecord::Base
include ImageUploader::Attachment(:image)
end
require "minitest/autorun"
class BugTest < Minitest::Test
def test_upload_and_download
user = User.new
tempfile = Tempfile.new
tempfile.write("dummy")
tempfile.rewind
File.open(tempfile.path) do |file|
user.image = file
end
user.image.metadata['filename'] = 'original_filename!'
SaveChainInspector.start do
user.save!
end
assert_equal "dummy", user.image.download.read
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment