mongoid-simple-tags 0.0.6 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +4 -47
- data/.travis.yml +4 -0
- data/Gemfile.lock +9 -8
- data/README.rdoc +7 -7
- data/VERSION +1 -1
- data/lib/mongoid-simple-tags.rb +28 -47
- data/mongoid-simple-tags.gemspec +3 -3
- data/mongoid.yml +6 -0
- data/spec/mongoid-simple-tags_spec.rb +96 -97
- data/spec/spec_helper.rb +6 -4
- metadata +6 -4
data/.gitignore
CHANGED
@@ -1,48 +1,5 @@
|
|
1
|
-
|
2
|
-
coverage
|
3
|
-
|
4
|
-
# rdoc generated
|
5
|
-
rdoc
|
6
|
-
|
7
|
-
# yard generated
|
8
|
-
doc
|
9
|
-
.yardoc
|
10
|
-
|
11
|
-
# bundler
|
1
|
+
*.gem
|
12
2
|
.bundle
|
13
|
-
|
14
|
-
|
15
|
-
pkg
|
16
|
-
|
17
|
-
# Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
|
18
|
-
#
|
19
|
-
# * Create a file at ~/.gitignore
|
20
|
-
# * Include files you want ignored
|
21
|
-
# * Run: git config --global core.excludesfile ~/.gitignore
|
22
|
-
#
|
23
|
-
# After doing this, these files will be ignored in all your git projects,
|
24
|
-
# saving you from having to 'pollute' every project you touch with them
|
25
|
-
#
|
26
|
-
# Not sure what to needs to be ignored for particular editors/OSes? Here's some ideas to get you started. (Remember, remove the leading # of the line)
|
27
|
-
#
|
28
|
-
# For MacOS:
|
29
|
-
#
|
30
|
-
#.DS_Store
|
31
|
-
|
32
|
-
# For TextMate
|
33
|
-
#*.tmproj
|
34
|
-
#tmtags
|
35
|
-
|
36
|
-
# For emacs:
|
37
|
-
#*~
|
38
|
-
#\#*
|
39
|
-
#.\#*
|
40
|
-
|
41
|
-
# For vim:
|
42
|
-
*.swp
|
43
|
-
|
44
|
-
# For redcar:
|
45
|
-
#.redcar
|
46
|
-
|
47
|
-
# For rubinius:
|
48
|
-
#*.rbc
|
3
|
+
.rvmrc
|
4
|
+
Gemfile.lock
|
5
|
+
pkg/*
|
data/.travis.yml
ADDED
data/Gemfile.lock
CHANGED
@@ -3,15 +3,15 @@ PATH
|
|
3
3
|
specs:
|
4
4
|
mongoid-simple-tags (0.0.6)
|
5
5
|
bson_ext (~> 1.6)
|
6
|
-
mongoid (~>
|
6
|
+
mongoid (~> 3.0.3)
|
7
7
|
|
8
8
|
GEM
|
9
9
|
remote: http://rubygems.org/
|
10
10
|
specs:
|
11
|
-
activemodel (3.2.
|
12
|
-
activesupport (= 3.2.
|
11
|
+
activemodel (3.2.7)
|
12
|
+
activesupport (= 3.2.7)
|
13
13
|
builder (~> 3.0.0)
|
14
|
-
activesupport (3.2.
|
14
|
+
activesupport (3.2.7)
|
15
15
|
i18n (~> 0.6)
|
16
16
|
multi_json (~> 1.0)
|
17
17
|
bson (1.6.4)
|
@@ -20,13 +20,14 @@ GEM
|
|
20
20
|
builder (3.0.0)
|
21
21
|
diff-lcs (1.1.3)
|
22
22
|
i18n (0.6.0)
|
23
|
-
|
24
|
-
bson (~> 1.6.2)
|
25
|
-
mongoid (2.4.11)
|
23
|
+
mongoid (3.0.3)
|
26
24
|
activemodel (~> 3.1)
|
27
|
-
|
25
|
+
moped (~> 1.1)
|
26
|
+
origin (~> 1.0)
|
28
27
|
tzinfo (~> 0.3.22)
|
28
|
+
moped (1.2.0)
|
29
29
|
multi_json (1.3.6)
|
30
|
+
origin (1.0.4)
|
30
31
|
rspec (2.10.0)
|
31
32
|
rspec-core (~> 2.10.0)
|
32
33
|
rspec-expectations (~> 2.10.0)
|
data/README.rdoc
CHANGED
@@ -1,11 +1,13 @@
|
|
1
|
-
= mongoid-simple-tags
|
1
|
+
= mongoid-simple-tags {<img src="https://secure.travis-ci.org/chebyte/mongoid-simple-tags.png"/>}[http://travis-ci.org/chebyte/mongoid-simple-tags]
|
2
2
|
|
3
3
|
mongoid-simple-tags is a basic and simple tagging system for mongoid using map-reduce function
|
4
|
+
(no backwards compatibility with mongoid v2!)
|
4
5
|
|
5
6
|
== Install
|
7
|
+
|
6
8
|
Add the following to Gemfile:
|
7
9
|
|
8
|
-
gem "mongoid-simple-tags", "0.0.
|
10
|
+
gem "mongoid-simple-tags", "0.0.7"
|
9
11
|
|
10
12
|
== Usage
|
11
13
|
|
@@ -18,10 +20,9 @@ mongoid-simple-tags is a basic and simple tagging system for mongoid using map-r
|
|
18
20
|
|
19
21
|
=== Console
|
20
22
|
|
21
|
-
u = User.
|
22
|
-
|
23
|
+
u = User.create(:name => "Tuquito", tag_list: "linux, tucuman, free software")
|
24
|
+
|
23
25
|
u.tags # => ["linux","tucuman","free software"]
|
24
|
-
u.save
|
25
26
|
|
26
27
|
User.tagged_with("linux") # => u
|
27
28
|
User.tagged_with(["tucuman", "free software"]) # => u
|
@@ -32,7 +33,7 @@ mongoid-simple-tags is a basic and simple tagging system for mongoid using map-r
|
|
32
33
|
|
33
34
|
User.tagged_with("linux") # => [u, u2]
|
34
35
|
|
35
|
-
#using map-reduce function
|
36
|
+
# using map-reduce function
|
36
37
|
|
37
38
|
User.all_tags #=>[{:name=>"free software", :count=>1}, {:name=>"linux", :count=>2}, {:name=>"tucuman", :count=>1}]
|
38
39
|
|
@@ -41,4 +42,3 @@ mongoid-simple-tags is a basic and simple tagging system for mongoid using map-r
|
|
41
42
|
|
42
43
|
Copyright (c) 2011 chebyte(mauro torres). See LICENSE.txt for
|
43
44
|
further details.
|
44
|
-
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.7
|
data/lib/mongoid-simple-tags.rb
CHANGED
@@ -4,9 +4,7 @@ module Mongoid
|
|
4
4
|
def self.included(base)
|
5
5
|
base.class_eval do |klass|
|
6
6
|
klass.field :tags, :type => Array
|
7
|
-
klass.index :
|
8
|
-
|
9
|
-
klass.send :after_save, :rebuild_tags
|
7
|
+
klass.index({ tags: 1 }, { background: true })
|
10
8
|
|
11
9
|
include InstanceMethods
|
12
10
|
extend ClassMethods
|
@@ -22,59 +20,42 @@ module Mongoid
|
|
22
20
|
def tag_list
|
23
21
|
self.tags.join(", ") if tags
|
24
22
|
end
|
25
|
-
|
26
|
-
protected
|
27
|
-
def rebuild_tags
|
28
|
-
self.collection.map_reduce(
|
29
|
-
"function() { if(this.tags) this.tags.forEach(function(t){ emit(t, 1); }); }",
|
30
|
-
"function(key,values) { var count = 0; values.forEach(function(v){ count += v; }); return count; }",
|
31
|
-
{ :out => 'tags' }
|
32
|
-
)
|
33
|
-
end
|
34
23
|
end
|
35
24
|
|
36
25
|
|
37
26
|
module ClassMethods
|
38
|
-
|
39
|
-
def all_tags(opts={})
|
40
|
-
tags = Mongoid.master.collection('tags')
|
41
|
-
opts.merge(:sort => ["_id", :desc]) unless opts[:sort]
|
42
|
-
tags.find({}, opts).to_a.map!{|item| { :name => item['_id'], :count => item['value'].to_i } }
|
43
|
-
end
|
44
27
|
|
45
|
-
def
|
46
|
-
map =
|
47
|
-
function() {
|
48
|
-
if(this.tags)
|
49
|
-
this.tags.forEach(
|
50
|
-
|
51
|
-
})
|
52
|
-
}
|
28
|
+
def all_tags(scope = {})
|
29
|
+
map = %Q{
|
30
|
+
function() {
|
31
|
+
if(this.tags){
|
32
|
+
this.tags.forEach(function(tag){
|
33
|
+
emit(tag, 1)
|
34
|
+
});
|
35
|
+
}
|
53
36
|
}
|
54
|
-
|
37
|
+
}
|
55
38
|
|
56
|
-
reduce =
|
57
|
-
function(key,values) {
|
58
|
-
var
|
59
|
-
values.forEach(function(
|
60
|
-
|
61
|
-
})
|
62
|
-
return
|
39
|
+
reduce = %Q{
|
40
|
+
function(key, values) {
|
41
|
+
var tag_count = 0 ;
|
42
|
+
values.forEach(function(value) {
|
43
|
+
tag_count += value;
|
44
|
+
});
|
45
|
+
return tag_count;
|
63
46
|
}
|
64
|
-
|
47
|
+
}
|
65
48
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
)
|
77
|
-
results.find().to_a.map{ |item| { :name => item['_id'], :count => item['value'].to_i } }
|
49
|
+
tags = self
|
50
|
+
tags = tags.where(scope) if scope.present?
|
51
|
+
|
52
|
+
results = tags.map_reduce(map, reduce).out(inline: true)
|
53
|
+
results.to_a.map!{ |item| { :name => item['_id'], :count => item['value'].to_i } }
|
54
|
+
end
|
55
|
+
|
56
|
+
def scoped_tags(scope = {})
|
57
|
+
warn "[DEPRECATION] `scoped_tags` is deprecated. Please use `all_tags` instead."
|
58
|
+
all_tags(scope)
|
78
59
|
end
|
79
60
|
|
80
61
|
def tagged_with(tags)
|
data/mongoid-simple-tags.gemspec
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = "mongoid-simple-tags"
|
5
|
-
s.version = "0.0.
|
5
|
+
s.version = "0.0.7"
|
6
6
|
s.platform = Gem::Platform::RUBY
|
7
7
|
s.authors = ["chebyte"]
|
8
8
|
s.email = ["maurotorres@gmail.com"]
|
@@ -17,6 +17,6 @@ Gem::Specification.new do |s|
|
|
17
17
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
18
|
s.require_paths = ["lib"]
|
19
19
|
s.add_development_dependency "rspec", "~> 2.10.0"
|
20
|
-
s.add_dependency "mongoid", "~>
|
21
|
-
s.add_dependency "bson_ext", "~> 1.6"
|
20
|
+
s.add_dependency "mongoid", "~> 3.0.3"
|
21
|
+
s.add_dependency "bson_ext", "~> 1.6"
|
22
22
|
end
|
data/mongoid.yml
ADDED
@@ -1,114 +1,113 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
class User
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
4
|
+
include Mongoid::Document
|
5
|
+
include Mongoid::Document::Taggable
|
6
|
+
field :name
|
7
|
+
belongs_to :organization
|
8
8
|
end
|
9
9
|
|
10
10
|
class Organization
|
11
|
-
|
12
|
-
|
13
|
-
|
11
|
+
include Mongoid::Document
|
12
|
+
field :name
|
13
|
+
has_many :users
|
14
14
|
end
|
15
15
|
|
16
|
+
|
16
17
|
describe "A Taggable model" do
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
18
|
+
|
19
|
+
let(:user) { User.new(name: 'Tuquito') }
|
20
|
+
|
21
|
+
it "should have a tag_list method" do
|
22
|
+
user.respond_to?(:tag_list)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should be able to update a tag list" do
|
26
|
+
tag_list = "linux, tucuman, free software"
|
27
|
+
tags = tag_list.split(',').map{ |tag| tag.strip}.flatten
|
28
|
+
|
29
|
+
user.tag_list = tag_list
|
30
|
+
user.tags.should == tags
|
31
|
+
end
|
31
32
|
end
|
32
33
|
|
34
|
+
|
33
35
|
describe "A Taggable model with tags assigned" do
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
end
|
64
|
-
|
65
|
-
after do
|
66
|
-
cleanup_database
|
67
|
-
end
|
36
|
+
|
37
|
+
before(:each) do
|
38
|
+
@user = User.create!(name: 'Tuquito', tag_list: "linux, tucuman, free software")
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should be able to find tagged_with objects" do
|
42
|
+
User.tagged_with('linux').first.should == @user
|
43
|
+
User.tagged_with(['tucuman', 'free software']).first.should == @user
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should be able to find tagged_with objects if more than one object is present" do
|
47
|
+
user_2 = User.create!(name: 'ubuntu', tag_list: 'linux')
|
48
|
+
|
49
|
+
tagged_with_list = User.tagged_with('linux')
|
50
|
+
tagged_with_list.include?(@user).should be_true
|
51
|
+
tagged_with_list.include?(user_2).should be_true
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should return all_tags per Model class" do
|
55
|
+
User.create(name: 'ubuntu', tag_list: 'linux')
|
56
|
+
|
57
|
+
expected_tag_list = [
|
58
|
+
{:name => "free software", :count => 1},
|
59
|
+
{:name => "linux", :count => 2},
|
60
|
+
{:name => "tucuman", :count => 1}
|
61
|
+
]
|
62
|
+
User.all_tags.should == expected_tag_list
|
63
|
+
end
|
64
|
+
|
68
65
|
end
|
69
66
|
|
70
67
|
describe "A Taggable model with scope" do
|
71
|
-
before do
|
72
|
-
@organization_1 = Organization.create(name: 'Qualica')
|
73
|
-
@user_1 = @organization_1.users.create(name: 'User1', tag_list: "ubuntu, linux, tucuman")
|
74
|
-
@user_2 = @organization_1.users.create(name: 'User2', tag_list: "ubuntu, linux, tucuman")
|
75
|
-
@organization_2 = Organization.create(name: 'Microsoft')
|
76
|
-
@user_3 = @organization_2.users.create(name: 'User3', tag_list: 'microsoft, windows, tucuman')
|
77
|
-
@user_4 = @organization_2.users.create(name: 'User4', tag_list: 'ubuntu, linux, tucuman')
|
78
|
-
end
|
79
|
-
|
80
|
-
it "should return scoped tags when passing one option" do
|
81
|
-
results = User.scoped_tags(organization_id: @organization_1.id)
|
82
|
-
results.empty?.should be_false
|
83
|
-
|
84
|
-
results.include?( {:name => "linux", :count => 2 } ).should be_true
|
85
|
-
results.include?( {:name => "ubuntu", :count => 2 } ).should be_true
|
86
|
-
results.include?( {:name => "tucuman", :count => 2 } ).should be_true
|
87
|
-
|
88
|
-
results = User.scoped_tags(organization_id: @organization_2.id)
|
89
|
-
results.empty?.should be_false
|
90
|
-
results.include?( {:name =>"linux", :count => 1 } ).should be_true
|
91
|
-
results.include?( {:name => "microsoft", :count => 1 } ).should be_true
|
92
|
-
results.include?( {:name => "windows", :count => 1 } ).should be_true
|
93
|
-
results.include?( {:name => "tucuman", :count => 2 } ).should be_true
|
94
|
-
end
|
95
|
-
|
96
|
-
it "should return scoped tags when passing more than one option" do
|
97
|
-
results = User.scoped_tags(organization_id: @organization_1.id, name: @user_1.name)
|
98
|
-
results.empty?.should be_false
|
99
|
-
results.include?( {:name => "linux", :count => 1 } ).should be_true
|
100
|
-
results.include?( {:name => "ubuntu", :count => 1 } ).should be_true
|
101
|
-
results.include?( {:name => "tucuman", :count => 1 } ).should be_true
|
102
|
-
end
|
103
|
-
|
104
|
-
after do
|
105
|
-
cleanup_database
|
106
|
-
end
|
107
68
|
|
108
|
-
|
69
|
+
before(:each) do
|
70
|
+
@organization_1 = Organization.create(name: 'Qualica')
|
71
|
+
@user_1 = @organization_1.users.create(name: 'User1', tag_list: "ubuntu, linux, tucuman")
|
72
|
+
@user_2 = @organization_1.users.create(name: 'User2', tag_list: "ubuntu, linux, tucuman")
|
73
|
+
|
74
|
+
@organization_2 = Organization.create(name: 'Microsoft')
|
75
|
+
@user_3 = @organization_2.users.create(name: 'User3', tag_list: 'microsoft, windows, tucuman')
|
76
|
+
@user_4 = @organization_2.users.create(name: 'User4', tag_list: 'ubuntu, linux, tucuman')
|
77
|
+
end
|
78
|
+
|
79
|
+
it "should return scoped tags when passing one option" do
|
80
|
+
results = User.all_tags(organization_id: @organization_1.id)
|
81
|
+
results.empty?.should be_false
|
82
|
+
|
83
|
+
results.include?( {:name => "linux", :count => 2 } ).should be_true
|
84
|
+
results.include?( {:name => "ubuntu", :count => 2 } ).should be_true
|
85
|
+
results.include?( {:name => "tucuman", :count => 2 } ).should be_true
|
86
|
+
|
87
|
+
results = User.all_tags(organization_id: @organization_2.id)
|
88
|
+
results.empty?.should be_false
|
89
|
+
results.include?( {:name =>"linux", :count => 1 } ).should be_true
|
90
|
+
results.include?( {:name => "microsoft", :count => 1 } ).should be_true
|
91
|
+
results.include?( {:name => "windows", :count => 1 } ).should be_true
|
92
|
+
results.include?( {:name => "tucuman", :count => 2 } ).should be_true
|
93
|
+
end
|
94
|
+
|
95
|
+
it "should return scoped tags when passing more than one option" do
|
96
|
+
results = User.all_tags(organization_id: @organization_1.id, name: @user_1.name)
|
97
|
+
|
98
|
+
results.empty?.should be_false
|
99
|
+
results.include?( {:name => "linux", :count => 1 } ).should be_true
|
100
|
+
results.include?( {:name => "ubuntu", :count => 1 } ).should be_true
|
101
|
+
results.include?( {:name => "tucuman", :count => 1 } ).should be_true
|
102
|
+
end
|
103
|
+
|
104
|
+
it "should return scoped tags when calling deprecated scoped_tags method" do
|
105
|
+
results = User.all_tags(organization_id: @organization_1.id)
|
106
|
+
|
107
|
+
results.empty?.should be_false
|
108
|
+
results.include?( {:name => "linux", :count => 2 } ).should be_true
|
109
|
+
results.include?( {:name => "ubuntu", :count => 2 } ).should be_true
|
110
|
+
results.include?( {:name => "tucuman", :count => 2 } ).should be_true
|
111
|
+
end
|
109
112
|
|
110
|
-
|
111
|
-
Mongoid.database.collections.each do |collection|
|
112
|
-
collection.remove if collection.name !=~ /^system\./
|
113
|
-
end
|
114
|
-
end
|
113
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,8 +1,10 @@
|
|
1
1
|
require 'mongoid'
|
2
2
|
require 'mongoid-simple-tags'
|
3
3
|
|
4
|
-
Mongoid.
|
5
|
-
config.master = Mongo::Connection.new.db("mongoid_simple_tags_test")
|
6
|
-
end
|
7
|
-
|
4
|
+
Mongoid.load!("mongoid.yml", :test)
|
8
5
|
|
6
|
+
RSpec.configure do |config|
|
7
|
+
config.after(:each) do
|
8
|
+
Mongoid::Config.purge!
|
9
|
+
end
|
10
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid-simple-tags
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-07-
|
12
|
+
date: 2012-07-31 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
@@ -34,7 +34,7 @@ dependencies:
|
|
34
34
|
requirements:
|
35
35
|
- - ~>
|
36
36
|
- !ruby/object:Gem::Version
|
37
|
-
version:
|
37
|
+
version: 3.0.3
|
38
38
|
type: :runtime
|
39
39
|
prerelease: false
|
40
40
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -42,7 +42,7 @@ dependencies:
|
|
42
42
|
requirements:
|
43
43
|
- - ~>
|
44
44
|
- !ruby/object:Gem::Version
|
45
|
-
version:
|
45
|
+
version: 3.0.3
|
46
46
|
- !ruby/object:Gem::Dependency
|
47
47
|
name: bson_ext
|
48
48
|
requirement: !ruby/object:Gem::Requirement
|
@@ -68,6 +68,7 @@ extra_rdoc_files: []
|
|
68
68
|
files:
|
69
69
|
- .gitignore
|
70
70
|
- .rspec
|
71
|
+
- .travis.yml
|
71
72
|
- Gemfile
|
72
73
|
- Gemfile.lock
|
73
74
|
- LICENSE.txt
|
@@ -76,6 +77,7 @@ files:
|
|
76
77
|
- VERSION
|
77
78
|
- lib/mongoid-simple-tags.rb
|
78
79
|
- mongoid-simple-tags.gemspec
|
80
|
+
- mongoid.yml
|
79
81
|
- spec/mongoid-simple-tags_spec.rb
|
80
82
|
- spec/spec_helper.rb
|
81
83
|
homepage: https://github.com/chebyte/mongoid-simple-tags
|