subroutine 0.10.0.beta6 → 0.10.0.beta7
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/lib/subroutine/association_fields.rb +24 -11
- data/lib/subroutine/version.rb +1 -1
- data/test/subroutine/association_test.rb +11 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 27287a4d05110ba46ae806e8e3b38f4598c0bb8f69fbdb97f41a53eea025e955
|
4
|
+
data.tar.gz: 22da96f1411d3887a41f42a2a7ca493587ed404aa5760da45447c7ba443dd6ef
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8cef15bcb25e9792eee2a0073f9d428784cda0370aa40c49881db5e124821b9c0036e83cdfc173233f4a4a76ea8be1ce842a0b309bdd02023f7e1333219f19a6
|
7
|
+
data.tar.gz: e0797ab6a5153718d02ef4bb9d7dbeed5f793a8ef574d504627c2c109ff8147a8ed290cc555c08a5dd62cb5f226998d40faec1d8a6f14a10163d93b6974d1892
|
@@ -18,6 +18,11 @@ module Subroutine
|
|
18
18
|
|
19
19
|
end
|
20
20
|
|
21
|
+
attr_reader :association_cache
|
22
|
+
|
23
|
+
alias_method :setup_fields_without_association, :setup_fields
|
24
|
+
alias_method :setup_fields, :setup_fields_with_association
|
25
|
+
|
21
26
|
alias_method :set_field_without_association, :set_field
|
22
27
|
alias_method :set_field, :set_field_with_association
|
23
28
|
|
@@ -83,33 +88,40 @@ module Subroutine
|
|
83
88
|
|
84
89
|
end
|
85
90
|
|
91
|
+
def setup_fields_with_association(*args)
|
92
|
+
@association_cache = {}
|
93
|
+
setup_fields_without_association(*args)
|
94
|
+
end
|
95
|
+
|
86
96
|
def set_field_with_association(field_name, value, opts = {})
|
87
97
|
config = get_field_config(field_name)
|
88
98
|
|
89
99
|
if config&.behavior == :association
|
90
|
-
|
100
|
+
maybe_raise_on_association_type_mismatch!(config, value)
|
91
101
|
set_field(config.foreign_type_method, value&.class&.name, opts) if config.polymorphic?
|
92
102
|
set_field(config.foreign_key_method, value&.id, opts)
|
93
|
-
|
94
|
-
|
95
|
-
|
103
|
+
association_cache[config.field_name] = value
|
104
|
+
else
|
105
|
+
if config&.behavior == :association_component
|
106
|
+
clear_field_without_association(config.association_name)
|
107
|
+
end
|
96
108
|
|
97
|
-
|
109
|
+
set_field_without_association(field_name, value, opts)
|
110
|
+
end
|
98
111
|
end
|
99
112
|
|
100
113
|
def get_field_with_association(field_name)
|
101
114
|
config = get_field_config(field_name)
|
102
115
|
|
103
116
|
if config&.behavior == :association
|
104
|
-
stored_result =
|
117
|
+
stored_result = association_cache[config.field_name]
|
105
118
|
return stored_result unless stored_result.nil?
|
106
119
|
|
107
120
|
fk = send(config.foreign_key_method)
|
108
121
|
type = send(config.foreign_type_method)
|
109
122
|
|
110
123
|
result = fetch_association_instance(type, fk, config.unscoped?)
|
111
|
-
|
112
|
-
result
|
124
|
+
association_cache[config.field_name] = result
|
113
125
|
else
|
114
126
|
get_field_without_association(field_name)
|
115
127
|
end
|
@@ -121,9 +133,10 @@ module Subroutine
|
|
121
133
|
if config&.behavior == :association
|
122
134
|
clear_field(config.foreign_type_method) if config.polymorphic?
|
123
135
|
clear_field(config.foreign_key_method)
|
136
|
+
association_cache.delete(config.field_name)
|
137
|
+
else
|
138
|
+
clear_field_without_association(field_name)
|
124
139
|
end
|
125
|
-
|
126
|
-
clear_field_without_association(field_name)
|
127
140
|
end
|
128
141
|
|
129
142
|
def field_provided_with_association?(field_name)
|
@@ -156,7 +169,7 @@ module Subroutine
|
|
156
169
|
scope.find(_fk)
|
157
170
|
end
|
158
171
|
|
159
|
-
def
|
172
|
+
def maybe_raise_on_association_type_mismatch!(config, record)
|
160
173
|
return if config.polymorphic?
|
161
174
|
return if record.nil?
|
162
175
|
|
data/lib/subroutine/version.rb
CHANGED
@@ -187,5 +187,16 @@ module Subroutine
|
|
187
187
|
assert_equal account, op.user
|
188
188
|
end
|
189
189
|
|
190
|
+
def test_params_only_contain_the_id_and_type_of_associations
|
191
|
+
user = ::User.new(id: 1)
|
192
|
+
op = SimpleAssociationOp.new(user: user)
|
193
|
+
op.user
|
194
|
+
assert_equal({ "user_id" => 1 }, op.params)
|
195
|
+
|
196
|
+
op = PolymorphicAssociationOp.new(admin: user)
|
197
|
+
op.admin
|
198
|
+
assert_equal({ "admin_id" => 1, "admin_type" => "User" }, op.params)
|
199
|
+
end
|
200
|
+
|
190
201
|
end
|
191
202
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: subroutine
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.10.0.
|
4
|
+
version: 0.10.0.beta7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mike Nelson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-01-
|
11
|
+
date: 2020-01-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|