subroutine 2.0.0.beta → 2.0.0.beta2

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: d0ade77ec5abb9601e1dce7bc53e34baac35c978a41aef3268b57ee5b9def6e5
4
- data.tar.gz: c450947886efc34b26b202b52202909f44735a2d5f26a3c1af51fdf93be509c6
3
+ metadata.gz: bc8eb530552fea8e2bacd033372826e11c57734d9695973ebca956c76aa2ed93
4
+ data.tar.gz: 908764a3fc127881b4c719c2c5523ae280771683e765ea8f47ddda52bed76a33
5
5
  SHA512:
6
- metadata.gz: c171a9bb82076ed4f5e407375d19e423a5e6bba0211192109d300e3b7372d537a0bac3d67ef5c480ef9af34ddd2a849a07fef79459f5597ea97e195cad239fe7
7
- data.tar.gz: 52502e8dd504de2d59faa4665ab7c755c9ff6ba8b951e79acaa264c4d257f09a20da58977f1565e8d121023d6a2f5291cd4528eca143d697252b2f6a27d4197b
6
+ metadata.gz: 4de2ad64fede40a510afe5b0af073eb6fae1800a61e2f9387337c1d46475ec439c1c35cb393ecececb3a5954b7c9e1b32be10e6be963dc7ce3fb840d40a3b368
7
+ data.tar.gz: 9ec24f1a6ce47edb502bded197096dec4d1ff444c4d3b0fda64291111aa7a29e67e7bd4f8e01a018c72795117c40e16fa3c68131b64db346e3c1073611c8134e
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.5.1
1
+ 2.5.7
data/CHANGELOG.MD CHANGED
@@ -81,7 +81,7 @@ class AccountUpdateOp < ::Op
81
81
  end
