subroutine 0.10.0.beta6 → 0.10.0.beta7

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: c22ca4172e9f569b15bbccae7c59dc7df49dfca43f367bd1cfcf1d771d77e6ab
4
- data.tar.gz: 84cf68b11db90711abbf87b480a36f852673e2ed7c975404b1ffc085bd3269df
3
+ metadata.gz: 27287a4d05110ba46ae806e8e3b38f4598c0bb8f69fbdb97f41a53eea025e955
4
+ data.tar.gz: 22da96f1411d3887a41f42a2a7ca493587ed404aa5760da45447c7ba443dd6ef
5
5
  SHA512:
6
- metadata.gz: 780e4d568a18e6e6d19a2f92bfbd3e12cb3433e0e11a1a3986a9af7552efdcf69f546d5d95639704a4e4dbcbe8e7dfb5567ee5741d6bc8c811e35058420e3d21
7
- data.tar.gz: 3191b5dbe495a426db558b14f5ff22ce3b2bfddd87b5fa60d9606f2b107fb57f5f1830a235982aa2d1be471308b8cde278160c5747d54a266ea90f2fe7de9123
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
- maybe_raise_on_type_mismatch!(config, value)
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
- elsif config&.behavior == :association_component
94
- clear_field_without_association(config.association_name)
95
- end
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
- set_field_without_association(field_name, value, opts)
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 = get_field_without_association(field_name)
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
- set_field_without_association(field_name, result, track_provided: false) unless result.nil?
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 maybe_raise_on_type_mismatch!(config, record)
172
+ def maybe_raise_on_association_type_mismatch!(config, record)
160
173
  return if config.polymorphic?
161
174
  return if record.nil?
162
175
 
@@ -5,7 +5,7 @@ module Subroutine
5
5
  MAJOR = 0
6
6
  MINOR = 10
7
7
  PATCH = 0
8
- PRE = "beta6"
8
+ PRE = "beta7"
9
9
 
10
10
  VERSION = [MAJOR, MINOR, PATCH, PRE].compact.join(".")
11
11
 
@@ -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.beta6
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-24 00:00:00.000000000 Z
11
+ date: 2020-01-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel