subroutine 2.2.0 → 2.3.0

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: 8695d158b7e6ab0464d8e7b4a6c9a641b5dde81f422ff139c29eab8bc8512e75
4
- data.tar.gz: 61a393835d542a613f91335581c114fbd44764809ed78c2f4bd14f463b3c604d
3
+ metadata.gz: a130a9c00d47631ee67621f3772ff5d050c348af94c9ec99a3ef4d90f0164079
4
+ data.tar.gz: d7c099c1c586bfaf516955b1f94aa86188fbbe0194e42021d0f0191341efb482
5
5
  SHA512:
6
- metadata.gz: '011484de567ba987afdc22abcd3aa198ae1cf62ecca93c42412cacab2ef14832d5eb343d64150cbed7f8fc06526709a05918a4a5e0cd8f655e4c6d903cc9c40f'
7
- data.tar.gz: 40bda69b27c0ec4fd2ca152136918809da4a5b1793b9732bc8e0910714213a4696230a21fc3872e76294d75b6550ba106c52ec0a262d334b2b2da4c8b06bc132
6
+ metadata.gz: ffa8be2183e84ad07c523251c499673a169f7b9968d79164f7662082ffc9497027f4344cb12fda6b602aaf2b6884a8be17690a58448f50138f4fb547f9d6e0cf
7
+ data.tar.gz: c6130d737e8860e05d529e679e4a2f2be2de3f7114da5e97c5f8d3844463f3fc6f9e3a2f23da50891282989adcbd08759be9f243ff3aaf25adc978255cd18d43
@@ -60,7 +60,7 @@ module Subroutine
60
60
  end
61
61
 
62
62
  def build_foreign_key_field
63
- build_child_field(foreign_key_method, type: :foreign_key, foreign_key_type: config[:foreign_key_type])
63
+ build_child_field(foreign_key_method, type: :foreign_key, foreign_key_type: determine_foreign_key_type)
64
64
  end
65
65
 
66
66
  def build_foreign_type_field
@@ -84,6 +84,23 @@ module Subroutine
84
84
  ComponentConfiguration.new(name, child_opts)
85
85
  end
86
86
 
87
+ def determine_foreign_key_type
88
+ return config[:foreign_key_type] if config[:foreign_key_type]
89
+
90
+ # TODO: Make this logic work for polymorphic associations.
91
+ return if polymorphic?
92
+
93
+ klass = inferred_foreign_type&.constantize
94
+ if klass && klass.respond_to?(:type_for_attribute)
95
+ case klass.type_for_attribute(find_by)&.type&.to_sym
96
+ when :string
97
+ :string
98
+ else
99
+ :integer
100
+ end
101
+ end
102
+ end
103
+
87
104
  end
88
105
  end
89
106
  end
@@ -3,7 +3,7 @@
3
3
  module Subroutine
4
4
 
5
5
  MAJOR = 2
6
- MINOR = 2
6
+ MINOR = 3
7
7
  PATCH = 0
8
8
  PRE = nil
9
9
 
@@ -13,6 +13,10 @@ module Subroutine
13
13
  @fred ||= ::User.new(id: 2, email_address: "fred@example.com")
14
14
  end
15
15
 
16
+ def murphy
17
+ @murphy ||= ::StringIdUser.new(id: "ABACABADABACABA", email_address: "murphy@example.com")
18
+ end
19
+
16
20
  def account
17
21
  @account ||= ::Account.new(id: 1)
18
22
  end
@@ -43,6 +47,16 @@ module Subroutine
43
47
  assert_equal doug, op.user
44
48
  end
45
49
 
50
+ def test_it_looks_up_an_association_with_string_ids
51
+ all_mock = mock
52
+
53
+ ::StringIdUser.expects(:all).returns(all_mock)
54
+ all_mock.expects(:find_by!).with(id: "ABACABADABACABA").returns(murphy)
55
+
56
+ op = ::SimpleAssociationWithStringIdOp.new(string_id_user_id: murphy.id)
57
+ assert_equal murphy, op.string_id_user
58
+ end
59
+
46
60
  def test_it_sanitizes_types
47
61
  all_mock = mock
48
62
 
@@ -122,6 +136,16 @@ module Subroutine
122
136
  assert_equal "email_address", op.field_configurations[:user][:find_by]
123
137
  end
124
138
 
139
+ def test_it_allows_a_find_by_to_be_set_with_implicit_string
140
+ all_mock = mock
141
+ ::User.expects(:all).returns(all_mock)
142
+ all_mock.expects(:find_by!).with(email_address: doug.email_address).returns(doug)
143
+
144
+ op = ::AssociationWithImplicitStringFindByOp.new(user_id: doug.email_address)
145
+ assert_equal doug, op.user
146
+ assert_equal "email_address", op.field_configurations[:user][:find_by]
147
+ end
148
+
125
149
  def test_values_are_correct_for_find_by_usage
126
150
  op = ::AssociationWithFindByKeyOp.new(user: doug)
127
151
  assert_equal doug, op.user
data/test/support/ops.rb CHANGED
@@ -31,6 +31,23 @@ class User
31
31
  find_by(params) || raise
32
32
  end
33
33
 
34
+ def self.type_for_attribute(attribute)
35
+ case attribute
36
+ when :id
37
+ Struct.new(:type).new(:integer)
38
+ else
39
+ Struct.new(:type).new(:string)
40
+ end
41
+ end
42
+
43
+ end
44
+
45
+ class StringIdUser < ::User
46
+
47
+ def self.type_for_attribute(attribute)
48
+ Struct.new(:type).new(:string)
49
+ end
50
+
34
51
  end
35
52
 
36
53
  class AdminUser < ::User
@@ -325,6 +342,12 @@ class SimpleAssociationOp < ::OpWithAssociation
325
342
 
326
343
  end
327
344
 
345
+ class SimpleAssociationWithStringIdOp < ::OpWithAssociation
346
+
347
+ association :string_id_user
348
+
349
+ end
350
+
328
351
  class UnscopedSimpleAssociationOp < ::OpWithAssociation
329
352
 
330
353
  association :user, unscoped: true, allow_overwrite: true
@@ -355,6 +378,12 @@ class AssociationWithFindByKeyOp < ::OpWithAssociation
355
378
 
356
379
  end
357
380
 
381
+ class AssociationWithImplicitStringFindByOp < ::OpWithAssociation
382
+
383
+ association :user, find_by: "email_address"
384
+
385
+ end
386
+
358
387
  class AssociationWithFindByAndForeignKeyOp < ::OpWithAssociation
359
388
 
360
389
  association :user, foreign_key: "email_address", find_by: "email_address"
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: 2.2.0
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Nelson
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-10-26 00:00:00.000000000 Z
11
+ date: 2023-03-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -205,7 +205,7 @@ homepage: https://github.com/mnelson/subroutine
205
205
  licenses:
206
206
  - MIT
207
207
  metadata: {}
208
- post_install_message:
208
+ post_install_message:
209
209
  rdoc_options: []
210
210
  require_paths:
211
211
  - lib
@@ -220,8 +220,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
220
220
  - !ruby/object:Gem::Version
221
221
  version: '0'
222
222
  requirements: []
223
- rubygems_version: 3.1.6
224
- signing_key:
223
+ rubygems_version: 3.3.23
224
+ signing_key:
225
225
  specification_version: 4
226
226
  summary: Feature-driven operation objects.
227
227
  test_files: