active_record_compose 0.1.4 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -0
- data/lib/active_record_compose/inner_model.rb +17 -0
- data/lib/active_record_compose/model.rb +26 -3
- data/lib/active_record_compose/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dd2ae31a400b17c7ab14c90bd1440060676590c23f91a61f7f54e9a7decf0294
|
4
|
+
data.tar.gz: 4a599ee2da8a5dcc3b0d9279a360b611b2e38f72304e8a837271f01e2b9821b2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 18f3632810ffc48ed4bd10f78c4e54ff3c383d34231289f9639e488f270bbfd623a5f002b9f4afc653762611621c6f96b85b52498c2ba34e942d0c7c7288bfe4
|
7
|
+
data.tar.gz: 45fc861a9d223da11b841800dbc0ab689160cabdfdfda3a69433cb1d98884f76f30c742319ffc1ad3601527e9464b322456e44b73c6f098355f898909b5e340c
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,15 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
+
## [0.1.6] - 2024-01-14
|
4
|
+
|
5
|
+
- add doc for `#save` and `#save!`.
|
6
|
+
- implement `#save` for symmetry with `#save!`
|
7
|
+
- add `InnerModel#initialize` doc.
|
8
|
+
|
9
|
+
## [0.1.5] - 2024-01-11
|
10
|
+
|
11
|
+
- when invalid, raises ActiveRecord::RecordInvalid on #save!
|
12
|
+
|
3
13
|
## [0.1.4] - 2024-01-10
|
4
14
|
|
5
15
|
- remove uniquely defined exception class.
|
@@ -6,6 +6,7 @@ module ActiveRecordCompose
|
|
6
6
|
class InnerModel
|
7
7
|
# @param model [Object] the model instance.
|
8
8
|
# @param context [Symbol] :save or :destroy
|
9
|
+
# @param context [Proc] proc returning either :save or :destroy
|
9
10
|
def initialize(model, context: :save)
|
10
11
|
@model = model
|
11
12
|
@context = context
|
@@ -19,6 +20,22 @@ module ActiveRecordCompose
|
|
19
20
|
ret.presence_in(%i[save destroy]) || :save
|
20
21
|
end
|
21
22
|
|
23
|
+
# Execute save or destroy. Returns true on success, false on failure.
|
24
|
+
# Whether save or destroy is executed depends on the value of context.
|
25
|
+
#
|
26
|
+
# @return [Boolean] returns true on success, false on failure.
|
27
|
+
def save
|
28
|
+
case context
|
29
|
+
when :destroy
|
30
|
+
model.destroy
|
31
|
+
else
|
32
|
+
model.save
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# Execute save or destroy. Unlike #save, an exception is raises on failure.
|
37
|
+
# Whether save or destroy is executed depends on the value of context.
|
38
|
+
#
|
22
39
|
# @return [InnerModel] self
|
23
40
|
def save!
|
24
41
|
case context
|
@@ -22,13 +22,28 @@ module ActiveRecordCompose
|
|
22
22
|
super(attributes)
|
23
23
|
end
|
24
24
|
|
25
|
+
# Save the models that exist in models.
|
26
|
+
# Returns false if any of the targets fail, true if all succeed.
|
27
|
+
#
|
28
|
+
# The save is performed within a single transaction.
|
29
|
+
#
|
30
|
+
# @return [Boolean] returns true on success, false on failure.
|
25
31
|
def save
|
26
32
|
return false if invalid?
|
27
33
|
|
28
34
|
save_in_transaction { run_callbacks(:save) { save_models } }
|
29
35
|
end
|
30
36
|
|
31
|
-
|
37
|
+
# Save the models that exist in models.
|
38
|
+
# Unlike #save, an exception is raises on failure.
|
39
|
+
#
|
40
|
+
# Saving, like `#save`, is performed within a single transaction.
|
41
|
+
#
|
42
|
+
def save!
|
43
|
+
valid? || raise_validation_error
|
44
|
+
|
45
|
+
save_in_transaction { run_callbacks(:save) { save_models } } || raise_on_save_error
|
46
|
+
end
|
32
47
|
|
33
48
|
# Behavior is same to `#save`, but `before_create` and `after_create` hooks fires.
|
34
49
|
#
|
@@ -65,7 +80,10 @@ module ActiveRecordCompose
|
|
65
80
|
# Behavior is same to `#create`, but raises an exception prematurely on failure.
|
66
81
|
#
|
67
82
|
def create!(attributes = {})
|
68
|
-
|
83
|
+
assign_attributes(attributes)
|
84
|
+
valid? || raise_validation_error
|
85
|
+
|
86
|
+
save_in_transaction { run_callbacks(:save) { run_callbacks(:create) { save_models } } } || raise_on_save_error
|
69
87
|
end
|
70
88
|
|
71
89
|
# Behavior is same to `#save`, but `before_update` and `after_update` hooks fires.
|
@@ -103,7 +121,10 @@ module ActiveRecordCompose
|
|
103
121
|
# Behavior is same to `#update`, but raises an exception prematurely on failure.
|
104
122
|
#
|
105
123
|
def update!(attributes = {})
|
106
|
-
|
124
|
+
assign_attributes(attributes)
|
125
|
+
valid? || raise_validation_error
|
126
|
+
|
127
|
+
save_in_transaction { run_callbacks(:save) { run_callbacks(:update) { save_models } } } || raise_on_save_error
|
107
128
|
end
|
108
129
|
|
109
130
|
private
|
@@ -130,6 +151,8 @@ module ActiveRecordCompose
|
|
130
151
|
|
131
152
|
def save_models = wrapped_models.all? { _1.save! }
|
132
153
|
|
154
|
+
def raise_validation_error = raise ActiveRecord::RecordInvalid, self
|
155
|
+
|
133
156
|
def raise_on_save_error = raise ActiveRecord::RecordNotSaved.new('Failed to save the model.', self)
|
134
157
|
end
|
135
158
|
end
|
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.1.
|
4
|
+
version: 0.1.6
|
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-
|
11
|
+
date: 2024-01-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -65,7 +65,7 @@ metadata:
|
|
65
65
|
homepage_uri: https://github.com/hamajyotan/active_record_compose
|
66
66
|
source_code_uri: https://github.com/hamajyotan/active_record_compose
|
67
67
|
changelog_uri: https://github.com/hamajyotan/active_record_compose/blob/main/CHANGELOG.md
|
68
|
-
documentation_uri: https://www.rubydoc.info/gems/active_record_compose/0.1.
|
68
|
+
documentation_uri: https://www.rubydoc.info/gems/active_record_compose/0.1.6
|
69
69
|
rubygems_mfa_required: 'true'
|
70
70
|
post_install_message:
|
71
71
|
rdoc_options: []
|