active_record_compose 0.2.0 → 0.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: 6693618ddc81944ece9c991f800ba8ce1ef5b7f462b13bfe0c9f4b03b04bdc6f
4
- data.tar.gz: 36cc7ec0a5852c409dab736d21058f2538c3a656e893d37a7ec8bf2ae499f2cb
3
+ metadata.gz: 9253aaba5ac1cc24965caf5b0cd683c7e910b693f91290626318208f3d35f079
4
+ data.tar.gz: 57c187fe08e94cdac9a5fd1dc690dea98af686e898be1acaab0ad6a529ca54f6
5
5
  SHA512:
6
- metadata.gz: 2780e3749043f5667bcd1a693c248f0c3f2378f7cb8d221f636ada171ab4c5b03c7baa2c479e381ab73570352fabd5c1fb623d9d3ca79b8718919bbe77f5fffe
7
- data.tar.gz: 4f6eec237d85851c0eb6041dacb2ea8c2000c431bfe4c632e11647e3009a0d9745bcb572b492ff71e07baa03c298faecb9406d76b939468f0d21cb4315c3eb04
6
+ metadata.gz: f66509b473e1ddbd2c17ff7f566b684d512e234874178f0cd123bea7eed09877e9198bb4d779f2de0dd0b68f63c675a4a9f2bf529f93d14f531b777a8519f437
7
+ data.tar.gz: 0fca4a8c7712faea8eaae1326591059a53684feece7f79ad68eb66a231ed37d635b77d082c7e1590da44c785c7b4c90f8d9cbbbc1665368e826d1c30fa0b5c71
data/.rubocop.yml CHANGED
@@ -33,6 +33,9 @@ Layout/LineLength:
33
33
  Metrics/BlockLength:
34
34
  Enabled: false
35
35
 
36
+ Metrics/MethodLength:
37
+ Enabled: false
38
+
36
39
  Naming/BlockForwarding:
37
40
  Enabled: false
38
41
 
data/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.3.0] - 2024-02-24
4
+
5
+ - strictify type checking
6
+ - testing with CI even in the head version of rails
7
+ - consolidate the main process of saving into the `#save` method
8
+ - leave transaction control to ActiveRecord::Transactions
9
+ - execution of `before_commit`, `after_commit` and `after_rollback` hook is delayed until after the database commit (or rollback).
10
+
11
+ ## [0.2.1] - 2024-01-31
12
+
13
+ - in `#save` (without bang), `ActiveRecord::RecordInvalid` error is not passed outward.
14
+
3
15
  ## [0.2.0] - 2024-01-21
4
16
 
5
17
  - add i18n doc.
@@ -63,10 +63,7 @@ module ActiveRecordCompose
63
63
  #
64
64
  # @return [Hash] hash with the attribute name as key and the attribute value as value.
65
65
  def attributes
66
- # steep:ignore all
67
- attrs = defined?(super) ? super : {}
68
- # steep:ignore end
69
- # @type var attrs: Hash[String, untyped]
66
+ attrs = __skip__ = defined?(super) ? super : {}
70
67
  attrs.merge(delegated_attributes.to_h { [_1, public_send(_1)] })
71
68
  end
72
69
  end
@@ -18,10 +18,14 @@ module ActiveRecordCompose
18
18
 
19
19
  validate :validate_models
20
20
 
21
+ # for ActiveRecord::Transactions
22
+ class << self
23
+ def connection = ActiveRecord::Base.connection
24
+ __skip__ = def composite_primary_key? = false
25
+ end
26
+
21
27
  def initialize(attributes = {})
22
- # steep:ignore all
23
- super(attributes)
24
- # steep:ignore end
28
+ __skip__ = super(attributes)
25
29
  end
26
30
 
27
31
  # Save the models that exist in models.
@@ -33,7 +37,11 @@ module ActiveRecordCompose
33
37
  def save
34
38
  return false if invalid?
35
39
 
36
- save_in_transaction { save_models(bang: false) }
40
+ with_transaction_returning_status do
41
+ run_callbacks(:save) { save_models(bang: false) }
42
+ rescue ActiveRecord::RecordInvalid
43
+ false
44
+ end
37
45
  end
38
46
 
39
47
  # Save the models that exist in models.
@@ -44,7 +52,9 @@ module ActiveRecordCompose
44
52
  def save!
45
53
  valid? || raise_validation_error
46
54
 
47
- save_in_transaction { save_models(bang: true) } || raise_on_save_error
55
+ with_transaction_returning_status do
56
+ run_callbacks(:save) { save_models(bang: true) }
57
+ end || raise_on_save_error
48
58
  end
49
59
 
50
60
  # Behavior is same to `#save`, but `before_create` and `after_create` hooks fires.
@@ -76,7 +86,11 @@ module ActiveRecordCompose
76
86
  assign_attributes(attributes)
77
87
  return false if invalid?
78
88
 
