Skip to content

Commit 0aaf8d5

Browse files
johnnyshieldsjamis
andauthored
MONGOID-5677 [Monkey Patch Removal] Remove Hash#to_criteria and Criteria#to_criteria. Add Criteria.from_hash (#5717)
* Remove Hash#to_criteria and Criteria#to_criteria. Add Criteria.from_hash * deprecate rathr than remove also, some code-style standardization --------- Co-authored-by: Jamis Buck <[email protected]>
1 parent f4b29ce commit 0aaf8d5

File tree

3 files changed

+123
-59
lines changed

3 files changed

+123
-59
lines changed

lib/mongoid/criteria.rb

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,25 @@ class Criteria
4141
include Clients::Sessions
4242
include Options
4343

44-
Mongoid.deprecate(self, :for_js)
44+
class << self
45+
# Convert the given hash to a criteria. Will iterate over each keys in the
46+
# hash which must correspond to method on a criteria object. The hash
47+
# must also include a "klass" key.
48+
#
49+
# @example Convert the hash to a criteria.
50+
# Criteria.from_hash({ klass: Band, where: { name: "Depeche Mode" })
51+
#
52+
# @param [ Hash ] hash The hash to convert.
53+
#
54+
# @return [ Criteria ] The criteria.
55+
def from_hash(hash)
56+
criteria = Criteria.new(hash.delete(:klass) || hash.delete('klass'))
57+
hash.each_pair do |method, args|
58+
criteria = criteria.__send__(method, args)
59+
end
60+
criteria
61+
end
62+
end
4563

4664
# Static array used to check with method missing - we only need to ever
4765
# instantiate once.
@@ -250,16 +268,16 @@ def merge(other)
250268
# @example Merge another criteria into this criteria.
251269
# criteria.merge(Person.where(name: "bob"))
252270
#
253-
# @param [ Criteria ] other The criteria to merge in.
271+
# @param [ Criteria | Hash ] other The criteria to merge in.
254272
#
255273
# @return [ Criteria ] The merged criteria.
256274
def merge!(other)
257-
criteria = other.to_criteria
258-
selector.merge!(criteria.selector)
259-
options.merge!(criteria.options)
260-
self.documents = criteria.documents.dup unless criteria.documents.empty?
261-
self.scoping_options = criteria.scoping_options
262-
self.inclusions = (inclusions + criteria.inclusions).uniq
275+
other = self.class.from_hash(other) if other.is_a?(Hash)
276+
selector.merge!(other.selector)
277+
options.merge!(other.options)
278+
self.documents = other.documents.dup unless other.documents.empty?
279+
self.scoping_options = other.scoping_options
280+
self.inclusions = (inclusions + other.inclusions).uniq
263281
self
264282
end
265283

@@ -352,9 +370,11 @@ def respond_to?(name, include_private = false)
352370
# criteria.to_criteria
353371
#
354372
# @return [ Criteria ] self.
373+
# @deprecated
355374
def to_criteria
356375
self
357376
end
377+
Mongoid.deprecate(self, :to_criteria)
358378

359379
# Convert the criteria to a proc.
360380
#
@@ -452,6 +472,7 @@ def for_js(javascript, scope = {})
452472
end
453473
js_query(code)
454474
end
475+
Mongoid.deprecate(self, :for_js)
455476

456477
private
457478

lib/mongoid/extensions/hash.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,11 @@ def resizable?
101101
# { klass: Band, where: { name: "Depeche Mode" }.to_criteria
102102
#
103103
# @return [ Criteria ] The criteria.
104+
# @deprecated
104105
def to_criteria
105-
criteria = Criteria.new(delete(:klass) || delete("klass"))
106-
each_pair do |method, args|
107-
criteria = criteria.__send__(method, args)
108-
end
109-
criteria
106+
Criteria.from_hash(self)
110107
end
108+
Mongoid.deprecate(self, :to_criteria)
111109

112110
private
113111

spec/mongoid/criteria_spec.rb

Lines changed: 91 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,52 +1461,68 @@
14611461
end
14621462
end
14631463

1464-
describe "#merge!" do
1464+
describe '#merge!' do
1465+
let(:band) { Band.new }
1466+
let(:criteria) { Band.scoped.where(name: 'Depeche Mode').asc(:name) }
1467+
let(:association) { Band.relations['records'] }
1468+
subject(:merged) { criteria.merge!(other) }
14651469

1466-
let(:band) do
1467-
Band.new
1468-
end
1470+
context 'when merging a Criteria' do
1471+
let(:other) do
1472+
{ klass: Band, includes: [:records] }
1473+
end
14691474

1470-
let(:criteria) do
1471-
Band.scoped.where(name: "Depeche Mode").asc(:name)
1472-
end
1475+
it 'merges the selector' do
1476+
expect(merged.selector).to eq({ 'name' => 'Depeche Mode' })
1477+
end
14731478

