Skip to content

Commit 737d8bf

Browse files
committed
Update to Mongoid 5
1 parent 4974dcf commit 737d8bf

File tree

5 files changed

+29
-42
lines changed

5 files changed

+29
-42
lines changed

Gemfile

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
source 'https://rubygems.org'
22

33
gem 'rake'
4-
gem 'mongoid'
4+
gem 'mongoid', '~> 5'
55

66
group :test do
7-
gem 'rspec', '>= 2'
7+
gem 'rspec', '~> 2'
88
gem 'rdoc'
99
gem 'database_cleaner'
1010
end
11-
12-
13-
group :development do
14-
gem 'debugger'
15-
end

lib/mongoid/taggable.rb

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def tags_index_collection_name
9595
end
9696

9797
def tags_index_collection
98-
@tags_index_collection ||= Moped::Collection.new(self.collection.database, tags_index_collection_name)
98+
@tags_index_collection ||= Mongo::Collection.new(self.collection.database, tags_index_collection_name)
9999
end
100100

101101
def save_tags_index!
@@ -110,24 +110,16 @@ def index_tags_now!
110110
# http://docs.mongodb.org/manual/core/map-reduce/ suggests using the aggregation pipeline
111111
total_docs = self.count.to_f
112112

113-
tags_index_pipeline = [
113+
self.unscoped.collection.aggregate([
114114
{"$unwind" => "$tags_array"},
115-
{"$group" => {_id: "$tags_array", matches: {"$sum" => 1} } },
115+
{"$group" => {_id: "$tags_array", matches: {"$sum" => 1}} },
116116
{"$project" => {
117+
tags_array: 1,
117118
matches: 1,
118-
uniqueness: {"$subtract" => [1, {"$divide" => ["$matches", total_docs]} ] } } },
119-
{"$sort" => {_id: 1}}
120-
]
121-
122-
# It would be good to use the "$out" pipeline step, to save this aggregation
123-
# to a collection (instead of array), but this is only available in unreleased Mongo 2.6
124-
125-
results = self.unscoped.collection.aggregate(*tags_index_pipeline)
126-
127-
128-
results.each do |r|
129-
tags_index_collection.find(_id: r["_id"]).upsert(r)
130-
end
119+
uniqueness: {"$subtract" => [1, {"$divide" => ["$matches", total_docs]} ] }
120+
} },
121+
{"$out" => tags_index_collection_name}
122+
]).to_a
131123

132124
@need_to_index_tags = false
133125
end
@@ -152,7 +144,7 @@ def find_related(document_array, limit = 0, uniqueness_match=false, pipeline_inj
152144
related_pipeline.insert(0, *pipeline_injection)
153145
: related_pipeline.insert(0, pipeline_injection) )
154146

155-
related = self.collection.aggregate(*related_pipeline)
147+
related = self.collection.aggregate(related_pipeline)
156148

157149
ordering = Hash.new(0)
158150

mongoid_taggable.gemspec

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
44

55
Gem::Specification.new do |g|
66
g.name = 'mongoid_taggable'
7-
g.version = '1.1.4'
8-
g.date = '2014-03-10'
7+
g.version = '1.1.5'
8+
g.date = '2016-03-22'
99
g.description = %q{Mongoid Taggable provides some helpers to create taggable documents.}
1010
g.summary = %q{Mongoid taggable behaviour}
11-
g.authors = ['Wilker Lucio', 'Kris Kowalik', 'Adam St. John', 'Caleb Clark']
12-
g.email = ['cclark@mobilizationlabs.com']
13-
g.homepage = 'http://github.com/wilkerlucio/mongoid_taggable'
11+
g.authors = ['Wilker Lucio', 'Kris Kowalik', 'Adam St. John', 'Caleb Clark', 'Thomas R. Koll']
12+
g.email = ['tomk32@tomk32.de']
13+
g.homepage = 'http://github.com/tomk32/mongoid_taggable'
1414

1515
g.extra_rdoc_files = %w(LICENSE README.textile)
1616

@@ -19,5 +19,5 @@ Gem::Specification.new do |g|
1919
g.require_paths = ['lib']
2020

2121
g.add_runtime_dependency 'rake', '>= 0'
22-
g.add_runtime_dependency 'mongoid', '>= 3'
22+
g.add_runtime_dependency 'mongoid', '~> 5'
2323
end

spec/mongoid.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
test:
2-
sessions:
2+
clients:
33
default:
44
database: mongoid_taggable_test
55
hosts:
6-
- localhost:27017
6+
- localhost:27017

spec/mongoid/taggable_spec.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,43 +33,43 @@ class MyModel
3333
context "by tagged_with" do
3434
let(:models){MyModel.tagged_with('interesting')}
3535
it "locates tagged objects" do
36-
models.include?(model).should be_true
36+
models.include?(model).should be_truthy
3737
end
3838
end
3939
context "by tagged_with_all using an array" do
4040
let(:models){MyModel.tagged_with_all(['interesting', 'good'])}
4141
it "locates tagged objects" do
42-
models.include?(model).should be_true
42+
models.include?(model).should be_truthy
4343
end
4444
end
4545
context "by tagged_with_all using strings" do
4646
let(:models){MyModel.tagged_with_all('interesting', 'good')}
4747
it "locates tagged objects" do
48-
models.include?(model).should be_true
48+
models.include?(model).should be_truthy
4949
end
5050
end
5151
context "by tagged_with_all when tag not included" do
5252
let(:models){MyModel.tagged_with_all('interesting', 'good', 'mcdonalds')}
5353
it "locates tagged objects" do
54-
models.include?(model).should be_false
54+
models.include?(model).should be_falsey
5555
end
5656
end
5757
context "by tagged_with_any using an array" do
5858
let(:models){MyModel.tagged_with_any(['interesting', 'mcdonalds'])}
5959
it "locates tagged objects" do
60-
models.include?(model).should be_true
60+
models.include?(model).should be_truthy
6161
end
6262
end
6363
context "by tagged_with_any using strings" do
6464
let(:models){MyModel.tagged_with_any('interesting', 'mcdonalds')}
6565
it "locates tagged objects" do
66-
models.include?(model).should be_true
66+
models.include?(model).should be_truthy
6767
end
6868
end
6969
context "by tagged_with_any when tag not included" do
7070
let(:models){MyModel.tagged_with_any('hardees', 'wendys', 'mcdonalds')}
7171
it "locates tagged objects" do
72-
models.include?(model).should be_false
72+
models.include?(model).should be_falsey
7373
end
7474
end
7575
end
@@ -142,7 +142,7 @@ class MyModel
142142
end
143143

144144
it "should generate the index collection model based on model" do
145-
MyModel.tags_index_collection.should be_a Moped::Collection
145+
MyModel.tags_index_collection.should be_a Mongo::Collection
146146
end
147147

148148
context "retrieving index" do
@@ -241,8 +241,8 @@ class MyModel
241241
related = MyModel.find_related([@george, @ringo])
242242
related.should have_at_least(1).items
243243
related[0].should == @someone_else # 5 matches
244-
related.include?(@george).should be_false
245-
related.include?(@ringo).should be_false
244+
related.include?(@george).should be_falsey
245+
related.include?(@ringo).should be_falsey
246246
end
247247

248248
it 'for multiple items as input, it should order based on tag matches' do

0 commit comments

Comments
 (0)