active_record_better_dependent_error_messages 0.0.2 → 0.0.3

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: d6ff57c47d56bcc5839e00341215cd0ce6c1acba1367ca7cf8c74044e2bd0857
4
- data.tar.gz: 9e6d9be7924d9ae510c0b416ef96af83a5b4fba56cf55c4bcbb807bc272c4026
3
+ metadata.gz: 9f6e4921f7713c0a6dfef6e39a317542af1a5573adeffd607cafccccb1e1bdc6
4
+ data.tar.gz: 70fd193c2aefd5c6a2a158505f0411dd90990d23c3be582fe22ac31c1a168a0f
5
5
  SHA512:
6
- metadata.gz: 6b5a02c387eaf465974fab7c6874037f50d462c7a669ca6da557e90ae69edee10b912d53a89dd7813c47769b1f83af5d417dd47325c7b72703ed824329be317b
7
- data.tar.gz: ce22197b91a3875b1493ebd5325ee5e10f8f6221a1030aea92d043fedd19fb8c4364b3c1b54bba5ed8113ecd2cf7ae2994fd7c787b521206984ab7b8a5627fee
6
+ metadata.gz: 03c815619cea44b48cb553db01156b81c28e6951d733a666078440b24427d53b6766c8883d324ebce8980b075ea73f4050648cacc08be691caecf1f601e30854
7
+ data.tar.gz: 5d1edbdd53d0c7766129a8c240088a694925a9590fa516f3712130fba42783c482f92d8c086cfeab3e5e64d952c65611a8005822518728a14d5f59d776d1fa33
@@ -43,6 +43,7 @@ private
43
43
  end
44
44
  elsif association.macro == :has_many && !has_error?
45
45
  ids = model.__send__(association.name).pluck(:id)
46
+
46
47
  if ids.any?
47
48
  root_model.errors.add(
48
49
  :base,
@@ -58,16 +59,50 @@ private
58
59
 
59
60
  def check_sub_destroy_association(association)
60
61
  if association.macro == :has_one
61
- sub_model = model.__send__(association.name)
62
- ActiveRecordBetterDependentErrorMessages::DestroyValidator.(root_model: root_model, model: sub_model, trace: trace + [sub_model]) if sub_model
62
+ sub_model = model.association(association.name).target
63
+
64
+ if sub_model
65
+ extract_sub_destroy_errors(association, sub_model)
66
+ ActiveRecordBetterDependentErrorMessages::DestroyValidator.(root_model: root_model, model: sub_model, trace: trace + [sub_model])
67
+ end
63
68
  elsif association.macro == :has_many
64
- sub_models = model.__send__(association.name)
65
- sub_models.find_each do |sub_model_i|
69
+ sub_models = model.association(association.name).target
70
+
71
+ sub_models.each do |sub_model_i|
72
+ extract_sub_destroy_errors(association, sub_model_i)
66
73
  ActiveRecordBetterDependentErrorMessages::DestroyValidator.(root_model: root_model, model: sub_model_i, trace: trace + [sub_model_i])
67
74
  end
68
75
  end
69
76
  end
70
77
 
78
+ def extract_sub_destroy_errors(association, model)
79
+ return unless model_has_custom_errors?(model)
80
+
81
+ root_model.errors.add(
82
+ :base,
83
+ :cannot_delete_because_of_child_errors,
84
+ association_name: association.klass.model_name.human(count: 2).downcase,
85
+ count: 1,
86
+ message: "Cannot delete because of child errors in #{association.name} with IDs: #{model.id}: #{model.errors.full_messages.join(". ")}",
87
+ model_name: model.model_name.human.downcase
88
+ )
89
+ end
90
+
91
+ def model_has_custom_errors?(model)
92
+ custom_errors = 0
93
+
94
+ model.errors.details.each do |error_types_with_detail|
95
+ attribute = error_types_with_detail.fetch(0)
96
+ errors = error_types_with_detail.fetch(1)
97
+
98
+ errors.each do |error|
99
+ custom_errors += 1 unless error.fetch(:error) == :"restrict_dependent_destroy.has_many"
100
+ end
101
+ end
102
+
103
+ custom_errors > 0
104
+ end
105
+
71
106
  def has_error?
72
107
  root_model.errors.details[:base] && root_model.errors.details[:base].any? { |error| error[:error] == :cannot_delete_because_of_restriction }
73
108
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveRecordBetterDependentErrorMessages
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_better_dependent_error_messages
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - kaspernj
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-14 00:00:00.000000000 Z
11
+ date: 2021-03-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -142,7 +142,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
142
142
  - !ruby/object:Gem::Version
143
143
  version: '0'
144
144
  requirements: []
145
- rubygems_version: 3.0.6
145
+ rubygems_version: 3.0.8
146
146
  signing_key:
147
147
  specification_version: 4
148
148
  summary: A gem that makes better dependent error messages telling exactly which relationship