-
-
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] Refacto some logic (#270)
- Loading branch information
Showing
13 changed files
with
139 additions
and
92 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
28 changes: 28 additions & 0 deletions
28
lib/active_storage_validations/concerns/active_storageable.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,28 @@ | ||
module ActiveStorageValidations | ||
# ActiveStorageValidations::ActiveStorageable | ||
# | ||
# Validator helper methods to make our code more explicit. | ||
module ActiveStorageable | ||
extend ActiveSupport::Concern | ||
|
||
private | ||
|
||
# Retrieve either an ActiveStorage::Attached::One or an | ||
# ActiveStorage::Attached::Many instance depending on attribute definition | ||
def attached_files(record, attribute) | ||
Array.wrap(record.send(attribute)) | ||
end | ||
|
||
def attachments_present?(record, attribute) | ||
record.send(attribute).attached? | ||
end | ||
|
||
def no_attachments?(record, attribute) | ||
!attachments_present?(record, attribute) | ||
end | ||
|
||
def will_have_attachments_after_save?(record, attribute) | ||
!Array.wrap(record.send(attribute)).all?(&:marked_for_destruction?) | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
require_relative '../metadata' | ||
|
||
module ActiveStorageValidations | ||
# ActiveStorageValidations::Metadatable | ||
# | ||
# Validator methods for analyzing the attachment metadata. | ||
module Metadatable | ||
extend ActiveSupport::Concern | ||
|
||
private | ||
|
||
# Loop through the newly submitted attachables to validate them | ||
def validate_changed_files_from_metadata(record, attribute) | ||
attachables_from_changes(record, attribute).each do |attachable| | ||
is_valid?(record, attribute, attachable, Metadata.new(attachable).metadata) | ||
end | ||
end | ||
|
||
# Retrieve an array of newly submitted attachables which are file | ||
# representations such as ActiveStorage::Blob, ActionDispatch::Http::UploadedFile, | ||
# Rack::Test::UploadedFile, Hash, String, File or Pathname | ||
def attachables_from_changes(record, attribute) | ||
changes = record.attachment_changes[attribute.to_s] | ||
return [] if changes.blank? | ||
|
||
Array.wrap( | ||
changes.is_a?(ActiveStorage::Attached::Changes::CreateMany) ? changes.attachables : changes.attachable | ||
) | ||
end | ||
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
25 changes: 13 additions & 12 deletions
25
lib/active_storage_validations/processable_image_validator.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 |
---|---|---|
@@ -1,33 +1,34 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'concerns/active_storageable.rb' | ||
require_relative 'concerns/errorable.rb' | ||
require_relative 'concerns/metadatable.rb' | ||
require_relative 'concerns/symbolizable.rb' | ||
require_relative 'metadata.rb' | ||
|
||
module ActiveStorageValidations | ||
class ProcessableImageValidator < ActiveModel::EachValidator # :nodoc | ||
include OptionProcUnfolding | ||
include ActiveStorageable | ||
include Errorable | ||
include Metadatable | ||
include Symbolizable | ||
|
||
ERROR_TYPES = %i[ | ||
image_not_processable | ||
].freeze | ||
|
||
def validate_each(record, attribute, _value) | ||
return true unless record.send(attribute).attached? | ||
return if no_attachments?(record, attribute) | ||
|
||
changes = record.attachment_changes[attribute.to_s] | ||
return true if changes.blank? | ||
validate_changed_files_from_metadata(record, attribute) | ||
end | ||
|
||
private | ||
|
||
files = Array.wrap(changes.is_a?(ActiveStorage::Attached::Changes::CreateMany) ? changes.attachables : changes.attachable) | ||
def is_valid?(record, attribute, attachable, metadata) | ||
return if !metadata.empty? | ||
|
||
files.each do |file| | ||
if !Metadata.new(file).valid? | ||
errors_options = initialize_error_options(options, file) | ||
add_error(record, attribute, ERROR_TYPES.first , **errors_options) unless Metadata.new(file).valid? | ||
end | ||
end | ||
errors_options = initialize_error_options(options, attachable) | ||
add_error(record, attribute, ERROR_TYPES.first , **errors_options) | ||
end | ||
end | ||
end |
Oops, something went wrong.