denormalize_fields 1.2.1 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/denormalize_fields/version.rb +1 -1
- data/lib/denormalize_fields.rb +17 -4
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b9b1cb8a3c118b8e410665f182f78fd63ec1dfa571af98f2d93f5deca9e0600a
|
4
|
+
data.tar.gz: 73785e5dc16509d33978fcc22571e9dc961d9c5dd02e712b640d4a57d8bb8a2f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5836ceeb70a8d592d5f9406c6b6c79bd6d0ffe51f5a80b4e3ab202983aba5ec0526ec30fe7a561b02e78da97578a10a8509be898c9a208e9dc3e043417c2387c
|
7
|
+
data.tar.gz: 8f87ae5f9efae07bd63eab9e5394d5b78cd6162569583327cac0c086dc244899d718c69ce2c50aa4039d4b35ea21d268641ce83892bdb44df0a899b0e50575e7
|
data/lib/denormalize_fields.rb
CHANGED
@@ -4,6 +4,18 @@ require 'denormalize_fields/version'
|
|
4
4
|
module DenormalizeFields
|
5
5
|
module_function
|
6
6
|
|
7
|
+
class ArgumentError < ::ArgumentError; end
|
8
|
+
|
9
|
+
class RelatedRecordInvalid < ::ActiveRecord::RecordInvalid
|
10
|
+
attr_accessor :owner, :mapping
|
11
|
+
|
12
|
+
def initialize(record:, owner:, mapping:)
|
13
|
+
super(record)
|
14
|
+
self.owner = owner
|
15
|
+
self.mapping = mapping
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
7
19
|
def denormalize(fields:, from:, onto:, prefix: nil, **options)
|
8
20
|
mapping = cast_to_mapping(fields, prefix: prefix)
|
9
21
|
validate_options(**options)
|
@@ -94,15 +106,16 @@ module DenormalizeFields
|
|
94
106
|
return if to.new_record? ? to.valid? : to.save
|
95
107
|
|
96
108
|
DenormalizeFields.copy_errors(to.errors, to_record: owner, mapping: mapping)
|
97
|
-
raise(
|
109
|
+
raise RelatedRecordInvalid.new(record: to, owner: owner, mapping: mapping)
|
98
110
|
end
|
99
111
|
|
100
112
|
# TODO: use Errors#import when it becomes available in rails 6.1 or 6.2
|
101
113
|
def copy_errors(errors, to_record:, mapping:)
|
102
114
|
errors.details.each do |key, array|
|
103
|
-
|
104
|
-
|
105
|
-
|
115
|
+
Array(mapping.rassoc(key.to_sym)).compact.each do |field|
|
116
|
+
array.each do |details|
|
117
|
+
to_record.errors.add(field, details[:error], **details.except(:error))
|
118
|
+
end
|
106
119
|
end
|
107
120
|
end
|
108
121
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: denormalize_fields
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Janosch Müller
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-12-
|
11
|
+
date: 2021-12-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -34,16 +34,16 @@ dependencies:
|
|
34
34
|
name: rails
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
36
36
|
requirements:
|
37
|
-
- -
|
37
|
+
- - "~>"
|
38
38
|
- !ruby/object:Gem::Version
|
39
|
-
version: 7.0
|
39
|
+
version: '7.0'
|
40
40
|
type: :development
|
41
41
|
prerelease: false
|
42
42
|
version_requirements: !ruby/object:Gem::Requirement
|
43
43
|
requirements:
|
44
|
-
- -
|
44
|
+
- - "~>"
|
45
45
|
- !ruby/object:Gem::Version
|
46
|
-
version: 7.0
|
46
|
+
version: '7.0'
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rake
|
49
49
|
requirement: !ruby/object:Gem::Requirement
|