Skip to content

Commit 94157f7

Browse files
committed
Add support for Mongoid 5
1 parent 16fd3b5 commit 94157f7

13 files changed

+140
-55
lines changed

.travis.yml

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ rvm:
99

1010
gemfile:
1111
- Gemfile
12+
- gemfiles/mongoid3.gemfile
13+
- gemfiles/mongoid4.gemfile
1214

1315
env:
1416
global:

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## Mongoid::Includes 1.0.3 (2015-10-10) ##
2+
3+
* Add support for Mongoid 5.
4+
15
## Mongoid::Includes 1.0.2 (2015-10-08) ##
26

37
* Fix error when using `merge` or `merge!` with a criteria and `includes`.

Gemfile

+2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
source 'https://rubygems.org'
22

33
gemspec
4+
45
gem 'bundler'
56
gem 'rake'
67
gem 'pry'
8+
gem 'mongoid', '~> 5.0.0'
79

810
group :test do
911
gem 'rspec-given', '~> 3.5'

Gemfile.lock

+12-15
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,41 @@
11
PATH
22
remote: .
33
specs:
4-
mongoid_includes (1.0.2)
5-
mongoid (>= 3.1.0, < 5.0.0)
4+
mongoid_includes (1.0.3)
5+
mongoid (>= 3.1.0, < 6.0.0)
66

77
GEM
88
remote: https://rubygems.org/
99
specs:
10-
activemodel (4.2.3)
11-
activesupport (= 4.2.3)
10+
activemodel (4.2.4)
11+
activesupport (= 4.2.4)
1212
builder (~> 3.1)
13-
activesupport (4.2.3)
13+
activesupport (4.2.4)
1414
i18n (~> 0.7)
1515
json (~> 1.7, >= 1.7.7)
1616
minitest (~> 5.1)
1717
thread_safe (~> 0.3, >= 0.3.4)
1818
tzinfo (~> 1.1)
19-
bson (3.2.1)
19+
bson (3.2.6)
2020
builder (3.2.2)
2121
codeclimate-test-reporter (0.4.7)
2222
simplecov (>= 0.7.1, < 1.0.0)
2323
coderay (1.1.0)
24-
connection_pool (2.2.0)
2524
diff-lcs (1.2.5)
2625
docile (1.1.5)
2726
given_core (3.7.1)
2827
sorcerer (>= 0.3.7)
2928
i18n (0.7.0)
3029
json (1.8.3)
3130
method_source (0.8.2)
32-
minitest (5.8.0)
33-
mongoid (4.0.2)
31+
minitest (5.8.1)
32+
mongo (2.1.1)
33+
bson (~> 3.0)
34+
mongoid (5.0.0)
3435
activemodel (~> 4.0)
35-
moped (~> 2.0.0)
36+
mongo (~> 2.1)
3637
origin (~> 2.1)
3738
tzinfo (>= 0.3.37)
38-
moped (2.0.6)
39-
bson (~> 3.0)
40-
connection_pool (~> 2.0)
41-
optionable (~> 0.2.0)
42-
optionable (0.2.0)
4339
origin (2.1.1)
4440
pry (0.10.1)
4541
coderay (~> 1.1.0)
@@ -84,6 +80,7 @@ PLATFORMS
8480
DEPENDENCIES
8581
bundler
8682
codeclimate-test-reporter
83+
mongoid (~> 5.0.0)
8784
mongoid_includes!
8885
pry
8986
rake

gemfiles/mongoid3.gemfile

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
source 'https://rubygems.org'
2+
3+
gemspec '..'
4+
5+
gem 'bundler'
6+
gem 'rake'
7+
gem 'pry'
8+
gem 'mongoid', '~> 3.1.0'
9+
10+
group :test do
11+
gem 'rspec-given', '~> 3.5'
12+
gem 'codeclimate-test-reporter', require: nil
13+
end
14+
15+
group :doc do
16+
gem 'yard'
17+
gem 'yard-tomdoc'
18+
end

gemfiles/mongoid4.gemfile

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
source 'https://rubygems.org'
2+
3+
gemspec '..'
4+
5+
gem 'bundler'
6+
gem 'rake'
7+
gem 'pry'
8+
gem 'mongoid', '~> 4.0.2'
9+
10+
group :test do
11+
gem 'rspec-given', '~> 3.5'
12+
gem 'codeclimate-test-reporter', require: nil
13+
end
14+
15+
group :doc do
16+
gem 'yard'
17+
gem 'yard-tomdoc'
18+
end

lib/mongoid/includes/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ module Mongoid
55
module Includes
66

77
# Public: This library will attempt to follow semantic versioning (whatever that's supposed to be).
8-
VERSION = '1.0.2'
8+
VERSION = '1.0.3'
99
end
1010
end

mongoid_includes.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ Gem::Specification.new do |s|
2222
s.files = Dir.glob('lib/**/*') + %w(CHANGELOG.md LICENSE.txt README.md Rakefile)
2323
s.test_files = Dir.glob('spec/**/*')
2424

