diff options
Diffstat (limited to 'activestorage')
-rw-r--r-- | activestorage/.gitignore | 1 | ||||
-rw-r--r-- | activestorage/CHANGELOG.md | 5 | ||||
-rw-r--r-- | activestorage/app/models/active_storage/blob.rb | 13 | ||||
-rw-r--r-- | activestorage/app/models/active_storage/filename.rb | 8 | ||||
-rw-r--r-- | activestorage/app/models/active_storage/identification.rb | 2 | ||||
-rw-r--r-- | activestorage/app/models/active_storage/preview.rb | 2 | ||||
-rw-r--r-- | activestorage/app/models/active_storage/variation.rb | 8 | ||||
-rw-r--r-- | activestorage/package.json | 10 | ||||
-rw-r--r-- | activestorage/test/models/blob_test.rb | 39 |
9 files changed, 81 insertions, 7 deletions
diff --git a/activestorage/.gitignore b/activestorage/.gitignore index a532335bdd..29bdcc4468 100644 --- a/activestorage/.gitignore +++ b/activestorage/.gitignore @@ -1,5 +1,6 @@ .byebug_history node_modules +src test/dummy/db/*.sqlite3 test/dummy/db/*.sqlite3-journal test/dummy/log/*.log diff --git a/activestorage/CHANGELOG.md b/activestorage/CHANGELOG.md index 6354ab9924..682e35e0a8 100644 --- a/activestorage/CHANGELOG.md +++ b/activestorage/CHANGELOG.md @@ -1,3 +1,8 @@ +* Add source code to published npm package + This allows activestorage users to depend on the javascript source code + rather than the compiled code, which can produce smaller javascript bundles. + + *Richard Macklin* Please check [5-2-stable](https://github.com/rails/rails/blob/5-2-stable/activestorage/CHANGELOG.md) for previous changes. diff --git a/activestorage/app/models/active_storage/blob.rb b/activestorage/app/models/active_storage/blob.rb index 6112c57cc0..3bcd7599d0 100644 --- a/activestorage/app/models/active_storage/blob.rb +++ b/activestorage/app/models/active_storage/blob.rb @@ -14,7 +14,9 @@ # update a blob's metadata on a subsequent pass, but you should not update the key or change the uploaded file. # If you need to create a derivative or otherwise change the blob, simply create a new blob and purge the old one. class ActiveStorage::Blob < ActiveRecord::Base - include Analyzable, Identifiable, Representable + include ActiveStorage::Blob::Analyzable + include ActiveStorage::Blob::Identifiable + include ActiveStorage::Blob::Representable self.table_name = "active_storage_blobs" @@ -25,6 +27,8 @@ class ActiveStorage::Blob < ActiveRecord::Base has_many :attachments + scope :unattached, -> { left_joins(:attachments).where(ActiveStorage::Attachment.table_name => { blob_id: nil }) } + class << self # You can used the signed ID of a blob to refer to it on the client side without fear of tampering. # This is particularly helpful for direct uploads where the client-side needs to refer to the blob @@ -109,8 +113,11 @@ class ActiveStorage::Blob < ActiveRecord::Base # with users. Instead, the +service_url+ should only be exposed as a redirect from a stable, possibly authenticated URL. # Hiding the +service_url+ behind a redirect also gives you the power to change services without updating all URLs. And # it allows permanent URLs that redirect to the +service_url+ to be cached in the view. - def service_url(expires_in: service.url_expires_in, disposition: :inline, filename: self.filename) - service.url key, expires_in: expires_in, disposition: forcibly_serve_as_binary? ? :attachment : disposition, filename: filename, content_type: content_type + def service_url(expires_in: service.url_expires_in, disposition: :inline, filename: nil, **options) + filename = ActiveStorage::Filename.wrap(filename || self.filename) + + service.url key, expires_in: expires_in, filename: filename, content_type: content_type, + disposition: forcibly_serve_as_binary? ? :attachment : disposition, **options end # Returns a URL that can be used to directly upload a file for this blob on the service. This URL is intended to be diff --git a/activestorage/app/models/active_storage/filename.rb b/activestorage/app/models/active_storage/filename.rb index b9413dec95..2b8880716e 100644 --- a/activestorage/app/models/active_storage/filename.rb +++ b/activestorage/app/models/active_storage/filename.rb @@ -5,6 +5,14 @@ class ActiveStorage::Filename include Comparable + class << self + # Returns a Filename instance based on the given filename. If the filename is a Filename, it is + # returned unmodified. If it is a String, it is passed to ActiveStorage::Filename.new. + def wrap(filename) + filename.kind_of?(self) ? filename : new(filename) + end + end + def initialize(filename) @filename = filename end diff --git a/activestorage/app/models/active_storage/identification.rb b/activestorage/app/models/active_storage/identification.rb index 4f295257ae..bf1d8f5624 100644 --- a/activestorage/app/models/active_storage/identification.rb +++ b/activestorage/app/models/active_storage/identification.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require "net/http" + class ActiveStorage::Identification attr_reader :blob diff --git a/activestorage/app/models/active_storage/preview.rb b/activestorage/app/models/active_storage/preview.rb index be5053edae..45efd26214 100644 --- a/activestorage/app/models/active_storage/preview.rb +++ b/activestorage/app/models/active_storage/preview.rb @@ -24,7 +24,7 @@ # The built-in previewers rely on third-party system libraries: # # * {ffmpeg}[https://www.ffmpeg.org] -# * {mupdf}[https://mupdf.com] +# * {mupdf}[https://mupdf.com] (version 1.8 or newer) # # These libraries are not provided by Rails. You must install them yourself to use the built-in previewers. Before you # install and use third-party software, make sure you understand the licensing implications of doing so. diff --git a/activestorage/app/models/active_storage/variation.rb b/activestorage/app/models/active_storage/variation.rb index da4af62666..12e7f9f0b5 100644 --- a/activestorage/app/models/active_storage/variation.rb +++ b/activestorage/app/models/active_storage/variation.rb @@ -8,6 +8,14 @@ # # ActiveStorage::Variation.new(resize: "100x100", monochrome: true, trim: true, rotate: "-90") # +# You can also combine multiple transformations in one step, e.g. for center-weighted cropping: +# +# ActiveStorage::Variation.new(combine_options: { +# resize: "100x100^", +# gravity: "center", +# crop: "100x100+0+0", +# }) +# # A list of all possible transformations is available at https://www.imagemagick.org/script/mogrify.php. class ActiveStorage::Variation attr_reader :transformations diff --git a/activestorage/package.json b/activestorage/package.json index ec77dc391d..d23f8b179e 100644 --- a/activestorage/package.json +++ b/activestorage/package.json @@ -4,7 +4,8 @@ "description": "Attach cloud and local files in Rails applications", "main": "app/assets/javascripts/activestorage.js", "files": [ - "app/assets/javascripts/*.js" + "app/assets/javascripts/*.js", + "src/*.js" ], "homepage": "http://rubyonrails.org/", "repository": { @@ -16,18 +17,21 @@ }, "author": "Javan Makhmali <javan@javan.us>", "license": "MIT", + "dependencies": { + "spark-md5": "^3.0.0" + }, "devDependencies": { "babel-core": "^6.25.0", "babel-loader": "^7.1.1", "babel-preset-env": "^1.6.0", "eslint": "^4.3.0", "eslint-plugin-import": "^2.7.0", - "spark-md5": "^3.0.0", "webpack": "^3.4.0" }, "scripts": { "prebuild": "yarn lint", "build": "webpack -p", - "lint": "eslint app/javascript" + "lint": "eslint app/javascript", + "prepublishOnly": "rm -rf src && cp -R app/javascript/activestorage src" } } diff --git a/activestorage/test/models/blob_test.rb b/activestorage/test/models/blob_test.rb index 664939dfa7..779e47ffb6 100644 --- a/activestorage/test/models/blob_test.rb +++ b/activestorage/test/models/blob_test.rb @@ -2,8 +2,28 @@ require "test_helper" require "database/setup" +require "active_support/testing/method_call_assertions" class ActiveStorage::BlobTest < ActiveSupport::TestCase + include ActiveSupport::Testing::MethodCallAssertions + + test ".unattached scope returns not attached blobs" do + class UserWithHasOneAttachedDependentFalse < User + has_one_attached :avatar, dependent: false + end + + ActiveStorage::Blob.delete_all + blob_1 = create_blob filename: "funky.jpg" + blob_2 = create_blob filename: "town.jpg" + + user = UserWithHasOneAttachedDependentFalse.create! + user.avatar.attach blob_1 + + assert_equal [blob_2], ActiveStorage::Blob.unattached + user.destroy + assert_equal [blob_1, blob_2].map(&:id).sort, ActiveStorage::Blob.unattached.pluck(:id).sort + end + test "create after upload sets byte size and checksum" do data = "Hello world!" blob = create_blob data: data @@ -79,6 +99,25 @@ class ActiveStorage::BlobTest < ActiveSupport::TestCase freeze_time do assert_equal expected_url_for(blob), blob.service_url assert_equal expected_url_for(blob, filename: new_filename), blob.service_url(filename: new_filename) + assert_equal expected_url_for(blob, filename: new_filename), blob.service_url(filename: "new.txt") + assert_equal expected_url_for(blob, filename: blob.filename), blob.service_url(filename: nil) + end + end + + test "urls allow for custom options" do + blob = create_blob(filename: "original.txt") + + options = [ + blob.key, + expires_in: blob.service.url_expires_in, + disposition: :inline, + content_type: blob.content_type, + filename: blob.filename, + thumb_size: "300x300", + thumb_mode: "crop" + ] + assert_called_with(blob.service, :url, options) do + blob.service_url(thumb_size: "300x300", thumb_mode: "crop") end end |