1474-
let(:mergeable) do
1475-
Band.includes(:records).tap do |crit|
1476-
crit.documents = [ band ]
1479+
it 'merges the options' do
1480+
expect(merged.options).to eq({ sort: { 'name' => 1 }})
14771481
end
1478-
end
14791482

1480-
let(:association) do
1481-
Band.relations["records"]
1482-
end
1483+
it 'merges the scoping options' do
1484+
expect(merged.scoping_options).to eq([ nil, nil ])
1485+
end
14831486

1484-
let(:merged) do
1485-
criteria.merge!(mergeable)
1486-
end
1487+
it 'merges the inclusions' do
1488+
expect(merged.inclusions).to eq([ association ])
1489+
end
14871490

1488-
it "merges the selector" do
1489-
expect(merged.selector).to eq({ "name" => "Depeche Mode" })
1491+
it 'returns the same criteria' do
1492+
expect(merged).to equal(criteria)
1493+
end
14901494
end
14911495

1492-
it "merges the options" do
1493-
expect(merged.options).to eq({ sort: { "name" => 1 }})
1494-
end
1496+
context 'when merging a Hash' do
1497+
let(:other) do
1498+
Band.includes(:records).tap do |crit|
1499+
crit.documents = [ band ]
1500+
end
1501+
end
14951502

1496-
it "merges the documents" do
1497-
expect(merged.documents).to eq([ band ])
1498-
end
1503+
it 'merges the selector' do
1504+
expect(merged.selector).to eq({ 'name' => 'Depeche Mode' })
1505+
end
14991506

1500-
it "merges the scoping options" do
1501-
expect(merged.scoping_options).to eq([ nil, nil ])
1502-
end
1507+
it 'merges the options' do
1508+
expect(merged.options).to eq({ sort: { 'name' => 1 }})
1509+
end
15031510

1504-
it "merges the inclusions" do
1505-
expect(merged.inclusions).to eq([ association ])
1506-
end
1511+
it 'merges the documents' do
1512+
expect(merged.documents).to eq([ band ])
1513+
end
1514+
1515+
it 'merges the scoping options' do
1516+
expect(merged.scoping_options).to eq([ nil, nil ])
1517+
end
15071518

1508-
it "returns the same criteria" do
1509-
expect(merged).to equal(criteria)
1519+
it 'merges the inclusions' do
1520+
expect(merged.inclusions).to eq([ association ])
1521+
end
1522+
1523+
it 'returns the same criteria' do
1524+
expect(merged).to equal(criteria)
1525+
end
15101526
end
15111527
end
15121528

@@ -2308,17 +2324,6 @@ def self.ages; self; end
23082324
end
23092325
end
23102326

2311-
describe "#to_criteria" do
2312-
2313-
let(:criteria) do
2314-
Band.all
2315-
end
2316-
2317-
it "returns self" do
2318-
expect(criteria.to_criteria).to eq(criteria)
2319-
end
2320-
end
2321-
23222327
describe "#to_proc" do
23232328

23242329
let(:criteria) do
@@ -3031,11 +3036,11 @@ def self.ages; self; end
30313036
context "when the method exists on the criteria" do
30323037

30333038
before do
3034-
expect(criteria).to receive(:to_criteria).and_call_original
3039+
expect(criteria).to receive(:only).and_call_original
30353040
end
30363041

30373042
it "calls the method on the criteria" do
3038-
expect(criteria.to_criteria).to eq(criteria)
3043+
expect(criteria.only).to eq(criteria)
30393044
end
30403045
end
30413046

@@ -3242,4 +3247,44 @@ def self.ages; self; end
32423247
end
32433248
end
32443249
end
3250+
3251+
describe '.from_hash' do
3252+
subject(:criteria) { described_class.from_hash(hash) }
3253+
3254+
context 'when klass is specified' do
3255+
let(:hash) do
3256+
{ klass: Band, where: { name: 'Songs Ohia' } }
3257+
end
3258+
3259+
it 'returns a criteria' do
3260+
expect(criteria).to be_a(Mongoid::Criteria)
3261+
end
3262+
3263+
it 'sets the klass' do
3264+
expect(criteria.klass).to eq(Band)
3265+
end
3266+
3267+
it 'sets the selector' do
3268+
expect(criteria.selector).to eq({ 'name' => 'Songs Ohia' })
3269+
end
3270+
end
3271+
3272+
context 'when klass is missing' do
3273+
let(:hash) do
3274+
{ where: { name: 'Songs Ohia' } }
3275+
end
3276+
3277+
it 'returns a criteria' do
3278+
expect(criteria).to be_a(Mongoid::Criteria)
3279+
end
3280+
3281+
it 'has klass nil' do
3282+
expect(criteria.klass).to be_nil
3283+
end
3284+
3285+
it 'sets the selector' do
3286+
expect(criteria.selector).to eq({ 'name' => 'Songs Ohia' })
3287+
end
3288+
end
3289+
end
32453290
end

0 commit comments

Comments
 (0)