attr_json 2.3.0 → 2.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eb4f8078039ec81cc25e7a95318ff5e42cb5bf85a955b40326f60f9022af16a2
4
- data.tar.gz: 635f08ce5fa34ce45dfdd5256ae0e81fdcc2d455f4525bad5f71f5b0c4391c6b
3
+ metadata.gz: 5a2727136aa434d698d6c54612f529c3765c2603012725161850be4cfd7f317f
4
+ data.tar.gz: 0da797831a4a0a7c904c15b31bdb37dff2afaec49ae35caec993f85cfd23a31e
5
5
  SHA512:
6
- metadata.gz: 22c35f674c7b5f3a4e91f348d20439df3e8fe175ca5d3c8938720a5e71739df2474cdddd5fdab5f0527f8751ed0fb6dee11ba7f75889c143daca92bd84afc86b
7
- data.tar.gz: 636628f0083dd61c869de5b9eb65a0c11988560e26d5dcd8e822759788bdac4bdb3c6f0359e2257867ac91a61721fda32df54a36f51a001450ef883289be433a
6
+ metadata.gz: a7e88ae65b9a417c00cee127fa8a90c46566698bd3835a74d5de941bbc9a64e0fdce9af72c1dd6adbb3fda29e02bc0817fccfc6b419b75088f1666e8a7c8ddc8
7
+ data.tar.gz: 9bad39be885c3e8ce0065259713c17aebb96cb498098d25540ef3cc8239b56fc41a616f34353d4d21f1eea103d6ff87a084a36ce8812769389daf4598acc0c1a
@@ -62,6 +62,9 @@ jobs:
62
62
  - gemfile: rails_7_1
63
63
  ruby: 3.2
64
64
 
65
+ - gemfile: rails_7_1
66
+ ruby: 3.3
67
+
65
68
  name: ${{ matrix.gemfile }}, ruby ${{ matrix.ruby }}
66
69
 
67
70
  steps:
@@ -45,7 +45,7 @@ jobs:
45
45
  include:
46
46
 
47
47
  - gemfile: rails_edge
48
- ruby: '3.1'
48
+ ruby: '3.3'
49
49
 
50
50
  name: ${{ matrix.gemfile }}, ruby ${{ matrix.ruby }}
51
51
 
data/CHANGELOG.md CHANGED
@@ -4,7 +4,8 @@ Notable changes to this project will be documented in this file.
4
4
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
5
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
- ## [Unreleased](https://github.com/jrochkind/attr_json/compare/v2.3.0...HEAD)
7
+
8
+ ## [Unreleased](https://github.com/jrochkind/attr_json/compare/v2.3.1...HEAD)
8
9
 
9
10
  ### Fixed
10
11
 
@@ -30,6 +31,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
30
31
 
31
32
  *
32
33
 
34
+ ## [2.3.1](https://github.com/jrochkind/attr_json/compare/v2.3.0...2.3.1)
35
+
36
+ ### Fixed
37
+
38
+ * Keep nested model validation working in Rails post-7.1, by no longer trying to re-use ActiveRecord::Validations::AssociatedValidator, instead supplying our own custom code. https://github.com/jrochkind/attr_json/pull/220
39
+
40
+ * Validate PolymorphicModel instances. https://github.com/jrochkind/attr_json/pull/225 Thanks @LyricL-Gitster
41
+
42
+
33
43
  ## [2.3.0]((https://github.com/jrochkind/attr_json/compare/v2.2.0...v2.3.0))
34
44
 
35
45
 