25-
s.add_runtime_dependency 'mongoid', ['>= 3.1.0', '< 5.0.0']
25+
s.add_runtime_dependency 'mongoid', ['>= 3.1.0', '< 6.0.0']
2626
end
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
require 'spec_helper'
2+
3+
describe Mongoid::Criteria do
4+
Given(:criteria) { Band.includes(:songs, :owner, from: :albums) }
5+
Given(:other_criteria) { Band.includes(:musicians, :albums) }
6+
7+
describe '#merge' do
8+
context 'inclusions are combined properly' do
9+
When(:new_criteria) { criteria.merge(other_criteria) }
10+
Then { new_criteria.inclusions.size == 4 }
11+
And { new_criteria.inclusions.class == Mongoid::Includes::Inclusions }
12+
And { criteria.inclusions.size == 3 }
13+
And { other_criteria.inclusions.size == 2 }
14+
end
15+
end
16+
17+
describe '#merge!' do
18+
context 'inclusions are merged properly' do
19+
When { criteria.merge!(other_criteria) }
20+
Then { criteria.inclusions.size == 4 }
21+
And { criteria.inclusions.class == Mongoid::Includes::Inclusions }
22+
And { other_criteria.inclusions.size == 2 }
23+
end
24+
end
25+
end

spec/mongoid/includes/simple_inclusions_spec.rb

+1-8
Original file line numberDiff line numberDiff line change
@@ -285,13 +285,6 @@ class D
285285
end
286286
end
287287

288-
it 'does not eager load the last document' do
289-
doc = criteria.last
290-
expect_query(1) do
291-
expect(doc.person).to eq(person_two)
292-
end
293-
end
294-
295288
it 'returns the first document' do
296289
expect(document).to eq(post_one)
297290
end
@@ -300,7 +293,7 @@ class D
300293
context 'when calling last' do
301294

302295
let!(:criteria) do
303-
Post.includes(:person)
296+
Post.asc(:_id).includes(:person)
304297
end
305298

306299
let!(:document) do

spec/spec_helper.rb

+9
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ def mongodb_version
2727
# Set the database that the spec suite connects to.
2828
Mongoid.configure do |config|
2929
config.load_configuration(
30+
clients: {
31+
default: {
32+
database: DATABASE,
33+
hosts: [ "#{HOST}:#{PORT.to_i}" ],
34+
options: {
35+
max_pool_size: 1,
36+
}
37+
}
38+
},
3039
sessions: {
3140
default: {
3241
database: DATABASE,

spec/support/config/mongoid.yml

+10-22
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,22 @@
11
test:
2-
sessions:
2+
clients:
33
default:
44
database: mongoid_test
55
hosts:
66
- <%=ENV["MONGOID_SPEC_HOST"]%>:<%=ENV["MONGOID_SPEC_PORT"]%>
77
options:
8-
read: primary
9-
mongohq_single:
10-
database: <%=ENV["MONGOHQ_SINGLE_NAME"]%>
11-
username: <%=ENV["MONGOHQ_SINGLE_USER"]%>
12-
password: <%=ENV["MONGOHQ_SINGLE_PASS"]%>
13-
hosts:
14-
- <%=ENV["MONGOHQ_SINGLE_URL"]%>
15-
options:
16-
write:
17-
w: 1
18-
read: primary
19-
mongohq_repl:
20-
database: <%=ENV["MONGOHQ_REPL_NAME"]%>
21-
username: <%=ENV["MONGOHQ_REPL_USER"]%>
22-
password: <%=ENV["MONGOHQ_REPL_PASS"]%>
8+
read:
9+
mode: :primary
10+
max_pool_size: 1
11+
12+
sessions:
13+
default:
14+
database: mongoid_test
2315
hosts:
24-
- <%=ENV["MONGOHQ_REPL_1_URL"]%>
25-
- <%=ENV["MONGOHQ_REPL_2_URL"]%>
16+
- <%=ENV["MONGOID_SPEC_HOST"]%>:<%=ENV["MONGOID_SPEC_PORT"]%>
2617
options:
2718
read: primary
28-
write:
29-
w: majority
30-
mongohq_repl_uri:
31-
uri: <%= ENV["MONGOHQ_REPL_URI"]%>
19+
3220
options:
3321
include_root_in_json: false
3422
include_type_for_serialization: false

spec/support/helpers.rb

+37-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,43 @@
1-
require 'mongoid/support/query_counter'
1+
module Mongoid
2+
class QueryCounter
3+
attr_reader :events
4+
5+
def initialize
6+
@events = []
7+
end
8+
9+
def instrument
10+
subscriber = ActiveSupport::Notifications.subscribe('query.moped') do |*args|
11+
@events << ActiveSupport::Notifications::Event.new(*args)
12+
end
13+
yield
14+
ensure
15+
ActiveSupport::Notifications.unsubscribe(subscriber)
16+
end
17+
18+
def inspect
19+
@events.map { |e| e.payload[:ops].map(&:log_inspect) }.join("\n")
20+
end
21+
end
22+
end
223

324
module Mongoid
425
module SpecHelpers
5-
def expect_query(number, &block)
6-
query_counter = Mongoid::QueryCounter.new
7-
query_counter.instrument(&block)
8-
expect(query_counter.events.size).to(eq(number), %[
9-
Expected to receive #{number} queries, it received #{query_counter.events.size}
10-
#{query_counter.inspect}
11-
])
26+
if defined?(Mongo::Logger.logger)
27+
def expect_query(number)
28+
# There are both start and complete events for each query.
29+
expect(Mongo::Logger.logger).to receive(:debug?).exactly(number * 4).times.and_call_original
30+
yield
31+
end
32+
else
33+
def expect_query(number, &block)
34+
query_counter = Mongoid::QueryCounter.new
35+
query_counter.instrument(&block)
36+
expect(query_counter.events.size).to(eq(number), %[
37+
Expected to receive #{number} queries, it received #{query_counter.events.size}
38+
#{query_counter.inspect}
39+
])
40+
end
1241
end
1342

1443
def expect_no_queries(&block)

0 commit comments

Comments
 (0)