79
- save_in_transaction { run_callbacks(:create) { save_models(bang: false) } }
89
+ with_transaction_returning_status do
90
+ run_callbacks(:save) { run_callbacks(:create) { save_models(bang: false) } }
91
+ rescue ActiveRecord::RecordInvalid
92
+ false
93
+ end
80
94
  end
81
95
 
82
96
  # Behavior is same to `#create`, but raises an exception prematurely on failure.
@@ -85,7 +99,9 @@ module ActiveRecordCompose
85
99
  assign_attributes(attributes)
86
100
  valid? || raise_validation_error
87
101
 
88
- save_in_transaction { run_callbacks(:create) { save_models(bang: true) } } || raise_on_save_error
102
+ with_transaction_returning_status do
103
+ run_callbacks(:save) { run_callbacks(:create) { save_models(bang: true) } }
104
+ end || raise_on_save_error
89
105
  end
90
106
 
91
107
  # Behavior is same to `#save`, but `before_update` and `after_update` hooks fires.
@@ -117,7 +133,11 @@ module ActiveRecordCompose
117
133
  assign_attributes(attributes)
118
134
  return false if invalid?
119
135
 
120
- save_in_transaction { run_callbacks(:update) { save_models(bang: false) } }
136
+ with_transaction_returning_status do
137
+ run_callbacks(:save) { run_callbacks(:update) { save_models(bang: false) } }
138
+ rescue ActiveRecord::RecordInvalid
139
+ false
140
+ end
121
141
  end
122
142
 
123
143
  # Behavior is same to `#update`, but raises an exception prematurely on failure.
@@ -126,9 +146,20 @@ module ActiveRecordCompose
126
146
  assign_attributes(attributes)
127
147
  valid? || raise_validation_error
128
148
 
129
- save_in_transaction { run_callbacks(:update) { save_models(bang: true) } } || raise_on_save_error
149
+ with_transaction_returning_status do
150
+ run_callbacks(:save) { run_callbacks(:update) { save_models(bang: true) } }
151
+ end || raise_on_save_error
130
152
  end
131
153
 
154
+ # for ActiveRecord::Transactions
155
+ __skip__ = def id = nil
156
+
157
+ # for ActiveRecord::Transactions
158
+ __skip__ = def trigger_transactional_callbacks? = true
159
+
160
+ # for ActiveRecord::Transactions
161
+ __skip__ = def restore_transaction_record_state(_force_restore_state) = nil
162
+
132
163
  private
133
164
 
134
165
  def models = @__models ||= ActiveRecordCompose::InnerModelCollection.new
@@ -137,20 +168,6 @@ module ActiveRecordCompose
137
168
 
138
169
  def validate_models = wrapped_models.select { _1.invalid? }.each { errors.merge!(_1) }
139
170
 
140
- def save_in_transaction(&block)
141
- run_callbacks(:commit) do
142
- result = ::ActiveRecord::Base.transaction do
143
- raise ActiveRecord::Rollback unless run_callbacks(:save, &block)
144
-
145
- true
146
- end
147
- result.present?
148
- rescue StandardError
149
- run_callbacks :rollback
150
- raise
151
- end.present?
152
- end
153
-
154
171
  def save_models(bang:) = wrapped_models.all? { bang ? _1.save! : _1.save }
155
172
 
156
173
  def raise_validation_error = raise ActiveRecord::RecordInvalid, self
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveRecordCompose
4
- VERSION = '0.2.0'
4
+ VERSION = '0.3.0'
5
5
  end
@@ -88,7 +88,6 @@ module ActiveRecordCompose
88
88
  def models: -> InnerModelCollection
89
89
  def wrapped_models: -> Enumerator[InnerModel, InnerModelCollection]
90
90
  def validate_models: -> void
91
- def save_in_transaction: { -> bool } -> untyped
92
91
  def save_models: (bang: bool) -> bool
93
92
  def raise_validation_error: -> bot
94
93
  def raise_on_save_error: -> bot
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_compose
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - hamajyotan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-01-20 00:00:00.000000000 Z
11
+ date: 2024-02-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -51,7 +51,7 @@ metadata:
51
51
  homepage_uri: https://github.com/hamajyotan/active_record_compose
52
52
  source_code_uri: https://github.com/hamajyotan/active_record_compose
53
53
  changelog_uri: https://github.com/hamajyotan/active_record_compose/blob/main/CHANGELOG.md
54
- documentation_uri: https://www.rubydoc.info/gems/active_record_compose/0.2.0
54
+ documentation_uri: https://www.rubydoc.info/gems/active_record_compose/0.3.0
55
55
  rubygems_mfa_required: 'true'
56
56
  post_install_message:
57
57
  rdoc_options: []
@@ -68,7 +68,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
68
68
  - !ruby/object:Gem::Version
69
69
  version: '0'
70
70
  requirements: []
71
- rubygems_version: 3.5.5
71
+ rubygems_version: 3.5.6
72
72
  signing_key:
73
73
  specification_version: 4
74
74
  summary: activemodel form object pattern