aboutsummaryrefslogtreecommitdiffstats
path: root/activestorage
diff options
context:
space:
mode:
Diffstat (limited to 'activestorage')
-rw-r--r--activestorage/README.md6
-rw-r--r--activestorage/db/migrate/20170806125915_create_active_storage_tables.rb (renamed from activestorage/lib/active_storage/migration.rb)2
-rw-r--r--activestorage/lib/active_storage/engine.rb31
-rw-r--r--activestorage/lib/active_storage/service/azure_storage_service.rb5
-rw-r--r--activestorage/lib/tasks/activestorage.rake11
-rw-r--r--activestorage/package.json2
-rw-r--r--activestorage/test/database/setup.rb3
-rw-r--r--activestorage/test/service/azure_storage_service_test.rb9
-rw-r--r--activestorage/test/template/image_tag_test.rb40
-rw-r--r--activestorage/test/test_helper.rb4
10 files changed, 78 insertions, 35 deletions
diff --git a/activestorage/README.md b/activestorage/README.md
index 06594a3ecb..957adc05c3 100644
--- a/activestorage/README.md
+++ b/activestorage/README.md
@@ -2,7 +2,7 @@
Active Storage makes it simple to upload and reference files in cloud services, like Amazon S3, Google Cloud Storage or Microsoft Azure Storage and attach those files to Active Records. It also provides a disk service for testing or local deployments, but the focus is on cloud storage.
-Files can uploaded from the server to the cloud or directly from the client to the cloud.
+Files can be uploaded from the server to the cloud or directly from the client to the cloud.
Image files can further more be transformed using on-demand variants for quality, aspect ratio, size, or any other
MiniMagick supported transformation.
@@ -80,7 +80,7 @@ Variation of image attachment:
```erb
<%# Hitting the variant URL will lazy transform the original blob and then redirect to its new service location %>
-<%= image_tag url_for(user.avatar.variant(resize: "100x100")) %>
+<%= image_tag user.avatar.variant(resize: "100x100") %>
```
## Installation
@@ -119,7 +119,7 @@ Active Storage, with its included JavaScript library, supports uploading directl
| Event name | Event target | Event data (`event.detail`) | Description |
| --- | --- | --- | --- |
-| `direct-uploads:start` | `<form>` | None | A form containing files for direct upload fields was submit. |
+| `direct-uploads:start` | `<form>` | None | A form containing files for direct upload fields was submitted. |
| `direct-upload:initialize` | `<input>` | `{id, file}` | Dispatched for every file after form submission. |
| `direct-upload:start` | `<input>` | `{id, file}` | A direct upload is starting. |
| `direct-upload:before-blob-request` | `<input>` | `{id, file, xhr}` | Before making a request to your application for direct upload metadata. |
diff --git a/activestorage/lib/active_storage/migration.rb b/activestorage/db/migrate/20170806125915_create_active_storage_tables.rb
index 2e35e163cd..6eab7e0fa0 100644
--- a/activestorage/lib/active_storage/migration.rb
+++ b/activestorage/db/migrate/20170806125915_create_active_storage_tables.rb
@@ -1,4 +1,4 @@
-class ActiveStorageCreateTables < ActiveRecord::Migration[5.1]
+class CreateActiveStorageTables < ActiveRecord::Migration[5.1]
def change
create_table :active_storage_blobs do |t|
t.string :key
diff --git a/activestorage/lib/active_storage/engine.rb b/activestorage/lib/active_storage/engine.rb
index da83d3908a..d5bc70fc0c 100644
--- a/activestorage/lib/active_storage/engine.rb
+++ b/activestorage/lib/active_storage/engine.rb
@@ -34,22 +34,21 @@ module ActiveStorage
end
initializer "active_storage.services" do
- config.after_initialize do |app|
- if config_choice = app.config.active_storage.service
- config_file = Pathname.new(Rails.root.join("config/storage.yml"))
- raise("Couldn't find Active Storage configuration in #{config_file}") unless config_file.exist?
-
- require "yaml"
- require "erb"
-
- configs =
- begin
- YAML.load(ERB.new(config_file.read).result) || {}
- rescue Psych::SyntaxError => e
- raise "YAML syntax error occurred while parsing #{config_file}. " \
- "Please note that YAML must be consistently indented using spaces. Tabs are not allowed. " \
- "Error: #{e.message}"
- end
+ config.to_prepare do
+ if config_choice = Rails.configuration.active_storage.service
+ configs = Rails.configuration.active_storage.service_configurations ||= begin
+ config_file = Pathname.new(Rails.root.join("config/storage.yml"))
+ raise("Couldn't find Active Storage configuration in #{config_file}") unless config_file.exist?
+
+ require "yaml"
+ require "erb"
+
+ YAML.load(ERB.new(config_file.read).result) || {}
+ rescue Psych::SyntaxError => e
+ raise "YAML syntax error occurred while parsing #{config_file}. " \
+ "Please note that YAML must be consistently indented using spaces. Tabs are not allowed. " \
+ "Error: #{e.message}"
+ end
ActiveStorage::Blob.service =
begin
diff --git a/activestorage/lib/active_storage/service/azure_storage_service.rb b/activestorage/lib/active_storage/service/azure_storage_service.rb
index c2e1b25a43..2e0b20cce3 100644
--- a/activestorage/lib/active_storage/service/azure_storage_service.rb
+++ b/activestorage/lib/active_storage/service/azure_storage_service.rb
@@ -57,11 +57,12 @@ module ActiveStorage
end
end
- def url(key, expires_in:, disposition:, filename:)
+ def url(key, expires_in:, disposition:, filename:, content_type:)
instrument :url, key do |payload|
base_url = url_for(key)
generated_url = signer.signed_uri(URI(base_url), false, permissions: "r",
- expiry: format_expiry(expires_in), content_disposition: "#{disposition}; filename=\"#{filename}\"").to_s
+ expiry: format_expiry(expires_in), content_type: content_type,
+ content_disposition: "#{disposition}; filename=\"#{filename}\"").to_s
payload[:url] = generated_url
diff --git a/activestorage/lib/tasks/activestorage.rake b/activestorage/lib/tasks/activestorage.rake
index 1d386e67df..d9e240b141 100644
--- a/activestorage/lib/tasks/activestorage.rake
+++ b/activestorage/lib/tasks/activestorage.rake
@@ -1,13 +1,6 @@
-require "fileutils"
-
namespace :activestorage do
desc "Copy over the migration needed to the application"
- task :install do
- migration_file_path = "db/migrate/#{Time.now.utc.strftime("%Y%m%d%H%M%S")}_active_storage_create_tables.rb"
- FileUtils.mkdir_p Rails.root.join("db/migrate")
- FileUtils.cp File.expand_path("../../active_storage/migration.rb", __FILE__), Rails.root.join(migration_file_path)
- puts "Copied migration to #{migration_file_path}"
-
- puts "Now run rails db:migrate to create the tables for Active Storage"
+ task install: :environment do
+ Rake::Task["active_storage_engine:install:migrations"].invoke
end
end
diff --git a/activestorage/package.json b/activestorage/package.json
index 56d307f9d6..b481295a8d 100644
--- a/activestorage/package.json
+++ b/activestorage/package.json
@@ -1,6 +1,6 @@
{
"name": "activestorage",
- "version": "0.1.1",
+ "version": "5.2.0.alpha",
"description": "Attach cloud and local files in Rails applications",
"main": "app/assets/javascripts/activestorage.js",
"files": [
diff --git a/activestorage/test/database/setup.rb b/activestorage/test/database/setup.rb
index b12038ee68..610b927cc3 100644
--- a/activestorage/test/database/setup.rb
+++ b/activestorage/test/database/setup.rb
@@ -1,6 +1,5 @@
-require "active_storage/migration"
require_relative "create_users_migration"
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
-ActiveStorageCreateTables.migrate(:up)
+ActiveRecord::Migrator.migrate File.expand_path("../../../db/migrate", __FILE__)
ActiveStorageCreateUsers.migrate(:up)
diff --git a/activestorage/test/service/azure_storage_service_test.rb b/activestorage/test/service/azure_storage_service_test.rb
index e2be510b60..a0c6a4de73 100644
--- a/activestorage/test/service/azure_storage_service_test.rb
+++ b/activestorage/test/service/azure_storage_service_test.rb
@@ -6,8 +6,15 @@ if SERVICE_CONFIGURATIONS[:azure]
SERVICE = ActiveStorage::Service.configure(:azure, SERVICE_CONFIGURATIONS)
include ActiveStorage::Service::SharedServiceTests
- end
+ test "signed URL generation" do
+ url = @service.url(FIXTURE_KEY, expires_in: 5.minutes,
+ disposition: :inline, filename: "avatar.png", content_type: "image/png")
+
+ assert_match(/(\S+)&rsct=image%2Fpng&rscd=inline%3B\+filename%3D%22avatar.png/, url)
+ assert_match SERVICE_CONFIGURATIONS[:azure][:container], url
+ end
+ end
else
puts "Skipping Azure Storage Service tests because no Azure configuration was supplied"
end
diff --git a/activestorage/test/template/image_tag_test.rb b/activestorage/test/template/image_tag_test.rb
new file mode 100644
index 0000000000..83c95c01a1
--- /dev/null
+++ b/activestorage/test/template/image_tag_test.rb
@@ -0,0 +1,40 @@
+require "test_helper"
+require "database/setup"
+
+class User < ActiveRecord::Base
+ has_one_attached :avatar
+end
+
+class ActiveStorage::ImageTagTest < ActionView::TestCase
+ tests ActionView::Helpers::AssetTagHelper
+
+ setup do
+ @blob = create_image_blob filename: "racecar.jpg"
+ end
+
+ test "blob" do
+ assert_dom_equal %(<img alt="Racecar" src="#{polymorphic_url @blob}" />), image_tag(@blob)
+ end
+
+ test "variant" do
+ variant = @blob.variant(resize: "100x100")
+ assert_dom_equal %(<img alt="Racecar" src="#{polymorphic_url variant}" />), image_tag(variant)
+ end
+
+ test "attachment" do
+ attachment = ActiveStorage::Attachment.new(blob: @blob)
+ assert_dom_equal %(<img alt="Racecar" src="#{polymorphic_url attachment}" />), image_tag(attachment)
+ end
+
+ test "error when attachment's empty" do
+ @user = User.create!(name: "DHH")
+
+ assert_not @user.avatar.attached?
+ assert_raises(ArgumentError) { image_tag(@user.avatar) }
+ end
+
+ test "error when object can't be resolved into url" do
+ unresolvable_object = ActionView::Helpers::AssetTagHelper
+ assert_raises(ArgumentError) { image_tag(unresolvable_object) }
+ end
+end
diff --git a/activestorage/test/test_helper.rb b/activestorage/test/test_helper.rb
index 5b6d3b64c2..50cc72cbe3 100644
--- a/activestorage/test/test_helper.rb
+++ b/activestorage/test/test_helper.rb
@@ -12,6 +12,10 @@ ActiveJob::Base.logger = nil
require "active_storage"
+# Filter out Minitest backtrace while allowing backtrace from other libraries
+# to be shown.
+Minitest.backtrace_filter = Minitest::BacktraceFilter.new
+
require "yaml"
SERVICE_CONFIGURATIONS = begin
erb = ERB.new(Pathname.new(File.expand_path("../service/configurations.yml", __FILE__)).read)