File tree Expand file tree Collapse file tree 4 files changed +80
-5
lines changed Expand file tree Collapse file tree 4 files changed +80
-5
lines changed Original file line number Diff line number Diff line change @@ -305,7 +305,7 @@ Mongoid 7 behavior:
305
305
306
306
307
307
``#pluck`` on Embedded Criteria Returns ``nil`` Values
308
- ------------------------------------------------
308
+ ------------------------------------------------------
309
309
310
310
Mongoid 8 fixes a bug where calling ``#pluck`` on a Mongoid::Criteria
311
311
for embedded documents discarded nil values. This behavior was
@@ -573,3 +573,30 @@ Calling ``tally`` on the age field yields the following:
573
573
574
574
The ``tally`` method accepts the dot notation and field aliases. It also
575
575
allows for tallying localized fields.
576
+
577
+
578
+ ``find`` delegates to ``Enumerable#find`` when given a block
579
+ ------------------------------------------------------------
580
+
581
+ When given a block, without ``_id`` arguments, ``find`` delegates to
582
+ ``Enumerable#find``. Consider the following model:
583
+
584
+ .. code::
585
+
586
+ class Band
587
+ include Mongoid::Document
588
+ field :name, type: String
589
+ end
590
+
591
+ Band.create!(name: "Depeche Mode")
592
+ Band.create!(name: "The Rolling Stones")
593
+
594
+ Calling ``find`` with a block returns the first document for which the block
595
+ returns ``true``:
596
+
597
+ .. code::
598
+
599
+ Band.find do |b|
600
+ b.name == "Depeche Mode"
601
+ end
602
+ # => #<Band _id: 62c58e383282a4cbe82bd74b, name: "Depeche Mode">
Original file line number Diff line number Diff line change @@ -119,6 +119,13 @@ def exists?
119
119
# strings will be transparently converted to +BSON::ObjectId+ instances
120
120
# during query construction.
121
121
#
122
+ # If this method is given a block, it delegates to +Enumerable#find+ and
123
+ # returns the first document of those found by the current Crieria object
124
+ # for which the block returns a truthy value. If both a block and ids are
125
+ # given, the block is ignored and the documents for the given ids are
126
+ # returned. If a block and a Proc are given, the method delegates to
127
+ # +Enumerable#find+ and uses the proc as the default.
128
+ #
122
129
# The +find+ method takes into account the default scope defined on the
123
130
# model class, if any.
124
131
#
@@ -129,8 +136,13 @@ def exists?
129
136
#
130
137
# @raise Errors::DocumentNotFound If not all documents are found and
131
138
# the +raise_not_found_error+ Mongoid configuration option is truthy.
132
- def find ( *args )
133
- with_default_scope . find ( *args )
139
+ def find ( *args , &block )
140
+ empty_or_proc = args . empty? || ( args . length == 1 && args . first . is_a? ( Proc ) )
141
+ if block_given? && empty_or_proc
142
+ with_default_scope . find ( *args , &block )
143
+ else
144
+ with_default_scope . find ( *args )
145
+ end
134
146
end
135
147
136
148
# Find the first +Document+ given the conditions.
Original file line number Diff line number Diff line change 1006
1006
end
1007
1007
end
1008
1008
end
1009
+
1010
+ context "when passing in a block" do
1011
+
1012
+ let! ( :band1 ) { Band . create! ( name : '1' ) }
1013
+ let! ( :band2 ) { Band . create! ( name : '2' ) }
1014
+ let! ( :band3 ) { Band . create! ( name : '2' ) }
1015
+
1016
+ it "yields the documents to the block" do
1017
+ doc = Band . find { |b | b . name == '2' }
1018
+ expect ( doc ) . to eq ( band2 )
1019
+ end
1020
+ end
1021
+
1022
+ context "when passing in ids and a block" do
1023
+
1024
+ let! ( :band1 ) { Band . create! ( name : '1' ) }
1025
+ let! ( :band2 ) { Band . create! ( name : '2' ) }
1026
+ let! ( :band3 ) { Band . create! ( name : '2' ) }
1027
+
1028
+ it "acts like findable find" do
1029
+ docs = Band . find ( band1 . id , band2 . id ) { |b | b . name == '2' }
1030
+ expect ( docs ) . to eq ( [ band1 , band2 ] )
1031
+ end
1032
+ end
1033
+
1034
+ context "when passing in a Proc and a block" do
1035
+
1036
+ let! ( :band1 ) { Band . create! ( name : '1' ) }
1037
+ let! ( :band2 ) { Band . create! ( name : '2' ) }
1038
+ let! ( :band3 ) { Band . create! ( name : '2' ) }
1039
+
1040
+ it "acts like findable find" do
1041
+ docs = Band . find ( -> { 'default' } ) { |b | b . name == '3' }
1042
+ expect ( docs ) . to eq ( 'default' )
1043
+ end
1044
+ end
1009
1045
end
1010
1046
1011
1047
describe "#for_ids" do
Original file line number Diff line number Diff line change 837
837
end
838
838
end
839
839
840
- context "when given a Proc" do
841
- it "behaves as Enumerable " do
840
+ context "when given a Proc without a block " do
841
+ it "raises an error " do
842
842
lambda do
843
843
criteria . find ( -> { "default" } )
844
844
# Proc is not serializable to a BSON type
You can’t perform that action at this time.
0 commit comments