@@ -0,0 +1,27 @@
1
+ module AttrJson
2
+ module Model
3
+ # Used to validate an attribute in an AttrJson::Model whose values are other models, when
4
+ # you want validation errors on the nested models to post up.
5
+ #
6
+ # This is based on ActiveRecord's own ActiveRecord::Validations::AssociatedValidator, and actually forked
7
+ # from it at https://github.com/rails/rails/blob/e37adfed4eff3b43350ec87222a922e9c72d9c1b/activerecord/lib/active_record/validations/associated.rb
8
+ #
9
+ # We used to simply use an ActiveRecord::Validations::AssociatedValidator, but as of https://github.com/jrochkind/attr_json/pull/220 (e1e798142d)
10
+ # it got ActiveRecord-specific functionality that no longer worked with our use case.
11
+ #
12
+ # No problem, the implementation is simple, we can provide it here, based on the last version that did work.
13
+ class NestedModelValidator < ActiveModel::EachValidator
14
+ def validate_each(record, attribute, value)
15
+ if Array(value).reject { |r| valid_object?(r) }.any?
16
+ record.errors.add(attribute, :invalid, **options.merge(value: value))
17
+ end
18
+ end
19
+
20
+ private
21
+ def valid_object?(record)
22
+ #(record.respond_to?(:marked_for_destruction?) && record.marked_for_destruction?) || record.valid?
23
+ record.valid?
24
+ end
25
+ end
26
+ end
27
+ end
@@ -8,6 +8,7 @@ require 'attr_json/attribute_definition/registry'
8
8
 
9
9
  require 'attr_json/type/model'
10
10
  require 'attr_json/model/cocoon_compat'
11
+ require 'attr_json/model/nested_model_validator'
11
12
 
12
13
  require 'attr_json/serialization_coder_from_type'
13
14
 
@@ -207,8 +208,8 @@ module AttrJson
207
208
  # @option options [String,Symbol] :store_key (nil) Serialize to JSON using
208
209
  # given store_key, rather than name as would be usual.
209
210
  #
210
- # @option options [Boolean] :validate (true) Create an ActiveRecord::Validations::AssociatedValidator so
211
- # validation errors on the attributes post up to self.
211
+ # @option options [Boolean] :validate (true) Mak validation errors on the attributes post up
212
+ # to self, using something similar to an ActiveRecord::Validations::AssociatedValidator
212
213
  def attr_json(name, type, **options)
213
214
  options.assert_valid_keys(*(AttributeDefinition::VALID_OPTIONS - [:container_attribute] + [:validate]))
214
215
 
@@ -219,10 +220,9 @@ module AttrJson
219
220
  )
220
221
 
221
222
  # By default, automatically validate nested models
222
- if type.kind_of?(AttrJson::Type::Model) && options[:validate] != false
223
- # Yes. we're passing an ActiveRecord::Validations validator, but
224
- # it works fine for ActiveModel. If this changes in the future, tests will catch.
225
- self.validates_with ActiveRecord::Validations::AssociatedValidator, attributes: [name.to_sym]
223
+ if (type.kind_of?(AttrJson::Type::Model) || type.kind_of?(AttrJson::Type::PolymorphicModel)) && options[:validate] != false
224
+ # Post validations up with something based on ActiveRecord::Validations::AssociatedValidator
225
+ self.validates_with ::AttrJson::Model::NestedModelValidator, attributes: [name.to_sym]
226
226
  end
227
227
 
228
228
  _attr_jsons_module.module_eval do
@@ -1,3 +1,3 @@
1
1
  module AttrJson
2
- VERSION = "2.3.0"
2
+ VERSION = "2.3.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: attr_json
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.0
4
+ version: 2.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonathan Rochkind
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-12-04 00:00:00.000000000 Z
11
+ date: 2024-05-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -172,6 +172,7 @@ files:
172
172
  - lib/attr_json/config.rb
173
173
  - lib/attr_json/model.rb
174
174
  - lib/attr_json/model/cocoon_compat.rb
175
+ - lib/attr_json/model/nested_model_validator.rb
175
176
  - lib/attr_json/nested_attributes.rb
176
177
  - lib/attr_json/nested_attributes/builder.rb
177
178
  - lib/attr_json/nested_attributes/multiparameter_attribute_writer.rb
@@ -207,7 +208,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
207
208
  - !ruby/object:Gem::Version
208
209
  version: '0'
209
210
  requirements: []
210
- rubygems_version: 3.4.21
211
+ rubygems_version: 3.4.19
211
212
  signing_key:
212
213
  specification_version: 4
213
214
  summary: ActiveRecord attributes stored serialized in a json column, super smooth.