From 9416a458ec4beb07bb8baac12ad18efd12e05bb6 Mon Sep 17 00:00:00 2001 From: Niklas Bichinger Date: Sat, 8 Oct 2022 17:51:27 +0200 Subject: [PATCH 1/2] do not unfold explicitly excluded keys --- lib/active_storage_validations/option_proc_unfolding.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/active_storage_validations/option_proc_unfolding.rb b/lib/active_storage_validations/option_proc_unfolding.rb index bb65eb5f..c36ab9ce 100644 --- a/lib/active_storage_validations/option_proc_unfolding.rb +++ b/lib/active_storage_validations/option_proc_unfolding.rb @@ -1,10 +1,10 @@ module ActiveStorageValidations module OptionProcUnfolding - def unfold_procs(record, object, only_keys = nil) + def unfold_procs(record, object, only_keys) case object when Hash - object.merge(object) { |key, value| only_keys&.exclude?(key) ? unfold_procs(record, value, []) : unfold_procs(record, value) } + object.merge(object) { |key, value| only_keys&.exclude?(key) ? {} : unfold_procs(record, value, nil) } when Array object.map { |o| unfold_procs(record, o, only_keys) } else From 8cb152b7a1f7a4dee4c4ab4e8e53970682aa49d2 Mon Sep 17 00:00:00 2001 From: Niklas Bichinger Date: Sat, 8 Oct 2022 17:51:54 +0200 Subject: [PATCH 2/2] add tests for #166 --- test/active_storage_validations_test.rb | 18 ++++++++++++++++++ test/dummy/app/models/user.rb | 2 ++ 2 files changed, 20 insertions(+) diff --git a/test/active_storage_validations_test.rb b/test/active_storage_validations_test.rb index d2c670e4..7104b6c4 100644 --- a/test/active_storage_validations_test.rb +++ b/test/active_storage_validations_test.rb @@ -91,6 +91,24 @@ class ActiveStorageValidations::Test < ActiveSupport::TestCase u.proc_photos.attach(bad_dummy_file) assert !u.valid? assert_equal u.errors.full_messages, ['Avatar has an invalid content type', 'Photos has an invalid content type', 'Image regex has an invalid content type', 'Proc avatar has an invalid content type', 'Proc photos has an invalid content type', 'Proc image regex has an invalid content type'] + + u = User.new(name: 'Peter Griffin') + u.avatar.attach(dummy_file) + u.proc_avatar.attach(dummy_file) + u.photos.attach(dummy_file) + u.proc_photos.attach(dummy_file) + u.conditional_image_2.attach(dummy_file) + assert u.valid? + assert_equal u.errors.full_messages, [] + + u = User.new(name: 'Peter Griffin') + u.avatar.attach(bad_dummy_file) + u.proc_avatar.attach(bad_dummy_file) + u.photos.attach(bad_dummy_file) + u.proc_photos.attach(dummy_file) + u.conditional_image_2.attach(bad_dummy_file) + assert !u.valid? + assert_equal u.errors.full_messages, ["Avatar has an invalid content type", "Photos has an invalid content type", "Conditional image 2 has an invalid content type", "Proc avatar has an invalid content type"] end # reads content type from file, not from webp_file_wrong method diff --git a/test/dummy/app/models/user.rb b/test/dummy/app/models/user.rb index 2b77da8c..b49903db 100644 --- a/test/dummy/app/models/user.rb +++ b/test/dummy/app/models/user.rb @@ -13,6 +13,7 @@ class User < ApplicationRecord has_many_attached :photos has_one_attached :image_regex has_one_attached :conditional_image + has_one_attached :conditional_image_2 has_one_attached :proc_avatar has_many_attached :proc_photos has_one_attached :proc_image_regex @@ -23,6 +24,7 @@ class User < ApplicationRecord validates :photos, attached: true, content_type: ['image/png', 'image/jpg', /\A.*\/pdf\z/] validates :image_regex, content_type: /\Aimage\/.*\z/ validates :conditional_image, attached: true, if: -> { name == 'Foo' } + validates :conditional_image_2, attached: true, content_type: -> (record) {[/\Aimage\/.*\z/]}, size: { less_than: 10.megabytes }, if: -> { name == 'Peter Griffin' } validates :proc_avatar, attached: { message: "must not be blank" }, content_type: -> (record) {:png} validates :proc_photos, attached: true, content_type: -> (record) {['image/png', 'image/jpg', /\A.*\/pdf\z/]} validates :proc_image_regex, content_type: -> (record) {/\Aimage\/.*\z/}