service_pattern 1.0.8 → 1.0.9

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: 63c1dea6c5ce4fe51b3e76f3d00d5ca1ae0ffe1606bd25e5bcb4015aa88b56bf
4
- data.tar.gz: 99fcc5fe4cda47aab13599331717ea8c3f1781aa9889a2d1e208257de5c8cb16
3
+ metadata.gz: bc1d86311bd4cf4a8bd24e9261f1e7cc7bde638786699be7d859c1b6a8259e6f
4
+ data.tar.gz: 7874ffb81a44c4d6c05f281026e7926e5cf5b62b64aa0479d4eb76fc3620ed87
5
5
  SHA512:
6
- metadata.gz: b427f1b1346a9cf648a0366e0882c15244422b02e6cbeea637f23965ad74116f18427bf9df5c71c85e1f90aba61265925874d044bdd6b451d14cb44fc1e16bd7
7
- data.tar.gz: ff393b3a73bb1951de6936005a95e47b248aecaf7ad1c96d3333bfc0d27eb422dc98c368b233e25f07e4846b49f824400331b5585ab0e77e21c1b9b1dffc56a5
6
+ metadata.gz: b16d78da76eb279b919e28fa1dcdb9ec8d4a84cd44b2008bd002522112be7fdaef68037fe80646d1d87398e02cf86775f40b38510ea9d39452d58cc9c58228c8
7
+ data.tar.gz: 28f526d8a9516d1bff202a708e789f3fec76452d1240b8281ade421b4d7fb3b3b8979c92ebb93b38e355cd9a5765fa8732954ff90205bb6c09e4c7f330e9dc64
@@ -0,0 +1,29 @@
1
+ class ServicePattern::ModelsSave < ServicePattern::Service
2
+ attr_reader :models, :simple_model_errors
3
+
4
+ def initialize(models:, simple_model_errors: false)
5
+ @models = models
6
+ @simple_model_errors = simple_model_errors
7
+ end
8
+
9
+ def perform
10
+ errors = []
11
+
12
+ models.first.transaction do
13
+ models.each do |model|
14
+ next if model.save
15
+
16
+ if simple_model_errors
17
+ errors += ServicePattern::SimpleModelErrors.execute!(model: model)
18
+ else
19
+ errors += models.map(&:errors).map(&:full_messages).flatten
20
+ end
21
+
22
+ raise ActiveRecord::Rollback if errors.any?
23
+ end
24
+ end
25
+
26
+ fail! errors.uniq if errors.any?
27
+ succeed!
28
+ end
29
+ end
@@ -113,6 +113,10 @@ class ServicePattern::Service
113
113
  ServicePattern::Service.fail!(errors)
114
114
  end
115
115
 
116
+ def save_models_or_fail(*models, simple_model_errors: false)
117
+ ServicePattern::ModelsSave.execute!(models: models, simple_model_errors: simple_model_errors)
118
+ end
119
+
116
120
  def succeed!(result = nil)
117
121
  ServicePattern::Response.new(result: result)
118
122
  end
@@ -0,0 +1,52 @@
1
+ class ServicePattern::SimpleModelErrors
2
+ attr_reader :additional_attributes, :errors, :model, :models_inspected
3
+
4
+ def initialize(model:, additional_attributes: [])
5
+ @additional_attributes = additional_attributes
6
+ @model = model
7
+ @errors = []
8
+ @models_inspected = []
9
+ end
10
+
11
+ def perform
12
+ inspect_model(model)
13
+ errors
14
+ end
15
+
16
+ def inspect_model(model_to_inspect)
17
+ return if models_inspected.include?(model_to_inspect)
18
+
19
+ model_to_inspect.valid? if model_to_inspect.errors.empty? # Generates the errors on the model so we can detect them
20
+ models_inspected << model_to_inspect
21
+
22
+ model_to_inspect.errors.messages.each do |attribute_name, attribute_errors|
23
+ if attribute_name == :base
24
+ @errors += attribute_errors
25
+ else
26
+ next if model_to_inspect.attribute_names.exclude?(attribute_name.to_s) && additional_attributes.exclude?(attribute_name)
27
+
28
+ attribute_errors.each do |message|
29
+ errors << "#{model_to_inspect.class.human_attribute_name(attribute_name)} #{message}"
30
+ end
31
+ end
32
+ end
33
+
34
+ collect_errors_from_associations(model_to_inspect)
35
+ end
36
+
37
+ private
38
+
39
+ def collect_errors_from_associations(model_to_scan_reflections_on)
40
+ model_to_scan_reflections_on._reflections.each_key do |association_name|
41
+ target = model_to_scan_reflections_on.association(association_name.to_sym).target
42
+
43
+ if target.is_a?(ActiveRecord::Base)
44
+ inspect_model(target)
45
+ else
46
+ target&.each do |sub_model|
47
+ inspect_model(sub_model)
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -1,3 +1,3 @@
1
1
  module ServicePattern
2
- VERSION = "1.0.8".freeze
2
+ VERSION = "1.0.9".freeze
3
3
  end
@@ -5,6 +5,8 @@ module ServicePattern
5
5
 
6
6
  autoload :FailError, "#{path}/fail_error"
7
7
  autoload :FailedError, "#{path}/failed_error"
8
+ autoload :ModelsSave, "#{path}/models_save"
8
9
  autoload :Response, "#{path}/response"
9
10
  autoload :Service, "#{path}/service"
11
+ autoload :SimpleModelsError, "#{path}/simple_models_errors"
10
12
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: service_pattern
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.8
4
+ version: 1.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - kaspernj
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-06-24 00:00:00.000000000 Z
11
+ date: 2022-09-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: appraisal
@@ -163,8 +163,10 @@ files:
163
163
  - lib/service_pattern.rb
164
164
  - lib/service_pattern/fail_error.rb
165
165
  - lib/service_pattern/failed_error.rb
166
+ - lib/service_pattern/models_save.rb
166
167
  - lib/service_pattern/response.rb
167
168
  - lib/service_pattern/service.rb
169
+ - lib/service_pattern/simple_models_errors.rb
168
170
  - lib/service_pattern/version.rb
169
171
  - lib/tasks/service_pattern_tasks.rake
170
172
  homepage: https://www.github.com/kaspernj/service_pattern