active_record_compose 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0c3cb23854b3caafb1f25c297b1d12fb7693c5af5d8e7f917ce3117e5334afa0
4
- data.tar.gz: fe905d6071cc4c491fadd68c35b36540b57881d78b6c398acaf2e573d766912c
3
+ metadata.gz: 9253aaba5ac1cc24965caf5b0cd683c7e910b693f91290626318208f3d35f079
4
+ data.tar.gz: 57c187fe08e94cdac9a5fd1dc690dea98af686e898be1acaab0ad6a529ca54f6
5
5
  SHA512:
6
- metadata.gz: 6a8850ebb01e4705f658bbc65c2cc10366214a2c34787b3d6c6228c34ac405aa00b548212747ef5429fe26eabe1633bf5af9d1019bd95094c1cfd79897811d4c
7
- data.tar.gz: a531f7980d9d5ed490aa8c35927176511db6ff81f81009d02ac4121a04abe57efd6ac4dc5db8f353b4ef03c18214c73f246a1c6c8e43e235d1065d436538de31
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,13 @@
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
+
3
11
  ## [0.2.1] - 2024-01-31
4
12
 
5
13
  - in `#save` (without bang), `ActiveRecord::RecordInvalid` error is not passed outward.
@@ -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,9 +37,11 @@ module ActiveRecordCompose
33
37
  def save
34
38
  return false if invalid?
35
39
 
36
- save_in_transaction { save_models(bang: false) }
37
- rescue ActiveRecord::RecordInvalid
38
- false
40
+ with_transaction_returning_status do
41
+ run_callbacks(:save) { save_models(bang: false) }
42
+ rescue ActiveRecord::RecordInvalid
43
+ false
44
+ end
39
45
  end
40
46
 
41
47
  # Save the models that exist in models.
@@ -46,7 +52,9 @@ module ActiveRecordCompose
46
52
  def save!
47
53
  valid? || raise_validation_error
48
54
 
49
- 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
50
58
  end
51
59
 
52
60
  # Behavior is same to `#save`, but `before_create` and `after_create` hooks fires.
@@ -78,9 +86,11 @@ module ActiveRecordCompose
78
86
  assign_attributes(attributes)
79
87
  return false if invalid?
80
88
 
81
- save_in_transaction { run_callbacks(:create) { save_models(bang: false) } }
82
- rescue ActiveRecord::RecordInvalid
83
- 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
84
94
  end
85
95
 
86
96
  # Behavior is same to `#create`, but raises an exception prematurely on failure.
@@ -89,7 +99,9 @@ module ActiveRecordCompose
89
99
  assign_attributes(attributes)
90
100
  valid? || raise_validation_error
91
101
 
92
- 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
93
105
  end
94
106
 
95
107
  # Behavior is same to `#save`, but `before_update` and `after_update` hooks fires.
@@ -121,9 +133,11 @@ module ActiveRecordCompose
121
133
  assign_attributes(attributes)
122
134
  return false if invalid?
123
135
 
124
- save_in_transaction { run_callbacks(:update) { save_models(bang: false) } }
125
- rescue ActiveRecord::RecordInvalid
126
- 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
127
141
  end
128
142
 
129
143
  # Behavior is same to `#update`, but raises an exception prematurely on failure.
@@ -132,9 +146,20 @@ module ActiveRecordCompose
132
146
  assign_attributes(attributes)
133
147
  valid? || raise_validation_error
134
148
 
135
- 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
136
152
  end
137
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
+
138
163
  private
139
164
 
140
165
  def models = @__models ||= ActiveRecordCompose::InnerModelCollection.new
@@ -143,20 +168,6 @@ module ActiveRecordCompose
143
168
 
144
169
  def validate_models = wrapped_models.select { _1.invalid? }.each { errors.merge!(_1) }
145
170
 
146
- def save_in_transaction(&block)
147
- run_callbacks(:commit) do
148
- result = ::ActiveRecord::Base.transaction do
149
- raise ActiveRecord::Rollback unless run_callbacks(:save, &block)
150
-
151
- true
152
- end
153
- result.present?
154
- rescue StandardError
155
- run_callbacks :rollback
156
- raise
157
- end.present?
158
- end
159
-
160
171
  def save_models(bang:) = wrapped_models.all? { bang ? _1.save! : _1.save }
161
172
 
162
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.1'
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.1
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-31 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.1
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