aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2017-07-01 12:47:13 +0200
committerDavid Heinemeier Hansson <david@loudthinking.com>2017-07-01 12:47:29 +0200
commit182445e1b4b2e12542457ba32f255a0cc2f01910 (patch)
tree9287944c1431894bcb13c3ce71858edadf3761f4
parenta239abb7fcaea4c271ea4bef031eda158d7cf8ad (diff)
downloadrails-182445e1b4b2e12542457ba32f255a0cc2f01910.tar.gz
rails-182445e1b4b2e12542457ba32f255a0cc2f01910.tar.bz2
rails-182445e1b4b2e12542457ba32f255a0cc2f01910.zip
Test blobs with real db backend
-rw-r--r--Gemfile1
-rw-r--r--Gemfile.lock2
-rw-r--r--lib/active_file/blob.rb19
-rw-r--r--lib/active_file/migration.rb4
-rw-r--r--test/blob_test.rb9
-rw-r--r--test/database/setup.rb4
6 files changed, 32 insertions, 7 deletions
diff --git a/Gemfile b/Gemfile
index dbddb9f913..5d3b906243 100644
--- a/Gemfile
+++ b/Gemfile
@@ -4,3 +4,4 @@ gemspec
gem 'rake'
gem 'byebug'
+gem 'sqlite3' \ No newline at end of file
diff --git a/Gemfile.lock b/Gemfile.lock
index c990ec9a72..d7dc3e105d 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -31,6 +31,7 @@ GEM
i18n (0.8.4)
minitest (5.10.2)
rake (12.0.0)
+ sqlite3 (1.3.13)
thread_safe (0.3.6)
tzinfo (1.2.3)
thread_safe (~> 0.1)
@@ -43,6 +44,7 @@ DEPENDENCIES
bundler (~> 1.15)
byebug
rake
+ sqlite3
BUNDLED WITH
1.15.1
diff --git a/lib/active_file/blob.rb b/lib/active_file/blob.rb
index 817617ecaf..75e606b68b 100644
--- a/lib/active_file/blob.rb
+++ b/lib/active_file/blob.rb
@@ -18,8 +18,8 @@ class ActiveFile::Blob < ActiveRecord::Base
def build_after_upload(data:, filename:, content_type: nil, metadata: nil)
new.tap do |blob|
blob.filename = name
- blob.content_type = Marcel::MimeType.for(data, name: name, declared_type: content_type)
- blob.data = data
+ blob.content_type = content_type # Marcel::MimeType.for(data, name: name, declared_type: content_type)
+ blob.upload data
end
end
@@ -28,14 +28,27 @@ class ActiveFile::Blob < ActiveRecord::Base
end
end
+ # We can't wait until the record is first saved to have a key for it
+ def key
+ self[:key] ||= self.class.generate_unique_secure_token
+ end
def filename
Filename.new(filename)
end
+ def upload(data)
+ site.upload key, data
+ end
+
+ def download
+ site.download key
+ end
+
+
def delete
- site.delete(key)
+ site.delete key
end
def purge
diff --git a/lib/active_file/migration.rb b/lib/active_file/migration.rb
index 6e5ed0c997..7a424722e0 100644
--- a/lib/active_file/migration.rb
+++ b/lib/active_file/migration.rb
@@ -1,7 +1,7 @@
-class ActiveFile::CreateBlobs < ActiveRecord::Migration[5.2]
+class ActiveFile::CreateBlobs < ActiveRecord::Migration[5.1]
def change
create_table :active_file_blobs do |t|
- t.string :token
+ t.string :key
t.string :filename
t.string :content_type
t.integer :byte_size
diff --git a/test/blob_test.rb b/test/blob_test.rb
index 9f7c14533e..ad2df51ca9 100644
--- a/test/blob_test.rb
+++ b/test/blob_test.rb
@@ -1,7 +1,12 @@
require "test_helper"
+require "database/setup"
+require "active_file/blob"
+
+ActiveFile::Blob.site = ActiveFile::Sites::DiskSite.new(File.join(Dir.tmpdir, "active_file"))
class ActiveFile::BlobTest < ActiveSupport::TestCase
- test "truth" do
- assert true
+ test "create after upload" do
+ blob = ActiveFile::Blob.create_after_upload! data: StringIO.new("Hello world!"), filename: "hello.txt", content_type: "text/plain"
+ assert_equal "Hello world!", blob.download
end
end
diff --git a/test/database/setup.rb b/test/database/setup.rb
new file mode 100644
index 0000000000..21ede8f49c
--- /dev/null
+++ b/test/database/setup.rb
@@ -0,0 +1,4 @@
+require "active_file/migration"
+
+ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
+ActiveFile::CreateBlobs.migrate(:up)