Skip to content

Commit 009be25

Browse files
authored
MONGOID-5267 remove driver argument in queryable constructor (#5190)
* MONGOID-5267 remove driver argument in queryable constructor * MONGOID-5267 remove mongo-1.x tests * MONGOID-5267 fix marshalable tests * MONGOID-5267 raise on mongo1x * MONGOID-5267 add comment and remove instance var
1 parent e39ae6f commit 009be25

File tree

5 files changed

+36
-500
lines changed

5 files changed

+36
-500
lines changed

lib/mongoid/criteria/marshalable.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ module Marshalable
99
# @example Dump the criteria.
1010
# Marshal.dump(criteria)
1111
#
12+
# Note :mongo is written here for backwards compatibility with Mongoid 7
13+
# and earlier.
14+
#
1215
# @return [ Array<Object> ] The dumped data.
1316
def marshal_dump
14-
data = [ klass, driver, inclusions, documents, strategy, negating ]
17+
data = [ klass, :mongo, inclusions, documents, strategy, negating ]
1518
data.push(scoping_options).push(dump_hash(:selector)).push(dump_hash(:options))
1619
end
1720

@@ -23,7 +26,12 @@ def marshal_dump
2326
# @param [ Array ] data The raw data.
2427
def marshal_load(data)
2528
@scoping_options, raw_selector, raw_options = data.pop(3)
26-
@klass, @driver, @inclusions, @documents, @strategy, @negating = data
29+
@klass, driver, @inclusions, @documents, @strategy, @negating = data
30+
31+
if driver == :mongo1x
32+
raise NotImplementedError, "Mongoid no longer supports marshalling with driver version 1.x."
33+
end
34+
2735
@selector = load_hash(Queryable::Selector, raw_selector)
2836
@options = load_hash(Queryable::Options, raw_options)
2937
end

lib/mongoid/criteria/queryable.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ module Queryable
3333
include Optional
3434

3535
# @attribute [r] aliases The aliases.
36-
# @attribute [r] driver The Mongo driver being used.
36+
attr_reader :aliases
37+
3738
# @attribute [r] serializers The serializers.
38-
attr_reader :aliases, :driver, :serializers
39+
attr_reader :serializers
3940

4041
# Is this queryable equal to another object? Is true if the selector and
4142
# options are equal.
@@ -64,8 +65,8 @@ def ==(other)
6465
# @param [ Symbol ] driver The driver being used.
6566
#
6667
# @api private
67-
def initialize(aliases = {}, serializers = {}, associations = {}, aliased_associations = {}, driver = :mongo)
68-
@aliases, @driver, @serializers = aliases, driver.to_sym, serializers
68+
def initialize(aliases = {}, serializers = {}, associations = {}, aliased_associations = {})
69+
@aliases, @serializers = aliases, serializers
6970
@options = Options.new(aliases, serializers, associations, aliased_associations)
7071
@selector = Selector.new(aliases, serializers, associations, aliased_associations)
7172
@pipeline = Pipeline.new(aliases)

lib/mongoid/criteria/queryable/optional.rb

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -318,15 +318,9 @@ def collation(collation_doc)
318318
#
319319
# @return [ Optional ] The cloned optional.
320320
def add_sort_option(options, field, direction)
321-
if driver == :mongo1x
322-
sorting = (options[:sort] || []).dup
323-
sorting.push([ field, direction ])
324-
options.store(:sort, sorting)
325-
else
326-
sorting = (options[:sort] || {}).dup
327-
sorting[field] = direction
328-
options.store(:sort, sorting)
329-
end
321+
sorting = (options[:sort] || {}).dup
322+
sorting[field] = direction
323+
options.store(:sort, sorting)
330324
end
331325

332326
# Take the provided criterion and store it as an option in the query

spec/mongoid/criteria/marshalable_spec.rb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
require "spec_helper"
44

55
describe Mongoid::Criteria::Marshalable do
6-
76
describe "Marshal.dump" do
87

98
let(:criteria) do
@@ -26,5 +25,23 @@
2625
it "loads the proper attributes" do
2726
expect(Marshal.load(Marshal.dump(criteria))).to eq(criteria)
2827
end
28+
29+
context "when it receives driver mongo1x" do
30+
let(:dump) { Marshal.dump(criteria) }
31+
32+
before do
33+
expect_any_instance_of(Mongoid::Criteria).to receive(:marshal_dump).and_wrap_original do |m, *args|
34+
data = m.call(*args)
35+
data[1] = :mongo1x
36+
data
37+
end
38+
end
39+
40+
it "raises an error" do
41+
expect do
42+
Marshal.load(dump)
43+
end.to raise_error(NotImplementedError, /Mongoid no longer supports marshalling with driver version 1.x./)
44+
end
45+
end
2946
end
3047
end

0 commit comments

Comments
 (0)