Permalink
Browse files

Update to Mongoid 5

  • Loading branch information...
TomK32 committed Mar 22, 2017
1 parent 4974dcf commit 737d8bfc68fd4bf000aac0488bc88a9260cd6f60
Showing with 29 additions and 42 deletions.
  1. +2 −7 Gemfile
  2. +9 −17 lib/mongoid/taggable.rb
  3. +6 −6 mongoid_taggable.gemspec
  4. +2 −2 spec/mongoid.yml
  5. +10 −10 spec/mongoid/taggable_spec.rb
@@ -1,15 +1,10 @@
source 'https://rubygems.org'

gem 'rake'
gem 'mongoid'
gem 'mongoid', '~> 5'

group :test do
gem 'rspec', '>= 2'
gem 'rspec', '~> 2'
gem 'rdoc'
gem 'database_cleaner'
end


group :development do
gem 'debugger'
end
@@ -95,7 +95,7 @@ def tags_index_collection_name
end

def tags_index_collection
@tags_index_collection ||= Moped::Collection.new(self.collection.database, tags_index_collection_name)
@tags_index_collection ||= Mongo::Collection.new(self.collection.database, tags_index_collection_name)
end

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

tags_index_pipeline = [
self.unscoped.collection.aggregate([
{"$unwind" => "$tags_array"},
{"$group" => {_id: "$tags_array", matches: {"$sum" => 1} } },
{"$group" => {_id: "$tags_array", matches: {"$sum" => 1}} },
{"$project" => {
tags_array: 1,
matches: 1,
uniqueness: {"$subtract" => [1, {"$divide" => ["$matches", total_docs]} ] } } },
{"$sort" => {_id: 1}}
]

# It would be good to use the "$out" pipeline step, to save this aggregation
# to a collection (instead of array), but this is only available in unreleased Mongo 2.6

results = self.unscoped.collection.aggregate(*tags_index_pipeline)


results.each do |r|
tags_index_collection.find(_id: r["_id"]).upsert(r)
end
uniqueness: {"$subtract" => [1, {"$divide" => ["$matches", total_docs]} ] }
} },
{"$out" => tags_index_collection_name}
]).to_a

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

related = self.collection.aggregate(*related_pipeline)
related = self.collection.aggregate(related_pipeline)

ordering = Hash.new(0)

@@ -4,13 +4,13 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)

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

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

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

g.add_runtime_dependency 'rake', '>= 0'
g.add_runtime_dependency 'mongoid', '>= 3'
g.add_runtime_dependency 'mongoid', '~> 5'
end
@@ -1,6 +1,6 @@
test:
sessions:
clients:
default:
database: mongoid_taggable_test
hosts:
- localhost:27017
- localhost:27017
@@ -33,43 +33,43 @@ class MyModel
context "by tagged_with" do
let(:models){MyModel.tagged_with('interesting')}
it "locates tagged objects" do
models.include?(model).should be_true
models.include?(model).should be_truthy
end
end
context "by tagged_with_all using an array" do
let(:models){MyModel.tagged_with_all(['interesting', 'good'])}
it "locates tagged objects" do
models.include?(model).should be_true
models.include?(model).should be_truthy
end
end
context "by tagged_with_all using strings" do
let(:models){MyModel.tagged_with_all('interesting', 'good')}
it "locates tagged objects" do
models.include?(model).should be_true
models.include?(model).should be_truthy
end
end
context "by tagged_with_all when tag not included" do
let(:models){MyModel.tagged_with_all('interesting', 'good', 'mcdonalds')}
it "locates tagged objects" do
models.include?(model).should be_false
models.include?(model).should be_falsey
end
end
context "by tagged_with_any using an array" do
let(:models){MyModel.tagged_with_any(['interesting', 'mcdonalds'])}
it "locates tagged objects" do
models.include?(model).should be_true
models.include?(model).should be_truthy
end
end
context "by tagged_with_any using strings" do
let(:models){MyModel.tagged_with_any('interesting', 'mcdonalds')}
it "locates tagged objects" do
models.include?(model).should be_true
models.include?(model).should be_truthy
end
end
context "by tagged_with_any when tag not included" do
let(:models){MyModel.tagged_with_any('hardees', 'wendys', 'mcdonalds')}
it "locates tagged objects" do
models.include?(model).should be_false
models.include?(model).should be_falsey
end
end
end
@@ -142,7 +142,7 @@ class MyModel
end

it "should generate the index collection model based on model" do
MyModel.tags_index_collection.should be_a Moped::Collection
MyModel.tags_index_collection.should be_a Mongo::Collection
end

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

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

0 comments on commit 737d8bf

Please sign in to comment.