diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2017-07-21 16:12:29 -0500 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2017-07-21 16:12:29 -0500 |
commit | 796f8330ad441e93590a57521ef8fb80a030fb66 (patch) | |
tree | d69396c2ac693de55aebbdc5f1e3df01e69d1fc0 | |
parent | 67606dcdf52ae7f83e42a9872fdc545b02f227a2 (diff) | |
download | rails-796f8330ad441e93590a57521ef8fb80a030fb66.tar.gz rails-796f8330ad441e93590a57521ef8fb80a030fb66.tar.bz2 rails-796f8330ad441e93590a57521ef8fb80a030fb66.zip |
Fix and test VariantsController
-rw-r--r-- | app/controllers/active_storage/variants_controller.rb | 5 | ||||
-rw-r--r-- | config/routes.rb | 10 | ||||
-rw-r--r-- | lib/active_storage/variant.rb | 6 | ||||
-rw-r--r-- | test/controllers/variants_controller.rb | 25 | ||||
-rw-r--r-- | test/test_helper.rb | 2 |
5 files changed, 36 insertions, 12 deletions
diff --git a/app/controllers/active_storage/variants_controller.rb b/app/controllers/active_storage/variants_controller.rb index 05685dca17..dde7e1458f 100644 --- a/app/controllers/active_storage/variants_controller.rb +++ b/app/controllers/active_storage/variants_controller.rb @@ -13,7 +13,10 @@ class ActiveStorage::VariantsController < ActionController::Base end def processed_variant_for(blob_key) - ActiveStorage::Variant.find_or_process_by!(blob_key: blob_key, encoded_variant_key: params[:encoded_variant_key]) + ActiveStorage::Variant.new( + ActiveStorage::Blob.find_by!(key: blob_key), + ActiveStorage::Variation.decode(params[:variation_key]) + ).processed end def disposition_param diff --git a/config/routes.rb b/config/routes.rb index ad54b178fe..bd0787180a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,13 +1,13 @@ ActiveStorage::Engine.routes.draw do get "/rails/active_storage/disk/:encoded_key/*filename" => "active_storage/disk#show", as: :rails_disk_blob - get "/rails/active_storage/variants/:encoded_blob_key/:encoded_variant_key/*filename" => "active_storage/variants#show", as: :rails_blob_variant + get "/rails/active_storage/variants/:encoded_blob_key/:variation_key/*filename" => "active_storage/variants#show", as: :rails_blob_variant post "/rails/active_storage/direct_uploads" => "active_storage/direct_uploads#create", as: :rails_direct_uploads resolve 'ActiveStorage::Variant' do |variant| - encoded_blob_key = ActiveStorage::VerifiedKeyWithExpiration.encode(variant.blob.key) - encoded_variant_key = ActiveStorage::Variant.encode_key(variant.variation) - filename = variant.blob.filename + encoded_blob_key = ActiveStorage::VerifiedKeyWithExpiration.encode(variant.blob.key) + variantion_key = ActiveStorage::Variation.encode(variant.variation) + filename = variant.blob.filename - route_for(:rails_blob_variant, encoded_blob_key, encoded_variant_key, filename) + route_for(:rails_blob_variant, encoded_blob_key, variantion_key, filename) end end diff --git a/lib/active_storage/variant.rb b/lib/active_storage/variant.rb index 8be51eba92..ba2604eccf 100644 --- a/lib/active_storage/variant.rb +++ b/lib/active_storage/variant.rb @@ -5,12 +5,6 @@ class ActiveStorage::Variant attr_reader :blob, :variation delegate :service, to: :blob - class << self - def find_or_process_by!(blob_key:, variation_key:) - new(ActiveStorage::Blob.find_by!(key: blob_key), variation: ActiveStorage::Variation.decode(variation_key)).processed - end - end - def initialize(blob, variation) @blob, @variation = blob, variation end diff --git a/test/controllers/variants_controller.rb b/test/controllers/variants_controller.rb new file mode 100644 index 0000000000..132d93a3cf --- /dev/null +++ b/test/controllers/variants_controller.rb @@ -0,0 +1,25 @@ +require "test_helper" +require "database/setup" + +require "active_storage/variants_controller" +require "active_storage/verified_key_with_expiration" + +class ActiveStorage::VariantsControllerTest < ActionController::TestCase + setup do + @blob = ActiveStorage::Blob.create_after_upload! \ + filename: "racecar.jpg", content_type: "image/jpeg", + io: File.open(File.expand_path("../../fixtures/files/racecar.jpg", __FILE__)) + + @routes = Routes + @controller = ActiveStorage::VariantsController.new + end + + test "showing variant inline" do + get :show, params: { + filename: @blob.filename, + encoded_blob_key: ActiveStorage::VerifiedKeyWithExpiration.encode(@blob.key, expires_in: 5.minutes), + variation_key: ActiveStorage::Variation.encode(resize: "100x100") } + + assert_redirected_to /racecar.jpg\?disposition=inline/ + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb index e98b6e0afc..7aa7b50bf3 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,3 +1,5 @@ +$LOAD_PATH << File.expand_path("../../app/controllers", __FILE__) + require "bundler/setup" require "active_support" require "active_support/test_case" |