82
82
  ```
83
83
 
84
- ActionController::Parameters from Rails 5+ are now transformed to as hash in `Subroutine::Fields` by default. This means strong parameters are essentially unused when passing `Subroutine::Fields`.
84
+ ActionController::Parameters from Rails 5+ are now transformed to a hash in `Subroutine::Fields` by default. This means strong parameters are essentially unused when passing `Subroutine::Fields`.
85
85
 
86
86
  Read more about field management and access in https://github.com/guideline-tech/subroutine/wiki/Param-Usage
87
87
 
@@ -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)
63
+ build_child_field(foreign_key_method, type: :foreign_key, foreign_key_type: config[:foreign_key_type])
64
64
  end
65
65
 
66
66
  def build_foreign_type_field
@@ -78,7 +78,10 @@ module Subroutine
78
78
  protected
79
79
 
80
80
  def build_child_field(name, opts = {})
81
- ComponentConfiguration.new(name, inheritable_options.merge(opts).merge(association_name: as))
81
+ child_opts = inheritable_options
82
+ child_opts.merge!(opts)
83
+ child_opts[:association_name] = as
84
+ ComponentConfiguration.new(name, child_opts)
82
85
  end
83
86
 
84
87
  end
@@ -101,6 +101,7 @@ module Subroutine
101
101
  opts[:groups] = Array(groups).map(&:to_sym).presence
102
102
  opts.delete(:group)
103
103
  opts[:aka] = opts[:aka].to_sym if opts[:aka]
104
+ opts[:name] = field_name
104
105
  opts
105
106
  end
106
107
 
@@ -63,6 +63,15 @@ end
63
63
  String(value)
64
64
  end
65
65
 
66
+ ::Subroutine::TypeCaster.register :foreign_key do |value, options = {}|
67
+ next nil if value.blank?
68
+
69
+ next ::Subroutine::TypeCaster.cast(value, type: options[:foreign_key_type]) if options[:foreign_key_type]
70
+ next ::Subroutine::TypeCaster.cast(value, type: :integer) if options[:name] && options[:name].to_s.end_with?("_id")
71
+
72
+ value
73
+ end
74
+
66
75
  ::Subroutine::TypeCaster.register :boolean, :bool do |value, _options = {}|
67
76
  !!(String(value) =~ /^(yes|true|1|ok)$/)
68
77
  end
@@ -5,7 +5,7 @@ module Subroutine
5
5
  MAJOR = 2
6
6
  MINOR = 0
7
7
  PATCH = 0
8
- PRE = "beta"
8
+ PRE = "beta2"
9
9
 
10
10
  VERSION = [MAJOR, MINOR, PATCH, PRE].compact.join(".")
11
11
 
@@ -83,20 +83,32 @@ module Subroutine
83
83
  def test_it_allows_foreign_key_to_be_set
84
84
  all_mock = mock
85
85
  ::User.expects(:all).returns(all_mock)
86
- all_mock.expects(:find_by!).with(id: "foobarbaz").returns(doug)
86
+ all_mock.expects(:find_by!).with(id: 10).returns(doug)
87
87
 
88
- op = ::AssociationWithForeignKeyOp.new(user_identifier: "foobarbaz")
88
+ op = ::AssociationWithForeignKeyOp.new(owner_id: 10)
89
89
  assert_equal doug, op.user
90
- assert_equal "user_identifier", op.field_configurations[:user][:foreign_key]
90
+ assert_equal "owner_id", op.field_configurations[:user][:foreign_key]
91
+ end
92
+
93
+ def test_the_foreign_key_is_cast
94
+ all_mock = mock
95
+ ::User.expects(:all).returns(all_mock)
96
+ all_mock.expects(:find_by!).with(id: 10).returns(doug)
97
+
98
+ op = ::AssociationWithForeignKeyOp.new(owner_id: "10")
99
+ assert_equal doug, op.user
100
+ assert_equal 10, op.owner_id
101
+ assert_equal "owner_id", op.field_configurations[:user][:foreign_key]
91
102
  end
92
103
 
93
104
  def test_it_allows_a_foreign_key_and_find_by_to_be_set
94
105
  all_mock = mock
95
106
  ::User.expects(:all).returns(all_mock)
96
- all_mock.expects(:find_by!).with(email_address: doug.email_address).returns(doug)
107
+ all_mock.expects(:find_by!).with(email_address: "foo@bar.com").returns(doug)
97
108
 
98
- op = ::AssociationWithFindByAndForeignKeyOp.new(email_address: doug.email_address)
109
+ op = ::AssociationWithFindByAndForeignKeyOp.new(email_address: "foo@bar.com")
99
110
  assert_equal doug, op.user
111
+ assert_equal "foo@bar.com", op.email_address
100
112
  assert_equal "email_address", op.field_configurations[:user][:find_by]
101
113
  end
102
114
 
@@ -119,7 +131,7 @@ module Subroutine
119
131
  def test_values_are_correct_for_foreign_key_usage
120
132
  op = ::AssociationWithForeignKeyOp.new(user: doug)
121
133
  assert_equal doug, op.user
122
- assert_equal doug.id, op.user_identifier
134
+ assert_equal doug.id, op.owner_id
123
135
  end
124
136
 
125
137
  def test_values_are_correct_for_both_foreign_key_and_find_by_usage
@@ -261,5 +261,28 @@ module Subroutine
261
261
  op.date_input = "2015-13-01"
262
262
  end
263
263
  end
264
+
265
+ def test_foreign_key_inputs
266
+ op.fk_input_owner_id = nil
267
+ assert_nil op.fk_input_owner_id
268
+
269
+ op.fk_input_owner_id = ""
270
+ assert_nil op.fk_input_owner_id
271
+
272
+ op.fk_input_owner_id = "19402"
273
+ assert_equal 19402, op.fk_input_owner_id
274
+
275
+ op.fk_input_owner_id = "19402.0"
276
+ assert_equal 19402, op.fk_input_owner_id
277
+
278
+ op.fk_input_email_address = nil
279
+ assert_nil op.fk_input_email_address
280
+
281
+ op.fk_input_email_address = ""
282
+ assert_nil op.fk_input_email_address
283
+
284
+ op.fk_input_email_address = "foo@bar.com"
285
+ assert_equal "foo@bar.com", op.fk_input_email_address
286
+ end
264
287
  end
265
288
  end
data/test/support/ops.rb CHANGED
@@ -166,6 +166,8 @@ class TypeCastOp < ::Subroutine::Op
166
166
  array :array_input, default: "foo"
167
167
  array :type_array_input, of: :integer
168
168
  file :file_input
169
+ foreign_key :fk_input_owner_id
170
+ foreign_key :fk_input_email_address, foreign_key_type: :string
169
171
 
170
172
  end
171
173
 
@@ -314,13 +316,13 @@ end
314
316
 
315
317
  class AssociationWithForeignKeyOp < ::OpWithAssociation
316
318
 
317
- association :user, foreign_key: "user_identifier"
319
+ association :user, foreign_key: "owner_id"
318
320
 
319
321
  end
320
322
 
321
323
  class AssociationWithFindByKeyOp < ::OpWithAssociation
322
324
 
323
- association :user, find_by: "email_address"
325
+ association :user, find_by: "email_address", foreign_key_type: :string
324
326
 
325
327
  end
326
328
 
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.0.0.beta
4
+ version: 2.0.0.beta2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Nelson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-06-23 00:00:00.000000000 Z
11
+ date: 2022-02-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -218,7 +218,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
218
218
  - !ruby/object:Gem::Version
219
219
  version: 1.3.1
220
220
  requirements: []
221
- rubygems_version: 3.0.6
221
+ rubygems_version: 3.3.7
222
222
  signing_key:
223
223
  specification_version: 4
224
224
  summary: Feature-driven operation objects.