File tree Expand file tree Collapse file tree 13 files changed +71
-11
lines changed Expand file tree Collapse file tree 13 files changed +71
-11
lines changed Original file line number Diff line number Diff line change 6
6
require "forwardable"
7
7
require "time"
8
8
require "set"
9
+ require "ruby2_keywords"
9
10
10
11
require "active_support"
11
12
require "active_support/core_ext"
Original file line number Diff line number Diff line change @@ -422,7 +422,7 @@ def integrate(document)
422
422
# @param [ Proc ] block Optional block to pass.
423
423
#
424
424
# @return [ Criteria, Object ] A Criteria or return value from the target.
425
- def method_missing ( name , *args , &block )
425
+ ruby2_keywords def method_missing ( name , *args , &block )
426
426
return super if _target . respond_to? ( name )
427
427
klass . send ( :with_scope , criteria ) do
428
428
criteria . public_send ( name , *args , &block )
Original file line number Diff line number Diff line change @@ -133,12 +133,12 @@ def characterize_one(document)
133
133
# @param [ String, Symbol ] name The name of the method.
134
134
# @param [ Array ] args The arguments passed to the method.
135
135
#
136
- def method_missing ( name , *args , &block )
136
+ ruby2_keywords def method_missing ( name , *args , &block )
137
137
_target . send ( name , *args , &block )
138
138
end
139
139
140
140
# @api private
141
- def respond_to_missing? ( name , *args )
141
+ ruby2_keywords def respond_to_missing? ( name , *args )
142
142
_target . respond_to? ( name , *args )
143
143
end
144
144
Original file line number Diff line number Diff line change @@ -532,7 +532,7 @@ def set_base(document)
532
532
end
533
533
end
534
534
535
- def method_missing ( name , *args , &block )
535
+ ruby2_keywords def method_missing ( name , *args , &block )
536
536
entries . send ( name , *args , &block )
537
537
end
538
538
Original file line number Diff line number Diff line change @@ -443,7 +443,7 @@ def cascade!(document)
443
443
# @return [ Criteria, Object ] A Criteria or return value from the target.
444
444
#
445
445
# @since 2.0.0.beta.1
446
- def method_missing ( name , *args , &block )
446
+ ruby2_keywords def method_missing ( name , *args , &block )
447
447
if _target . respond_to? ( name )
448
448
_target . send ( name , *args , &block )
449
449
else
Original file line number Diff line number Diff line change @@ -567,7 +567,7 @@ def initialize_copy(other)
567
567
# @return [ Object ] The result of the method call.
568
568
#
569
569
# @since 1.0.0
570
- def method_missing ( name , *args , &block )
570
+ ruby2_keywords def method_missing ( name , *args , &block )
571
571
if klass . respond_to? ( name )
572
572
klass . send ( :with_scope , self ) do
573
573
klass . send ( name , *args , &block )
Original file line number Diff line number Diff line change @@ -125,7 +125,7 @@ def run_before_callbacks(*kinds)
125
125
# @return [ Document ] The document
126
126
#
127
127
# @since 2.3.0
128
- def run_callbacks ( kind , *args , &block )
128
+ ruby2_keywords def run_callbacks ( kind , *args , &block )
129
129
cascadable_children ( kind ) . each do |child |
130
130
if child . run_callbacks ( child_callback_type ( kind , child ) , *args ) == false
131
131
return false
Original file line number Diff line number Diff line change @@ -38,6 +38,18 @@ Gem::Specification.new do |s|
38
38
s . add_dependency ( "activemodel" , [ ">=6.0" , "<6.2" ] )
39
39
end
40
40
s . add_dependency ( "mongo" , [ '>=2.10.5' , '<3.0.0' ] )
41
+ # Using this gem is recommended for handling argument delegation issues,
42
+ # especially if support for 2.6 or prior is required.
43
+ # See https://www.ruby-lang.org/en/news/2019/12/12/separation-of-positional-and-keyword-arguments-in-ruby-3-0/#delegation
44
+ #
45
+ # We have a bunch of complex delegation logic, including various method_missngs.
46
+ # If we try to fix them "right", it will add too much logic. We will have to
47
+ # handle different Ruby versions (including minor ones, Ruby 2.6 and 2.7
48
+ # behave differently), hash key types (strings vs symbols), ways of passing
49
+ # arguments (with curly braces vs without ones).
50
+ #
51
+ # Therefore, usage of this gem looks like a reasonable solution at the moment.
52
+ s . add_dependency ( "ruby2_keywords" , "~> 0.0.5" )
41
53
42
54
s . add_development_dependency ( "bson" , [ '>=4.9.4' , '<5.0.0' ] )
43
55
Original file line number Diff line number Diff line change @@ -2449,13 +2449,26 @@ class TrackingIdValidationHistory
2449
2449
end
2450
2450
2451
2451
context "when providing a criteria class method" do
2452
+ context "without keyword arguments" do
2452
2453
2453
- let ( :addresses ) do
2454
- person . addresses . california
2454
+ let ( :addresses ) do
2455
+ person . addresses . california
2456
+ end
2457
+
2458
+ it "applies the criteria to the documents" do
2459
+ expect ( addresses ) . to eq ( [ address_one ] )
2460
+ end
2455
2461
end
2456
2462
2457
- it "applies the criteria to the documents" do
2458
- expect ( addresses ) . to eq ( [ address_one ] )
2463
+ context "with keyword arguments" do
2464
+
2465
+ let ( :addresses ) do
2466
+ person . addresses . city_and_state ( city : "Sacramento" , state : "CA" )
2467
+ end
2468
+
2469
+ it "applies the criteria to the documents" do
2470
+ expect ( addresses ) . to eq ( [ ] )
2471
+ end
2459
2472
end
2460
2473
end
2461
2474
Original file line number Diff line number Diff line change @@ -1349,4 +1349,21 @@ class C
1349
1349
end
1350
1350
end
1351
1351
end
1352
+
1353
+ describe "#method_missing" do
1354
+ let! ( :person ) do
1355
+ Person . create
1356
+ end
1357
+
1358
+ let! ( :game ) do
1359
+ Game . create ( person : person )
1360
+ end
1361
+
1362
+ it 'handles keyword args' do
1363
+ expect do
1364
+ game . person . set_personal_data ( ssn : '123' , age : 25 )
1365
+ end . not_to raise_error
1366
+ end
1367
+
1368
+ end
1352
1369
end
You can’t perform that action at this time.
0 commit comments