active_record_compose 0.4.1 → 0.5.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 +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +5 -5
- data/lib/active_record_compose/inner_model.rb +13 -51
- data/lib/active_record_compose/inner_model_collection.rb +5 -22
- data/lib/active_record_compose/version.rb +1 -1
- data/sig/active_record_compose.rbs +4 -6
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7a3112ceb79671ca112aa5dde2222e10a0bd4daed96f82ebaa641746165d8991
|
4
|
+
data.tar.gz: e021f41a5e3334b0e91f170712c011679023a384bbfe17c3df2a97858747323e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 56cb8c6d615cbefb58fc774e96caa9add4cb72a8ca34188ce2d713f6b4f32ef75b7df4f273da2bd6966d08c1a8f1aadbeef838f3c5b436e5f1122efb7af5a961
|
7
|
+
data.tar.gz: 0aec57e80f57d0c3d1bf1d45e4cb39c3cebcae47c53a35b630817de592bd3893604ff58563eed2c6b23f4199d2f27bc39804a1a9e6fe20bbdb809defaae30e28
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,10 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
+
## [0.5.0] - 2024-10-09
|
4
|
+
|
5
|
+
- remove `:context` option. use `:destroy` option instead.
|
6
|
+
- remove `:destroy` option from `InnerModelCollection#destroy`.
|
7
|
+
|
3
8
|
## [0.4.1] - 2024-09-20
|
4
9
|
|
5
10
|
- Omitted optional argument for `InnerModelCollection#destroy`.
|
data/README.md
CHANGED
@@ -31,7 +31,7 @@ A callback is useful to define some processing before or after a save in a parti
|
|
31
31
|
However, if a callback is written directly in the AR model, it is necessary to consider the case where the model is updated in other contexts.
|
32
32
|
In particular, if you frequently create with test data, previously unnecessary processing will be called at every point of creation.
|
33
33
|
In addition to cost, the more complicated the callbacks you write, the more difficult it will be to create even a single test data.
|
34
|
-
If the callbacks are written in a class that inherits from `
|
34
|
+
If the callbacks are written in a class that inherits from `ActiveRecordCompose::Model`, the AR model itself will not be polluted, and the context can be limited.
|
35
35
|
|
36
36
|
```ruby
|
37
37
|
class AccountRegistration < ActiveRecordCompose::Model
|
@@ -70,7 +70,7 @@ Validates are basically fired in all cases where the model is manipulated. To av
|
|
70
70
|
and so on to work only in specific cases. This allows you to create context-sensitive validations for the same model operation.
|
71
71
|
However, this is the first step in making the model more and more complex. You will have to go around with `update(context: :foo)`
|
72
72
|
In some cases, you may have to go around with the context option, such as `update(context: :foo)` everywhere.
|
73
|
-
By writing validates in a class that extends `
|
73
|
+
By writing validates in a class that extends `ActiveRecordCompose::Model`, you can define context-specific validation without polluting the AR model itself.
|
74
74
|
|
75
75
|
```ruby
|
76
76
|
class AccountRegistration < ActiveRecordCompose::Model
|
@@ -116,7 +116,7 @@ account_registration.valid? #=> false
|
|
116
116
|
|
117
117
|
In an AR model, you can add, for example, `autosave: true` or `accepts_nested_attributes_for` to an association to update the related models at the same time.
|
118
118
|
There are ways to update related models at the same time. The operation is safe because it is transactional.
|
119
|
-
`
|
119
|
+
`ActiveRecordCompose::Model` has an internal array called models. By adding an AR object to this models array
|
120
120
|
By adding an AR object to the models, the object stored in the models provides an atomic update operation via #save.
|
121
121
|
|
122
122
|
```ruby
|
@@ -167,12 +167,12 @@ class AccountResignation < ActiveRecordCompose::Model
|
|
167
167
|
models.push(profile, destroy: true)
|
168
168
|
end
|
169
169
|
|
170
|
-
attr_reader :account, :profile
|
171
|
-
|
172
170
|
before_save :set_resigned_at
|
173
171
|
|
174
172
|
private
|
175
173
|
|
174
|
+
attr_reader :account, :profile
|
175
|
+
|
176
176
|
def set_resigned_at
|
177
177
|
account.resigned_at = Time.zone.now
|
178
178
|
end
|
@@ -7,60 +7,30 @@ module ActiveRecordCompose
|
|
7
7
|
# @param model [Object] the model instance.
|
8
8
|
# @param destroy [Boolean] given true, destroy model.
|
9
9
|
# @param destroy [Proc] when proc returning true, destroy model.
|
10
|
-
def initialize(model, destroy: false
|
10
|
+
def initialize(model, destroy: false)
|
11
11
|
@model = model
|
12
|
-
@destroy_context_type =
|
13
|
-
if context
|
14
|
-
c = context
|
15
|
-
|
16
|
-
if c.is_a?(Proc)
|
17
|
-
# @type var c: ((^() -> (context)) | (^(_ARLike) -> (context)))
|
18
|
-
if c.arity == 0
|
19
|
-
deprecator.warn(
|
20
|
-
'`:context` will be removed in 0.5.0. Use `:destroy` option instead. ' \
|
21
|
-
'for example, `models.push(model, context: -> { foo? ? :destroy : :save })` ' \
|
22
|
-
'is replaced by `models.push(model, destroy: -> { foo? })`.',
|
23
|
-
)
|
24
|
-
|
25
|
-
# @type var c: ^() -> (context)
|
26
|
-
-> { c.call == :destroy }
|
27
|
-
else
|
28
|
-
deprecator.warn(
|
29
|
-
'`:context` will be removed in 0.5.0. Use `:destroy` option instead. ' \
|
30
|
-
'for example, `models.push(model, context: ->(m) { m.bar? ? :destroy : :save })` ' \
|
31
|
-
'is replaced by `models.push(model, destroy: ->(m) { m.bar? })`.',
|
32
|
-
)
|
33
|
-
|
34
|
-
# @type var c: ^(_ARLike) -> (context)
|
35
|
-
->(model) { c.call(model) == :destroy }
|
36
|
-
end
|
37
|
-
elsif %i[save destroy].include?(c)
|
38
|
-
deprecator.warn(
|
39
|
-
'`:context` will be removed in 0.5.0. Use `:destroy` option instead. ' \
|
40
|
-
"for example, `models.push(model, context: #{c.inspect})` " \
|
41
|
-
"is replaced by `models.push(model, destroy: #{(c == :destroy).inspect})`.",
|
42
|
-
)
|
43
|
-
|
44
|
-
# @type var c: (:save | :destory)
|
45
|
-
c == :destroy
|
46
|
-
else
|
47
|
-
c
|
48
|
-
end
|
49
|
-
else
|
50
|
-
destroy
|
51
|
-
end
|
12
|
+
@destroy_context_type = destroy
|
52
13
|
end
|
53
14
|
|
54
15
|
delegate :errors, to: :model
|
55
16
|
|
17
|
+
# Determines whether to save or delete the target object.
|
18
|
+
# Depends on the `destroy` value of the InnerModel object initialization option.
|
19
|
+
#
|
20
|
+
# On the other hand, there are values `mark_for_destruction` and `marked_for_destruction?` in ActiveRecord.
|
21
|
+
# However, these values are not substituted here.
|
22
|
+
# These values only work if the `autosave` option is enabled for the parent model,
|
23
|
+
# and are not appropriate for other cases.
|
24
|
+
#
|
25
|
+
# @return [Boolean] returns true on destroy, false on save.
|
56
26
|
def destroy_context?
|
57
27
|
d = destroy_context_type
|
58
28
|
if d.is_a?(Proc)
|
59
29
|
if d.arity == 0
|
60
|
-
# @type var d: ^() ->
|
30
|
+
# @type var d: ^() -> bool
|
61
31
|
!!d.call
|
62
32
|
else
|
63
|
-
# @type var d: ^(_ARLike) ->
|
33
|
+
# @type var d: ^(_ARLike) -> bool
|
64
34
|
!!d.call(model)
|
65
35
|
end
|
66
36
|
else
|
@@ -106,13 +76,5 @@ module ActiveRecordCompose
|
|
106
76
|
private
|
107
77
|
|
108
78
|
attr_reader :model, :destroy_context_type
|
109
|
-
|
110
|
-
def deprecator
|
111
|
-
if ActiveRecord.respond_to?(:deprecator)
|
112
|
-
ActiveRecord.deprecator
|
113
|
-
else # for rails 7.0.x or lower
|
114
|
-
ActiveSupport::Deprecation
|
115
|
-
end
|
116
|
-
end
|
117
79
|
end
|
118
80
|
end
|
@@ -39,8 +39,8 @@ module ActiveRecordCompose
|
|
39
39
|
# @param destroy [Proc] when proc returning true, destroy model.
|
40
40
|
# @param destroy [Symbol] applies boolean value of result of sending a message to `owner` to evaluation.
|
41
41
|
# @return [self] returns itself.
|
42
|
-
def push(model, destroy: false
|
43
|
-
models << wrap(model, destroy
|
42
|
+
def push(model, destroy: false)
|
43
|
+
models << wrap(model, destroy:)
|
44
44
|
self
|
45
45
|
end
|
46
46
|
|
@@ -63,16 +63,7 @@ module ActiveRecordCompose
|
|
63
63
|
# @param model [Object] the model instance
|
64
64
|
# @return [self] Successful deletion
|
65
65
|
# @return [nil] If deletion fails
|
66
|
-
def delete(model
|
67
|
-
if !destroy.nil? || !context.nil?
|
68
|
-
# steep:ignore:start
|
69
|
-
deprecator.warn(
|
70
|
-
'In `InnerModelConnection#destroy`, the option values `destroy` and `context` are ignored. ' \
|
71
|
-
'These options will be removed in 0.5.0.',
|
72
|
-
)
|
73
|
-
# steep:ignore:end
|
74
|
-
end
|
75
|
-
|
66
|
+
def delete(model)
|
76
67
|
wrapped = wrap(model)
|
77
68
|
return nil unless models.delete(wrapped)
|
78
69
|
|
@@ -97,7 +88,7 @@ module ActiveRecordCompose
|
|
97
88
|
|
98
89
|
attr_reader :owner, :models
|
99
90
|
|
100
|
-
def wrap(model, destroy: false
|
91
|
+
def wrap(model, destroy: false)
|
101
92
|
if model.is_a?(ActiveRecordCompose::InnerModel) # steep:ignore
|
102
93
|
# @type var model: ActiveRecordCompose::InnerModel
|
103
94
|
model
|
@@ -107,15 +98,7 @@ module ActiveRecordCompose
|
|
107
98
|
destroy = -> { owner.__send__(method) }
|
108
99
|
end
|
109
100
|
# @type var model: ActiveRecordCompose::_ARLike
|
110
|
-
ActiveRecordCompose::InnerModel.new(model, destroy
|
111
|
-
end
|
112
|
-
end
|
113
|
-
|
114
|
-
def deprecator
|
115
|
-
if ActiveRecord.respond_to?(:deprecator)
|
116
|
-
ActiveRecord.deprecator # steep:ignore
|
117
|
-
else # for rails 7.0.x or lower
|
118
|
-
ActiveSupport::Deprecation
|
101
|
+
ActiveRecordCompose::InnerModel.new(model, destroy:)
|
119
102
|
end
|
120
103
|
end
|
121
104
|
end
|
@@ -15,8 +15,6 @@ module ActiveRecordCompose
|
|
15
15
|
end
|
16
16
|
|
17
17
|
type attribute_name = (String | Symbol)
|
18
|
-
type context = (:save | :destroy)
|
19
|
-
type context_proc = ((^() -> context) | (^(_ARLike) -> context))
|
20
18
|
type destroy_context_type = (bool | Symbol | (^() -> boolish) | (^(_ARLike) -> boolish))
|
21
19
|
|
22
20
|
module DelegateAttribute
|
@@ -35,19 +33,19 @@ module ActiveRecordCompose
|
|
35
33
|
def initialize: (Model) -> void
|
36
34
|
def each: () { (_ARLike) -> void } -> InnerModelCollection | () -> Enumerator[_ARLike, self]
|
37
35
|
def <<: (_ARLike) -> self
|
38
|
-
def push: (_ARLike, ?destroy: destroy_context_type
|
36
|
+
def push: (_ARLike, ?destroy: destroy_context_type) -> self
|
39
37
|
def empty?: -> bool
|
40
38
|
def clear: -> self
|
41
|
-
def delete: (_ARLike | InnerModel
|
39
|
+
def delete: (_ARLike | InnerModel) -> InnerModelCollection?
|
42
40
|
|
43
41
|
private
|
44
42
|
attr_reader owner: Model
|
45
43
|
attr_reader models: Array[InnerModel]
|
46
|
-
def wrap: (_ARLike | InnerModel, ?destroy: destroy_context_type
|
44
|
+
def wrap: (_ARLike | InnerModel, ?destroy: destroy_context_type) -> InnerModel
|
47
45
|
end
|
48
46
|
|
49
47
|
class InnerModel
|
50
|
-
def initialize: (_ARLike, ?destroy: destroy_context_type
|
48
|
+
def initialize: (_ARLike, ?destroy: destroy_context_type) -> void
|
51
49
|
def destroy_context?: -> bool
|
52
50
|
def save: -> bool
|
53
51
|
def save!: -> untyped
|
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.
|
4
|
+
version: 0.5.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-09
|
11
|
+
date: 2024-10-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -52,7 +52,7 @@ metadata:
|
|
52
52
|
homepage_uri: https://github.com/hamajyotan/active_record_compose
|
53
53
|
source_code_uri: https://github.com/hamajyotan/active_record_compose
|
54
54
|
changelog_uri: https://github.com/hamajyotan/active_record_compose/blob/main/CHANGELOG.md
|
55
|
-
documentation_uri: https://www.rubydoc.info/gems/active_record_compose/0.
|
55
|
+
documentation_uri: https://www.rubydoc.info/gems/active_record_compose/0.5.0
|
56
56
|
rubygems_mfa_required: 'true'
|
57
57
|
post_install_message:
|
58
58
|
rdoc_options: []
|
@@ -69,7 +69,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
69
69
|
- !ruby/object:Gem::Version
|
70
70
|
version: '0'
|
71
71
|
requirements: []
|
72
|
-
rubygems_version: 3.5.
|
72
|
+
rubygems_version: 3.5.11
|
73
73
|
signing_key:
|
74
74
|
specification_version: 4
|
75
75
|
summary: activemodel form object pattern
|