subroutine 2.2.0 → 2.3.1
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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0a85a75ee28c2c27b1d144059b1af01681aee27987ca2fdad9a0736b2270c824
|
4
|
+
data.tar.gz: 73bf392120764aa30da22c18b756ea8483f35b34e14e473fae6f7d495a4cbc9f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ea38c80c4bbf80eb1763f1052b5f0a906268b1eb6416bcd6e4f2f39a678af16395555ddc02b56867671413076680fb65343d7462974e594f112e8cdb2136f52b
|
7
|
+
data.tar.gz: b5590640ea893a227ef850fb052fb04cac6d46ccc0af2cb33b7b86972518c1b5b7988114fab0faa6472b215f8f927825c9699a08266fd6713ed8c84a88846065
|
@@ -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:
|
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,25 @@ 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
|
+
@determined_foreign_type ||= begin
|
94
|
+
klass = inferred_foreign_type&.constantize
|
95
|
+
if klass && klass.respond_to?(:type_for_attribute)
|
96
|
+
case klass.type_for_attribute(find_by)&.type&.to_sym
|
97
|
+
when :string
|
98
|
+
:string
|
99
|
+
else
|
100
|
+
:integer
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
87
106
|
end
|
88
107
|
end
|
89
108
|
end
|
@@ -66,6 +66,7 @@ end
|
|
66
66
|
::Subroutine::TypeCaster.register :foreign_key do |value, options = {}|
|
67
67
|
next nil if value.blank?
|
68
68
|
|
69
|
+
next ::Subroutine::TypeCaster.cast(value, type: options[:foreign_key_type].call) if options[:foreign_key_type].respond_to?(:call)
|
69
70
|
next ::Subroutine::TypeCaster.cast(value, type: options[:foreign_key_type]) if options[:foreign_key_type]
|
70
71
|
next ::Subroutine::TypeCaster.cast(value, type: :integer) if options[:name] && options[:name].to_s.end_with?("_id")
|
71
72
|
|
data/lib/subroutine/version.rb
CHANGED
@@ -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.
|
4
|
+
version: 2.3.1
|
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:
|
11
|
+
date: 2023-03-15 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.
|
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:
|