From 8f20624820ed0922b33fceb4013d3ff11015b366 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 23 Jul 2017 11:03:25 -0500 Subject: Switch to a single message verifier No need for this proliferation --- app/models/active_storage/variation.rb | 6 ++---- app/models/active_storage/verified_key_with_expiration.rb | 6 ++---- lib/active_storage.rb | 2 ++ lib/active_storage/engine.rb | 9 ++------- test/test_helper.rb | 1 + 5 files changed, 9 insertions(+), 15 deletions(-) diff --git a/app/models/active_storage/variation.rb b/app/models/active_storage/variation.rb index f7c81bb99a..b37397fcad 100644 --- a/app/models/active_storage/variation.rb +++ b/app/models/active_storage/variation.rb @@ -2,17 +2,15 @@ require "active_support/core_ext/object/inclusion" # A set of transformations that can be applied to a blob to create a variant. class ActiveStorage::Variation - class_attribute :verifier - attr_reader :transformations class << self def decode(key) - new verifier.verify(key) + new ActiveStorage.verifier.verify(key) end def encode(transformations) - verifier.generate(transformations) + ActiveStorage.verifier.generate(transformations) end end diff --git a/app/models/active_storage/verified_key_with_expiration.rb b/app/models/active_storage/verified_key_with_expiration.rb index 4a46483db5..5cb07c6988 100644 --- a/app/models/active_storage/verified_key_with_expiration.rb +++ b/app/models/active_storage/verified_key_with_expiration.rb @@ -1,13 +1,11 @@ class ActiveStorage::VerifiedKeyWithExpiration - class_attribute :verifier - class << self def encode(key, expires_in: nil) - verifier.generate([ key, expires_at(expires_in) ]) + ActiveStorage.verifier.generate([ key, expires_at(expires_in) ]) end def decode(encoded_key) - key, expires_at = verifier.verified(encoded_key) + key, expires_at = ActiveStorage.verifier.verified(encoded_key) key if key && fresh?(expires_at) end diff --git a/lib/active_storage.rb b/lib/active_storage.rb index 164525653b..4032fd59a7 100644 --- a/lib/active_storage.rb +++ b/lib/active_storage.rb @@ -6,4 +6,6 @@ module ActiveStorage autoload :Blob autoload :Service + + mattr_accessor :verifier end diff --git a/lib/active_storage/engine.rb b/lib/active_storage/engine.rb index cf21a055be..95ed021ce0 100644 --- a/lib/active_storage/engine.rb +++ b/lib/active_storage/engine.rb @@ -22,14 +22,9 @@ module ActiveStorage end end - initializer "active_storage.verifiers" do - require "active_storage/verified_key_with_expiration" - require "active_storage/variation" - + initializer "active_storage.verifier" do config.after_initialize do |app| - ActiveStorage::VerifiedKeyWithExpiration.verifier = \ - ActiveStorage::Variation.verifier = \ - Rails.application.message_verifier("ActiveStorage") + ActiveStorage.verifier = Rails.application.message_verifier("ActiveStorage") end end diff --git a/test/test_helper.rb b/test/test_helper.rb index a92c03cf7f..1d9737c4a4 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -33,6 +33,7 @@ ActiveStorage::VerifiedKeyWithExpiration.verifier = ActiveSupport::MessageVerifi require "active_storage/variation" ActiveStorage::Variation.verifier = ActiveSupport::MessageVerifier.new("Testing") +ActiveStorage.verifier = ActiveSupport::MessageVerifier.new("Testing") class ActiveSupport::TestCase private -- cgit v1.2.3