|
1461 | 1461 | end
|
1462 | 1462 | end
|
1463 | 1463 |
|
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) } |
1465 | 1469 |
|
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 |
1469 | 1474 |
|
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 |
1473 | 1478 |
|
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 }}) |
1477 | 1481 | end
|
1478 |
| - end |
1479 | 1482 |
|
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 |
1483 | 1486 |
|
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 |
1487 | 1490 |
|
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 |
1490 | 1494 | end
|
1491 | 1495 |
|
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 |
1495 | 1502 |
|
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 |
1499 | 1506 |
|
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 |
1503 | 1510 |
|
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 |
1507 | 1518 |
|
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 |
1510 | 1526 | end
|
1511 | 1527 | end
|
1512 | 1528 |
|
@@ -2308,17 +2324,6 @@ def self.ages; self; end
|
2308 | 2324 | end
|
2309 | 2325 | end
|
2310 | 2326 |
|
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 |
| - |
2322 | 2327 | describe "#to_proc" do
|
2323 | 2328 |
|
2324 | 2329 | let(:criteria) do
|
@@ -3031,11 +3036,11 @@ def self.ages; self; end
|
3031 | 3036 | context "when the method exists on the criteria" do
|
3032 | 3037 |
|
3033 | 3038 | before do
|
3034 |
| - expect(criteria).to receive(:to_criteria).and_call_original |
| 3039 | + expect(criteria).to receive(:only).and_call_original |
3035 | 3040 | end
|
3036 | 3041 |
|
3037 | 3042 | it "calls the method on the criteria" do
|
3038 |
| - expect(criteria.to_criteria).to eq(criteria) |
| 3043 | + expect(criteria.only).to eq(criteria) |
3039 | 3044 | end
|
3040 | 3045 | end
|
3041 | 3046 |
|
@@ -3242,4 +3247,44 @@ def self.ages; self; end
|
3242 | 3247 | end
|
3243 | 3248 | end
|
3244 | 3249 | 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 |
3245 | 3290 | end
|
0 commit comments