Skip to content

Commit 5e41670

Browse files
kyoshidajpp
andauthored
MONGOID-5435 Fix ArgumentError when block has positional and keyword arguments (#5398)
* Fix ArgumentError when block has positional and keyword arguments * add assertion on result * Switch to use of ruby2_keywords for prior to Ruby 2.7 Co-authored-by: Oleg Pudeyev <[email protected]>
1 parent 7a555df commit 5e41670

File tree

2 files changed

+44
-24
lines changed

2 files changed

+44
-24
lines changed

lib/mongoid/scopable.rb

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -289,15 +289,17 @@ def check_scope_validity(value)
289289
# @return [ Method ] The defined method.
290290
def define_scope_method(name)
291291
singleton_class.class_eval do
292-
define_method(name) do |*args|
293-
scoping = _declared_scopes[name]
294-
scope = instance_exec(*args, &scoping[:scope])
295-
extension = scoping[:extension]
296-
to_merge = scope || queryable
297-
criteria = to_merge.empty_and_chainable? ? to_merge : with_default_scope.merge(to_merge)
298-
criteria.extend(extension)
299-
criteria
300-
end
292+
ruby2_keywords(
293+
define_method(name) do |*args|
294+
scoping = _declared_scopes[name]
295+
scope = instance_exec(*args, &scoping[:scope])
296+
extension = scoping[:extension]
297+
to_merge = scope || queryable
298+
criteria = to_merge.empty_and_chainable? ? to_merge : with_default_scope.merge(to_merge)
299+
criteria.extend(extension)
300+
criteria
301+
end
302+
)
301303
end
302304
end
303305

spec/mongoid/scopable_spec.rb

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -481,27 +481,45 @@ class << Band
481481

482482
context "when a block is provided" do
483483

484-
before do
485-
Band.scope(:active, ->{ Band.where(active: true) }) do
486-
def add_origin
487-
tap { |c| c.selector[:origin] = "Deutschland" }
488-
end
484+
context "when with optional and keyword arguments" do
485+
before do
486+
Band.scope(:named_by, ->(name, deleted: false) {
487+
Band.where(name: name, deleted: deleted)
488+
})
489489
end
490-
end
491490

492-
after do
493-
class << Band
494-
undef_method :active
491+
let(:scope) do
492+
Band.named_by("Emily", deleted: true)
495493
end
496-
Band._declared_scopes.clear
497-
end
498494

499-
let(:scope) do
500-
Band.active.add_origin
495+
it "sets the conditions from keyword arguments" do
496+
scope.selector.should == {'name' => 'Emily', 'deleted' => true}
497+
end
501498
end
502499

503-
it "adds the extension to the scope" do
504-
expect(scope.selector).to eq({ "active" => true, "origin" => "Deutschland" })
500+
context "when without arguments" do
501+
before do
502+
Band.scope(:active, ->{ Band.where(active: true) }) do
503+
def add_origin
504+
tap { |c| c.selector[:origin] = "Deutschland" }
505+
end
506+
end
507+
end
508+
509+
after do
510+
class << Band
511+
undef_method :active
512+
end
513+
Band._declared_scopes.clear
514+
end
515+
516+
let(:scope) do
517+
Band.active.add_origin
518+
end
519+
520+
it "adds the extension to the scope" do
521+
expect(scope.selector).to eq({ "active" => true, "origin" => "Deutschland" })
522+
end
505523
end
506524
end
507525

0 commit comments

Comments
 (0)