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 +4 -4
- data/.github/workflows/ci.yml +3 -0
- data/.github/workflows/future_rails_ci.yml +1 -1
- data/CHANGELOG.md +11 -1
- data/lib/attr_json/model/nested_model_validator.rb +27 -0
- data/lib/attr_json/model.rb +6 -6
- data/lib/attr_json/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5a2727136aa434d698d6c54612f529c3765c2603012725161850be4cfd7f317f
|
4
|
+
data.tar.gz: 0da797831a4a0a7c904c15b31bdb37dff2afaec49ae35caec993f85cfd23a31e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a7e88ae65b9a417c00cee127fa8a90c46566698bd3835a74d5de941bbc9a64e0fdce9af72c1dd6adbb3fda29e02bc0817fccfc6b419b75088f1666e8a7c8ddc8
|
7
|
+
data.tar.gz: 9bad39be885c3e8ce0065259713c17aebb96cb498098d25540ef3cc8239b56fc41a616f34353d4d21f1eea103d6ff87a084a36ce8812769389daf4598acc0c1a
|
data/.github/workflows/ci.yml
CHANGED
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
|
-
|
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
|
data/lib/attr_json/model.rb
CHANGED
@@ -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)
|
211
|
-
#
|
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
|
-
#
|
224
|
-
|
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
|
data/lib/attr_json/version.rb
CHANGED
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.
|
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:
|
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.
|
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.
|