-
-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Validator] Improve performance leveraging ActiveStorage::Blob#metada…
…ta metadata (#340)
- Loading branch information
Showing
18 changed files
with
209 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
lib/active_storage_validations/extensors/asv_blob_metadatable.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
module ActiveStorageValidations | ||
module ASVBlobMetadatable | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
def active_storage_validations_metadata | ||
metadata.dig('custom', 'active_storage_validations') | ||
end | ||
|
||
def active_storage_validations_metadata=(value) | ||
metadata['custom'] ||= {} | ||
metadata['custom']['active_storage_validations'] = value | ||
end | ||
end | ||
end | ||
end |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
test/dummy/app/models/aspect_ratio/validator/is_performance_optimized.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
# == Schema Information | ||
# | ||
# Table name: aspect_ratio_validator_is_performance_optimizeds | ||
# | ||
# id :integer not null, primary key | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# | ||
|
||
class AspectRatio::Validator::IsPerformanceOptimized < ApplicationRecord | ||
has_one_attached :is_performance_optimized | ||
has_many_attached :is_performance_optimizeds | ||
validates :is_performance_optimized, aspect_ratio: :square | ||
validates :is_performance_optimizeds, aspect_ratio: :square | ||
end |
17 changes: 17 additions & 0 deletions
17
test/dummy/app/models/dimension/validator/is_performance_optimized.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
# == Schema Information | ||
# | ||
# Table name: dimension_validator_is_performance_optimizeds | ||
# | ||
# id :integer not null, primary key | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# | ||
|
||
class Dimension::Validator::IsPerformanceOptimized < ApplicationRecord | ||
has_one_attached :is_performance_optimized | ||
has_many_attached :is_performance_optimizeds | ||
validates :is_performance_optimized, dimension: { width: 150, height: 150 } | ||
validates :is_performance_optimizeds, dimension: { width: 150, height: 150 } | ||
end |
17 changes: 17 additions & 0 deletions
17
test/dummy/app/models/duration/validator/is_performance_optimized.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
# == Schema Information | ||
# | ||
# Table name: duration_validator_is_performance_optimizeds | ||
# | ||
# id :integer not null, primary key | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# | ||
|
||
class Duration::Validator::IsPerformanceOptimized < ApplicationRecord | ||
has_one_attached :is_performance_optimized | ||
has_many_attached :is_performance_optimizeds | ||
validates :is_performance_optimized, duration: { less_than: 2.seconds } | ||
validates :is_performance_optimizeds, duration: { less_than: 2.seconds } | ||
end |
17 changes: 17 additions & 0 deletions
17
test/dummy/app/models/processable_file/validator/is_performance_optimized.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
# == Schema Information | ||
# | ||
# Table name: processable_file_validator_is_performance_optimizeds | ||
# | ||
# id :integer not null, primary key | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# | ||
|
||
class ProcessableFile::Validator::IsPerformanceOptimized < ApplicationRecord | ||
has_one_attached :is_performance_optimized | ||
has_many_attached :is_performance_optimizeds | ||
validates :is_performance_optimized, processable_file: true | ||
validates :is_performance_optimizeds, processable_file: true | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
describe ActiveStorageValidations::ASVBlobMetadatable do | ||
let(:blob) { ActiveStorage::Blob.new } | ||
|
||
it "adds our gem's getter method to ActiveStorage::Blob custom metadata" do | ||
assert blob.respond_to?(:active_storage_validations_metadata) | ||
end | ||
|
||
it "adds our gem's setter method to ActiveStorage::Blob custom metadata" do | ||
blob.active_storage_validations_metadata = { 'duration' => '1.0' } | ||
assert blob.active_storage_validations_metadata == { 'duration' => '1.0' } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
test/validators/shared_examples/is_performance_optimized.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# frozen_string_literal: true | ||
|
||
module IsPerformanceOptimized | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
subject { validator_test_class::IsPerformanceOptimized.new(params) } | ||
|
||
let(:validator_class) { "ActiveStorageValidations::#{validator_test_class.name.delete('::')}".constantize } | ||
|
||
describe "when the attachable blob has not been analyzed by our gem yet" do | ||
before { subject.is_performance_optimized.attach(attachable) } | ||
|
||
it "calls the corresponding media analyzer (expensive operation) once" do | ||
assert_called_on_instance_of(validator_class, :generate_metadata_for, times: 1, returns: {}) do | ||
subject.valid? | ||
end | ||
end | ||
end | ||
|
||
describe "when an attachable blob has already been analyzed by our gem" do | ||
before do | ||
subject.is_performance_optimizeds.attach(attachable) | ||
subject.save! | ||
end | ||
|
||
it "only calls the corresponding media analyzer (expensive operation) on the new attachable" do | ||
assert_called_on_instance_of(validator_class, :generate_metadata_for, times: 1, returns: {}) do | ||
subject.is_performance_optimizeds.attach(attachable) | ||
end | ||
end | ||
end | ||
end | ||
end |