active_storage_validations 2.0.3 → 2.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9ba7e03a6367634ed5ece53bda87d82c76b179c75e2c9b37e4055d8f47f9ab98
4
- data.tar.gz: 7225e4937cab59e3209d082d9650c1077342eae7dcae46ddee0c25a9a5e93e56
3
+ metadata.gz: 491e16f93da96fad8b6b28ea04082b86b94720b12ba6fe1dcfc78c7896276ae7
4
+ data.tar.gz: fbb276c358504d03e6d6c46c02e9553e5e376141d070ee1fd155e5074cb43f8e
5
5
  SHA512:
6
- metadata.gz: 9464b889ae0f8ab4167bcc612926e44c52d3541de6092fa0c82baf57a2f9edb4f541780145443966fa168c8f3f819456b000fac4a8770c1e923219a7394918da
7
- data.tar.gz: 4dea3b65668a16cac757d85f4e8d8f31e98c5089bbcf5747a5f192083fb12e2b57d611091eb2e2fb468aff27c590cdad1232566f24b75415784c7fe28434013f
6
+ metadata.gz: 8f29a4388357d60f8d834833bb687450fa890f42462d48dc8c664a01c088f72edf3bad8eb5e6ec497119049fde1f0dfb2cdddcd8a98b0ad24a327a4a9122cb0e
7
+ data.tar.gz: 7ae4c7f58abb91ffec5f2bbfb6e02180667e8f47c5982137be1a2330d366c51f90eaa0a32b634a76e29b8e27d9821c07b2d4ef140fc6f40481f876d8b380f77f
data/README.md CHANGED
@@ -7,6 +7,7 @@
7
7
  [![RailsJazz](https://github.com/igorkasyanchuk/rails_time_travel/blob/main/docs/my_other.svg?raw=true)](https://www.railsjazz.com)
8
8
  [![https://www.patreon.com/igorkasyanchuk](https://github.com/igorkasyanchuk/rails_time_travel/blob/main/docs/patron.svg?raw=true)](https://www.patreon.com/igorkasyanchuk)
9
9
 
10
+ [!["Buy Me A Coffee"](https://github.com/igorkasyanchuk/get-smart/blob/main/docs/snapshot-bmc-button-small.png?raw=true)](https://buymeacoffee.com/igorkasyanchuk)
10
11
 
11
12
  Active Storage Validations is a gem that allows you to add validations for Active Storage attributes.
12
13
 
@@ -801,6 +802,7 @@ To run the gem tests, launch the following commands in the root folder of gem re
801
802
  * `BUNDLE_GEMFILE=gemfiles/rails_7_1.gemfile bundle exec rake test` to run for Rails 7.1
802
803
  * `BUNDLE_GEMFILE=gemfiles/rails_7_2.gemfile bundle exec rake test` to run for Rails 7.2
803
804
  * `BUNDLE_GEMFILE=gemfiles/rails_8_0.gemfile bundle exec rake test` to run for Rails 8.0
805
+ * `BUNDLE_GEMFILE=gemfiles/rails_next.gemfile bundle exec rake test` to run for Rails main
804
806
 
805
807
  Snippet to run in console:
806
808
 
@@ -810,11 +812,13 @@ BUNDLE_GEMFILE=gemfiles/rails_7_0.gemfile bundle
810
812
  BUNDLE_GEMFILE=gemfiles/rails_7_1.gemfile bundle
811
813
  BUNDLE_GEMFILE=gemfiles/rails_7_2.gemfile bundle
812
814
  BUNDLE_GEMFILE=gemfiles/rails_8_0.gemfile bundle
815
+ BUNDLE_GEMFILE=gemfiles/rails_next.gemfile bundle
813
816
  BUNDLE_GEMFILE=gemfiles/rails_6_1_4.gemfile bundle exec rake test
814
817
  BUNDLE_GEMFILE=gemfiles/rails_7_0.gemfile bundle exec rake test
815
818
  BUNDLE_GEMFILE=gemfiles/rails_7_1.gemfile bundle exec rake test
816
819
  BUNDLE_GEMFILE=gemfiles/rails_7_2.gemfile bundle exec rake test
817
820
  BUNDLE_GEMFILE=gemfiles/rails_8_0.gemfile bundle exec rake test
821
+ BUNDLE_GEMFILE=gemfiles/rails_next.gemfile bundle exec rake test
818
822
  ```
819
823
 
820
824
  Tips:
@@ -838,3 +842,5 @@ The gem is available as open source under the terms of the [MIT License](https:/
838
842
 
839
843
  [<img src="https://github.com/igorkasyanchuk/rails_time_travel/blob/main/docs/more_gems.png?raw=true"
840
844
  />](https://www.railsjazz.com/?utm_source=github&utm_medium=bottom&utm_campaign=active_storage_validations)
845
+
846
+ [!["Buy Me A Coffee"](https://github.com/igorkasyanchuk/get-smart/blob/main/docs/snapshot-bmc-button.png?raw=true)](https://buymeacoffee.com/igorkasyanchuk)
@@ -2,15 +2,18 @@
2
2
 
3
3
  module ActiveStorageValidations
4
4
  class Railtie < ::Rails::Railtie
5
- initializer "active_storage_validations.configure", after: :load_config_initializers do
5
+ # Using after: :load_config_initializers would cause a stack level too deep error
6
+ # See: https://github.com/igorkasyanchuk/active_storage_validations/issues/364
7
+
8
+ initializer "active_storage_validations.configure" do
6
9
  ActiveSupport.on_load(:active_record) do
7
- send :include, ActiveStorageValidations
10
+ include ActiveStorageValidations
8
11
  end
9
12
  end
10
13
 
11
14
  initializer "active_storage_validations.extend_active_storage_blob" do
12
15
  ActiveSupport.on_load(:active_storage_blob) do
13
- include(ActiveStorageValidations::ASVBlobMetadatable)
16
+ include ActiveStorageValidations::ASVBlobMetadatable
14
17
  end
15
18
  end
16
19
  end
@@ -20,9 +20,18 @@ module ActiveStorageValidations
20
20
  def add_error(record, attribute, error_type, **errors_options)
21
21
  return if record.errors.added?(attribute, error_type)
22
22
 
23
+ error = record.errors.add(attribute, error_type, **errors_options)
24
+
25
+ # Rails 8.0.2 introduced a new way to mark errors as nested
26
+ # https://github.com/igorkasyanchuk/active_storage_validations/issues/377
27
+ if Rails.gem_version >= Gem::Version.new("8.0.2")
28
+ # Mark errors as nested when they occur in a parent/child context
29
+ set_nested_error(record, error) if updating_through_parent?(record)
30
+ end
31
+
23
32
  # You can read https://api.rubyonrails.org/classes/ActiveModel/Errors.html#method-i-add
24
33
  # to better understand how Rails model errors work
25
- record.errors.add(attribute, error_type, **errors_options)
34
+ error
26
35
  end
27
36
 
28
37
  private
@@ -36,5 +45,20 @@ module ActiveStorageValidations
36
45
  when Hash then file[:filename]
37
46
  end.to_s
38
47
  end
48
+
49
+ def updating_through_parent?(record)
50
+ record.instance_variable_defined?(:@marked_for_destruction) ||
51
+ record.instance_variable_defined?(:@_destroy) ||
52
+ (record.respond_to?(:parent) && record.parent.present?)
53
+ end
54
+
55
+ def set_nested_error(record, error)
56
+ reflection = record.class.reflect_on_association(:parent)
57
+
58
+ if reflection
59
+ association = record.association(reflection.name)
60
+ record.errors.objects.append(ActiveRecord::Associations::NestedError.new(association, error))
61
+ end
62
+ end
39
63
  end
40
64
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveStorageValidations
4
- VERSION = "2.0.3"
4
+ VERSION = "2.0.4"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_storage_validations
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.3
4
+ version: 2.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Igor Kasyanchuk
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-04-19 00:00:00.000000000 Z
11
+ date: 2025-05-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activejob