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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b50f4ec91d36a884fae8c6463e116754aacccffa563b31d531ef003b331d61db
4
- data.tar.gz: 03ea2b3605d68b075d5d3ba7aebb344cfc9a21e9ff54d08a6be2600bedb21032
3
+ metadata.gz: b9b1cb8a3c118b8e410665f182f78fd63ec1dfa571af98f2d93f5deca9e0600a
4
+ data.tar.gz: 73785e5dc16509d33978fcc22571e9dc961d9c5dd02e712b640d4a57d8bb8a2f
5
5
  SHA512:
6
- metadata.gz: 6d1396b558389340190ffefca65373c7c1c44696a11f3a7d2c775111296a74a096b4144a58c31813f4d06912217d76586c0277206fbe72f64f6d8c167769bf01
7
- data.tar.gz: 6556c00f595d9bb5cf2e52fd4ce128ff75f8d8d2423db499e32eebf645ee0e411920f7df66668cc0f66ff48e0db00619ca279539e8b3c67bb4f2e09b8deda162
6
+ metadata.gz: 5836ceeb70a8d592d5f9406c6b6c79bd6d0ffe51f5a80b4e3ab202983aba5ec0526ec30fe7a561b02e78da97578a10a8509be898c9a208e9dc3e043417c2387c
7
+ data.tar.gz: 8f87ae5f9efae07bd63eab9e5394d5b78cd6162569583327cac0c086dc244899d718c69ce2c50aa4039d4b35ea21d268641ce83892bdb44df0a899b0e50575e7
@@ -1,3 +1,3 @@
1
1
  module DenormalizeFields
2
- VERSION = '1.2.1'
2
+ VERSION = '1.3.0'
3
3
  end
@@ -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(ActiveRecord::RecordInvalid, to)
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
- field = mapping.rassoc(key.to_sym).first
104
- array.each do |details|
105
- to_record.errors.add(field, details[:error], **details.except(:error))
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.2.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-06 00:00:00.000000000 Z
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.0.alpha2
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.0.alpha2
46
+ version: '7.0'
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rake
49
49
  requirement: !ruby/object:Gem::Requirement