aboutsummaryrefslogtreecommitdiffstats
path: root/activestorage
diff options
context:
space:
mode:
Diffstat (limited to 'activestorage')
-rw-r--r--activestorage/README.md2
-rw-r--r--activestorage/app/controllers/active_storage/blobs_controller.rb1
-rw-r--r--activestorage/app/controllers/active_storage/variants_controller.rb1
-rw-r--r--activestorage/db/migrate/20170806125915_create_active_storage_tables.rb20
-rw-r--r--activestorage/lib/active_storage/attached/macros.rb20
-rw-r--r--activestorage/test/controllers/blobs_controller_test.rb15
6 files changed, 36 insertions, 23 deletions
diff --git a/activestorage/README.md b/activestorage/README.md
index 957adc05c3..a86a04421e 100644
--- a/activestorage/README.md
+++ b/activestorage/README.md
@@ -88,7 +88,7 @@ Variation of image attachment:
1. Run `rails activestorage:install` to create needed directories, migrations, and configuration.
2. Optional: Add `gem "aws-sdk", "~> 2"` to your Gemfile if you want to use AWS S3.
3. Optional: Add `gem "google-cloud-storage", "~> 1.3"` to your Gemfile if you want to use Google Cloud Storage.
-4. Optional: Add `gem "azure-storage"` to your Gemfile if you want to use Microsoft Azure.
+4. Optional: Add `gem "azure-storage"` to your Gemfile if you want to use Microsoft Azure Storage.
5. Optional: Add `gem "mini_magick"` to your Gemfile if you want to use variants.
## Direct uploads
diff --git a/activestorage/app/controllers/active_storage/blobs_controller.rb b/activestorage/app/controllers/active_storage/blobs_controller.rb
index 05af29f8b2..cff88bd488 100644
--- a/activestorage/app/controllers/active_storage/blobs_controller.rb
+++ b/activestorage/app/controllers/active_storage/blobs_controller.rb
@@ -5,6 +5,7 @@
class ActiveStorage::BlobsController < ActionController::Base
def show
if blob = find_signed_blob
+ expires_in 5.minutes # service_url defaults to 5 minutes
redirect_to blob.service_url(disposition: disposition_param)
else
head :not_found
diff --git a/activestorage/app/controllers/active_storage/variants_controller.rb b/activestorage/app/controllers/active_storage/variants_controller.rb
index 994c57aafd..b72b0ff7f5 100644
--- a/activestorage/app/controllers/active_storage/variants_controller.rb
+++ b/activestorage/app/controllers/active_storage/variants_controller.rb
@@ -5,6 +5,7 @@
class ActiveStorage::VariantsController < ActionController::Base
def show
if blob = find_signed_blob
+ expires_in 5.minutes # service_url defaults to 5 minutes
redirect_to ActiveStorage::Variant.new(blob, decoded_variation).processed.service_url(disposition: disposition_param)
else
head :not_found
diff --git a/activestorage/db/migrate/20170806125915_create_active_storage_tables.rb b/activestorage/db/migrate/20170806125915_create_active_storage_tables.rb
index 6eab7e0fa0..2c7e3c5bc6 100644
--- a/activestorage/db/migrate/20170806125915_create_active_storage_tables.rb
+++ b/activestorage/db/migrate/20170806125915_create_active_storage_tables.rb
@@ -1,24 +1,24 @@
class CreateActiveStorageTables < ActiveRecord::Migration[5.1]
def change
create_table :active_storage_blobs do |t|
- t.string :key
- t.string :filename
+ t.string :key, null: false
+ t.string :filename, null: false
t.string :content_type
t.text :metadata
- t.integer :byte_size
- t.string :checksum
- t.datetime :created_at
+ t.integer :byte_size, null: false
+ t.string :checksum, null: false
+ t.datetime :created_at, null: false
t.index [ :key ], unique: true
end
create_table :active_storage_attachments do |t|
- t.string :name
- t.string :record_type
- t.integer :record_id
- t.integer :blob_id
+ t.string :name, null: false
+ t.string :record_type, null: false
+ t.integer :record_id, null: false
+ t.integer :blob_id, null: false
- t.datetime :created_at
+ t.datetime :created_at, null: false
t.index :blob_id
t.index [ :record_type, :record_id, :name, :blob_id ], name: "index_active_storage_attachments_uniqueness", unique: true
diff --git a/activestorage/lib/active_storage/attached/macros.rb b/activestorage/lib/active_storage/attached/macros.rb
index 6bd0240606..eb877f10c0 100644
--- a/activestorage/lib/active_storage/attached/macros.rb
+++ b/activestorage/lib/active_storage/attached/macros.rb
@@ -22,13 +22,11 @@ module ActiveStorage
# If the +:dependent+ option isn't set, the attachment will be purged
# (i.e. destroyed) whenever the record is destroyed.
def has_one_attached(name, dependent: :purge_later)
- define_method(name) do
- if instance_variable_defined?("@active_storage_attached_#{name}")
- instance_variable_get("@active_storage_attached_#{name}")
- else
- instance_variable_set("@active_storage_attached_#{name}", ActiveStorage::Attached::One.new(name, self))
+ class_eval <<-CODE, __FILE__, __LINE__ + 1
+ def #{name}
+ @active_storage_attached_#{name} ||= ActiveStorage::Attached::One.new("#{name}", self)
end
- end
+ CODE
has_one :"#{name}_attachment", -> { where(name: name) }, class_name: "ActiveStorage::Attachment", as: :record
has_one :"#{name}_blob", through: :"#{name}_attachment", class_name: "ActiveStorage::Blob", source: :blob
@@ -63,13 +61,11 @@ module ActiveStorage
# If the +:dependent+ option isn't set, all the attachments will be purged
# (i.e. destroyed) whenever the record is destroyed.
def has_many_attached(name, dependent: :purge_later)
- define_method(name) do
- if instance_variable_defined?("@active_storage_attached_#{name}")
- instance_variable_get("@active_storage_attached_#{name}")
- else
- instance_variable_set("@active_storage_attached_#{name}", ActiveStorage::Attached::Many.new(name, self))
+ class_eval <<-CODE, __FILE__, __LINE__ + 1
+ def #{name}
+ @active_storage_attached_#{name} ||= ActiveStorage::Attached::Many.new("#{name}", self)
end
- end
+ CODE
has_many :"#{name}_attachments", -> { where(name: name) }, as: :record, class_name: "ActiveStorage::Attachment"
has_many :"#{name}_blobs", through: :"#{name}_attachments", class_name: "ActiveStorage::Blob", source: :blob
diff --git a/activestorage/test/controllers/blobs_controller_test.rb b/activestorage/test/controllers/blobs_controller_test.rb
new file mode 100644
index 0000000000..5ec353889c
--- /dev/null
+++ b/activestorage/test/controllers/blobs_controller_test.rb
@@ -0,0 +1,15 @@
+require "test_helper"
+require "database/setup"
+
+class ActiveStorage::BlobsControllerTest < ActionDispatch::IntegrationTest
+ setup do
+ @blob = create_image_blob filename: "racecar.jpg"
+ end
+
+ test "showing blob utilizes browser caching" do
+ get rails_blob_url(@blob)
+
+ assert_redirected_to(/racecar.jpg/)
+ assert_equal "max-age=300, private", @response.headers["Cache-Control"]
+ end
+end