mongoid_taggable_with_context 1.1.3 → 1.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +13 -5
- data/.document +5 -5
- data/.travis.yml +7 -7
- data/Gemfile +12 -12
- data/LICENSE.txt +20 -20
- data/README.md +223 -223
- data/Rakefile +32 -32
- data/lib/mongoid/taggable_with_context.rb +320 -320
- data/lib/mongoid/taggable_with_context/aggregation_strategy/map_reduce.rb +71 -71
- data/lib/mongoid/taggable_with_context/aggregation_strategy/real_time.rb +116 -116
- data/lib/mongoid/taggable_with_context/aggregation_strategy/real_time_group_by.rb +51 -51
- data/lib/mongoid/taggable_with_context/deprecations.rb +25 -25
- data/lib/mongoid/taggable_with_context/version.rb +6 -6
- data/lib/mongoid_taggable_with_context.rb +7 -7
- data/mongoid_taggable_with_context.gemspec +71 -70
- data/spec/mongoid_taggable_with_context_spec.rb +465 -465
- data/spec/spec_helper.rb +16 -16
- metadata +25 -22
@@ -1,26 +1,26 @@
|
|
1
|
-
module Mongoid::TaggableWithContext::GroupBy
|
2
|
-
module TaggableWithContext
|
3
|
-
extend ActiveSupport::Concern
|
4
|
-
included do
|
5
|
-
raise <<-ERR
|
6
|
-
Mongoid::TaggableWithContext::GroupBy::TaggableWithContext is no longer used.
|
7
|
-
Instead, please include both Mongoid::TaggableWithContext and
|
8
|
-
Mongoid::TaggableWithContext::AggregationStrategy::RealTimeGroupBy
|
9
|
-
in your Model.
|
10
|
-
ERR
|
11
|
-
end
|
12
|
-
end
|
13
|
-
module AggregationStrategy
|
14
|
-
module RealTime
|
15
|
-
extend ActiveSupport::Concern
|
16
|
-
included do
|
17
|
-
raise <<-ERR
|
18
|
-
Mongoid::TaggableWithContext::GroupBy::AggregationStrategy::RealTime
|
19
|
-
is no longer used. Instead, please include both Mongoid::TaggableWithContext and
|
20
|
-
Mongoid::TaggableWithContext::AggregationStrategy::RealTimeGroupBy
|
21
|
-
in your Model.
|
22
|
-
ERR
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
1
|
+
module Mongoid::TaggableWithContext::GroupBy
|
2
|
+
module TaggableWithContext
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
included do
|
5
|
+
raise <<-ERR
|
6
|
+
Mongoid::TaggableWithContext::GroupBy::TaggableWithContext is no longer used.
|
7
|
+
Instead, please include both Mongoid::TaggableWithContext and
|
8
|
+
Mongoid::TaggableWithContext::AggregationStrategy::RealTimeGroupBy
|
9
|
+
in your Model.
|
10
|
+
ERR
|
11
|
+
end
|
12
|
+
end
|
13
|
+
module AggregationStrategy
|
14
|
+
module RealTime
|
15
|
+
extend ActiveSupport::Concern
|
16
|
+
included do
|
17
|
+
raise <<-ERR
|
18
|
+
Mongoid::TaggableWithContext::GroupBy::AggregationStrategy::RealTime
|
19
|
+
is no longer used. Instead, please include both Mongoid::TaggableWithContext and
|
20
|
+
Mongoid::TaggableWithContext::AggregationStrategy::RealTimeGroupBy
|
21
|
+
in your Model.
|
22
|
+
ERR
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
26
|
end
|
@@ -1,6 +1,6 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
module Mongoid
|
3
|
-
module TaggableWithContext
|
4
|
-
VERSION = '1.1.
|
5
|
-
end
|
6
|
-
end
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid
|
3
|
+
module TaggableWithContext
|
4
|
+
VERSION = '1.1.4'
|
5
|
+
end
|
6
|
+
end
|
@@ -1,8 +1,8 @@
|
|
1
|
-
require 'active_support/concern'
|
2
|
-
|
3
|
-
require File.join(File.dirname(__FILE__), 'mongoid/taggable_with_context')
|
4
|
-
require File.join(File.dirname(__FILE__), 'mongoid/taggable_with_context/aggregation_strategy/map_reduce')
|
5
|
-
require File.join(File.dirname(__FILE__), 'mongoid/taggable_with_context/aggregation_strategy/real_time')
|
6
|
-
require File.join(File.dirname(__FILE__), 'mongoid/taggable_with_context/aggregation_strategy/real_time_group_by')
|
7
|
-
require File.join(File.dirname(__FILE__), 'mongoid/taggable_with_context/deprecations')
|
1
|
+
require 'active_support/concern'
|
2
|
+
|
3
|
+
require File.join(File.dirname(__FILE__), 'mongoid/taggable_with_context')
|
4
|
+
require File.join(File.dirname(__FILE__), 'mongoid/taggable_with_context/aggregation_strategy/map_reduce')
|
5
|
+
require File.join(File.dirname(__FILE__), 'mongoid/taggable_with_context/aggregation_strategy/real_time')
|
6
|
+
require File.join(File.dirname(__FILE__), 'mongoid/taggable_with_context/aggregation_strategy/real_time_group_by')
|
7
|
+
require File.join(File.dirname(__FILE__), 'mongoid/taggable_with_context/deprecations')
|
8
8
|
require File.join(File.dirname(__FILE__), 'mongoid/taggable_with_context/version')
|
@@ -1,70 +1,71 @@
|
|
1
|
-
# Generated by jeweler
|
2
|
-
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
-
# Instead, edit Jeweler::Tasks in
|
4
|
-
# -*- encoding: utf-8 -*-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
s.
|
9
|
-
|
10
|
-
|
11
|
-
s.
|
12
|
-
s.
|
13
|
-
s.
|
14
|
-
s.
|
15
|
-
|
16
|
-
"
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
".
|
21
|
-
"
|
22
|
-
"
|
23
|
-
"
|
24
|
-
"
|
25
|
-
"
|
26
|
-
"
|
27
|
-
"lib/mongoid/taggable_with_context
|
28
|
-
"lib/mongoid/taggable_with_context/aggregation_strategy/
|
29
|
-
"lib/mongoid/taggable_with_context/aggregation_strategy/
|
30
|
-
"lib/mongoid/taggable_with_context/
|
31
|
-
"lib/mongoid/taggable_with_context/
|
32
|
-
"lib/
|
33
|
-
"mongoid_taggable_with_context.
|
34
|
-
"
|
35
|
-
"spec/
|
36
|
-
|
37
|
-
|
38
|
-
s.
|
39
|
-
s.
|
40
|
-
s.
|
41
|
-
s.
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
s.
|
49
|
-
s.add_development_dependency(%q<
|
50
|
-
s.add_development_dependency(%q<
|
51
|
-
s.add_development_dependency(%q<
|
52
|
-
s.add_development_dependency(%q<
|
53
|
-
|
54
|
-
|
55
|
-
s.add_dependency(%q<
|
56
|
-
s.add_dependency(%q<
|
57
|
-
s.add_dependency(%q<
|
58
|
-
s.add_dependency(%q<
|
59
|
-
s.add_dependency(%q<
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
s.add_dependency(%q<
|
64
|
-
s.add_dependency(%q<
|
65
|
-
s.add_dependency(%q<
|
66
|
-
s.add_dependency(%q<
|
67
|
-
s.add_dependency(%q<
|
68
|
-
|
69
|
-
end
|
70
|
-
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
# stub: mongoid_taggable_with_context 1.1.4 ruby lib
|
6
|
+
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.name = "mongoid_taggable_with_context"
|
9
|
+
s.version = "1.1.4"
|
10
|
+
|
11
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
12
|
+
s.authors = ["Aaron Qian", "Luca G. Soave", "John Shields", "Wilker Lucio", "Ches Martin"]
|
13
|
+
s.date = "2013-10-14"
|
14
|
+
s.description = "Add multiple tag fields on Mongoid documents with aggregation capability."
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.md"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".travis.yml",
|
22
|
+
"Gemfile",
|
23
|
+
"LICENSE.txt",
|
24
|
+
"README.md",
|
25
|
+
"Rakefile",
|
26
|
+
"init.rb",
|
27
|
+
"lib/mongoid/taggable_with_context.rb",
|
28
|
+
"lib/mongoid/taggable_with_context/aggregation_strategy/map_reduce.rb",
|
29
|
+
"lib/mongoid/taggable_with_context/aggregation_strategy/real_time.rb",
|
30
|
+
"lib/mongoid/taggable_with_context/aggregation_strategy/real_time_group_by.rb",
|
31
|
+
"lib/mongoid/taggable_with_context/deprecations.rb",
|
32
|
+
"lib/mongoid/taggable_with_context/version.rb",
|
33
|
+
"lib/mongoid_taggable_with_context.rb",
|
34
|
+
"mongoid_taggable_with_context.gemspec",
|
35
|
+
"spec/mongoid_taggable_with_context_spec.rb",
|
36
|
+
"spec/spec_helper.rb"
|
37
|
+
]
|
38
|
+
s.homepage = "http://github.com/lgs/mongoid_taggable_with_context"
|
39
|
+
s.licenses = ["MIT"]
|
40
|
+
s.require_paths = ["lib"]
|
41
|
+
s.rubygems_version = "2.1.8"
|
42
|
+
s.summary = "Mongoid taggable behaviour"
|
43
|
+
|
44
|
+
if s.respond_to? :specification_version then
|
45
|
+
s.specification_version = 4
|
46
|
+
|
47
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
48
|
+
s.add_runtime_dependency(%q<mongoid>, [">= 3.0.0"])
|
49
|
+
s.add_development_dependency(%q<rake>, [">= 0"])
|
50
|
+
s.add_development_dependency(%q<rspec>, [">= 0"])
|
51
|
+
s.add_development_dependency(%q<yard>, [">= 0"])
|
52
|
+
s.add_development_dependency(%q<bundler>, [">= 1.0.0"])
|
53
|
+
s.add_development_dependency(%q<jeweler>, [">= 0"])
|
54
|
+
else
|
55
|
+
s.add_dependency(%q<mongoid>, [">= 3.0.0"])
|
56
|
+
s.add_dependency(%q<rake>, [">= 0"])
|
57
|
+
s.add_dependency(%q<rspec>, [">= 0"])
|
58
|
+
s.add_dependency(%q<yard>, [">= 0"])
|
59
|
+
s.add_dependency(%q<bundler>, [">= 1.0.0"])
|
60
|
+
s.add_dependency(%q<jeweler>, [">= 0"])
|
61
|
+
end
|
62
|
+
else
|
63
|
+
s.add_dependency(%q<mongoid>, [">= 3.0.0"])
|
64
|
+
s.add_dependency(%q<rake>, [">= 0"])
|
65
|
+
s.add_dependency(%q<rspec>, [">= 0"])
|
66
|
+
s.add_dependency(%q<yard>, [">= 0"])
|
67
|
+
s.add_dependency(%q<bundler>, [">= 1.0.0"])
|
68
|
+
s.add_dependency(%q<jeweler>, [">= 0"])
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
@@ -1,465 +1,465 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
-
|
3
|
-
class MyModel
|
4
|
-
include Mongoid::Document
|
5
|
-
include Mongoid::TaggableWithContext
|
6
|
-
|
7
|
-
taggable
|
8
|
-
taggable :artists
|
9
|
-
taggable :albums, default: []
|
10
|
-
end
|
11
|
-
|
12
|
-
class M1
|
13
|
-
include Mongoid::Document
|
14
|
-
include Mongoid::TaggableWithContext
|
15
|
-
include Mongoid::TaggableWithContext::AggregationStrategy::MapReduce
|
16
|
-
|
17
|
-
taggable
|
18
|
-
taggable :a, as: :artists
|
19
|
-
end
|
20
|
-
|
21
|
-
class M2
|
22
|
-
include Mongoid::Document
|
23
|
-
include Mongoid::TaggableWithContext
|
24
|
-
include Mongoid::TaggableWithContext::AggregationStrategy::RealTime
|
25
|
-
|
26
|
-
taggable
|
27
|
-
taggable :artists
|
28
|
-
end
|
29
|
-
|
30
|
-
class M3
|
31
|
-
include Mongoid::Document
|
32
|
-
include Mongoid::TaggableWithContext
|
33
|
-
include Mongoid::TaggableWithContext::AggregationStrategy::RealTimeGroupBy
|
34
|
-
|
35
|
-
field :user
|
36
|
-
taggable group_by_field: :user
|
37
|
-
taggable :artists, group_by_field: :user
|
38
|
-
end
|
39
|
-
|
40
|
-
describe Mongoid::TaggableWithContext do
|
41
|
-
|
42
|
-
context "default field value" do
|
43
|
-
before :each do
|
44
|
-
@m = MyModel.new
|
45
|
-
end
|
46
|
-
|
47
|
-
it "should be nil for artists" do
|
48
|
-
@m.artists.should eql nil
|
49
|
-
end
|
50
|
-
|
51
|
-
it "should be array for albums" do
|
52
|
-
@m.albums.should eql []
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
context "saving tags from plain text" do
|
57
|
-
before :each do
|
58
|
-
@m = MyModel.new
|
59
|
-
end
|
60
|
-
|
61
|
-
it "should set tags from string" do
|
62
|
-
@m.tags = "some new tag"
|
63
|
-
@m.tags.should == %w[some new tag]
|
64
|
-
end
|
65
|
-
|
66
|
-
it "should set artists tags from string" do
|
67
|
-
@m.artists = "some new tag"
|
68
|
-
@m.artists.should == %w[some new tag]
|
69
|
-
end
|
70
|
-
|
71
|
-
it "should retrieve tags string" do
|
72
|
-
@m.tags = %w[some new tags]
|
73
|
-
@m.tags_string.should == "some new tags"
|
74
|
-
end
|
75
|
-
|
76
|
-
it "should set tags from tags string" do
|
77
|
-
tags = "some cool tags"
|
78
|
-
@m.albums_string = tags
|
79
|
-
@m.albums.should == tags.split(' ')
|
80
|
-
end
|
81
|
-
|
82
|
-
it "should retrieve artists string" do
|
83
|
-
@m.artists = %w[some new tags]
|
84
|
-
@m.artists_string.should == "some new tags"
|
85
|
-
end
|
86
|
-
|
87
|
-
it "should strip tags before put in array" do
|
88
|
-
@m.tags = "now with some spaces in places "
|
89
|
-
@m.tags.should == %w[now with some spaces in places]
|
90
|
-
end
|
91
|
-
|
92
|
-
it "should remove repeated tags from string" do
|
93
|
-
@m.tags = "some new tags some new tags"
|
94
|
-
@m.tags.should == %w[some new tags]
|
95
|
-
end
|
96
|
-
|
97
|
-
it "should remove repeated tags from array" do
|
98
|
-
@m.tags = %w[some new tags some new tags]
|
99
|
-
@m.tags.should == %w[some new tags]
|
100
|
-
end
|
101
|
-
|
102
|
-
it "should remove nil tags from array" do
|
103
|
-
@m.tags = ["some", nil, "new", nil, "tags"]
|
104
|
-
@m.tags.should == %w[some new tags]
|
105
|
-
end
|
106
|
-
|
107
|
-
it "should allow tags to be set to nil" do
|
108
|
-
@m.tags = nil
|
109
|
-
@m.tags.should == nil
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
|
-
context "saving tags from array" do
|
114
|
-
before :each do
|
115
|
-
@m = MyModel.new
|
116
|
-
end
|
117
|
-
|
118
|
-
it "should remove repeated tags from array" do
|
119
|
-
@m.tags = %w[some new tags some new tags]
|
120
|
-
@m.tags.should == %w[some new tags]
|
121
|
-
end
|
122
|
-
|
123
|
-
it "should remove nil tags from array" do
|
124
|
-
@m.tags = ["some", nil, "new", nil, "tags"]
|
125
|
-
@m.tags.should == %w[some new tags]
|
126
|
-
end
|
127
|
-
|
128
|
-
it "should remove empty strings from array" do
|
129
|
-
@m.tags = ["some", "", "new", "", "tags"]
|
130
|
-
@m.tags.should == %w[some new tags]
|
131
|
-
end
|
132
|
-
end
|
133
|
-
|
134
|
-
context "tagged_with" do
|
135
|
-
before :each do
|
136
|
-
@m1 = MyModel.create!(tags: "food ant bee", artists: "jeff greg mandy aaron andy")
|
137
|
-
@m2 = MyModel.create!(tags: "juice food bee zip", artists: "grant andrew andy")
|
138
|
-
@m3 = MyModel.create!(tags: "honey strip food", artists: "mandy aaron andy")
|
139
|
-
end
|
140
|
-
|
141
|
-
it "should retrieve a list of documents" do
|
142
|
-
(MyModel.tags_tagged_with("food").to_a - [@m1, @m2, @m3]).should be_empty
|
143
|
-
(MyModel.artists_tagged_with("aaron").to_a - [@m1, @m3]).should be_empty
|
144
|
-
end
|
145
|
-
end
|
146
|
-
|
147
|
-
context "no aggregation" do
|
148
|
-
it "should raise AggregationStrategyMissing exception when retreiving tags" do
|
149
|
-
lambda{ MyModel.tags }.should raise_error(Mongoid::TaggableWithContext::AggregationStrategyMissing)
|
150
|
-
end
|
151
|
-
|
152
|
-
it "should raise AggregationStrategyMissing exception when retreiving tags with weights" do
|
153
|
-
lambda{ MyModel.tags_with_weight }.should raise_error(Mongoid::TaggableWithContext::AggregationStrategyMissing)
|
154
|
-
end
|
155
|
-
|
156
|
-
end
|
157
|
-
|
158
|
-
shared_examples_for "aggregation" do
|
159
|
-
context "retrieving index" do
|
160
|
-
context "when there's no tags'" do
|
161
|
-
it "should return an empty array" do
|
162
|
-
klass.tags.should == []
|
163
|
-
klass.artists.should == []
|
164
|
-
|
165
|
-
klass.tags_with_weight.should == []
|
166
|
-
klass.artists_with_weight == []
|
167
|
-
end
|
168
|
-
end
|
169
|
-
|
170
|
-
context "on create directly" do
|
171
|
-
before :each do
|
172
|
-
klass.create!(user: "user1", tags: "food ant bee", artists: "jeff greg mandy aaron andy")
|
173
|
-
klass.create!(user: "user1", tags: "juice food bee zip", artists: "grant andrew andy")
|
174
|
-
klass.create!(user: "user2", tags: "honey strip food", artists: "mandy aaron andy")
|
175
|
-
end
|
176
|
-
|
177
|
-
it "should retrieve the list of all saved tags distinct and ordered" do
|
178
|
-
klass.tags.should == %w[ant bee food honey juice strip zip]
|
179
|
-
klass.artists.should == %w[aaron andrew andy grant greg jeff mandy]
|
180
|
-
end
|
181
|
-
|
182
|
-
it "should retrieve a list of tags with weight" do
|
183
|
-
klass.tags_with_weight.should == [
|
184
|
-
['ant', 1],
|
185
|
-
['bee', 2],
|
186
|
-
['food', 3],
|
187
|
-
['honey', 1],
|
188
|
-
['juice', 1],
|
189
|
-
['strip', 1],
|
190
|
-
['zip', 1]
|
191
|
-
]
|
192
|
-
|
193
|
-
klass.artists_with_weight.should == [
|
194
|
-
['aaron', 2],
|
195
|
-
['andrew', 1],
|
196
|
-
['andy', 3],
|
197
|
-
['grant', 1],
|
198
|
-
['greg', 1],
|
199
|
-
['jeff', 1],
|
200
|
-
['mandy', 2]
|
201
|
-
]
|
202
|
-
end
|
203
|
-
end
|
204
|
-
|
205
|
-
context "on new then change attributes directly" do
|
206
|
-
before :each do
|
207
|
-
m = klass.new
|
208
|
-
m.tags = "food ant bee"
|
209
|
-
m.artists = "jeff greg mandy aaron andy"
|
210
|
-
m.save!
|
211
|
-
|
212
|
-
m = klass.new
|
213
|
-
m.tags = "juice food bee zip"
|
214
|
-
m.artists = "grant andrew andy"
|
215
|
-
m.save!
|
216
|
-
|
217
|
-
m = klass.new
|
218
|
-
m.tags = "honey strip food"
|
219
|
-
m.artists = "mandy aaron andy"
|
220
|
-
m.save!
|
221
|
-
end
|
222
|
-
|
223
|
-
it "should retrieve the list of all saved tags distinct and ordered" do
|
224
|
-
klass.tags.should == %w[ant bee food honey juice strip zip]
|
225
|
-
klass.artists.should == %w[aaron andrew andy grant greg jeff mandy]
|
226
|
-
end
|
227
|
-
|
228
|
-
it "should retrieve a list of tags with weight" do
|
229
|
-
klass.tags_with_weight.should == [
|
230
|
-
['ant', 1],
|
231
|
-
['bee', 2],
|
232
|
-
['food', 3],
|
233
|
-
['honey', 1],
|
234
|
-
['juice', 1],
|
235
|
-
['strip', 1],
|
236
|
-
['zip', 1]
|
237
|
-
]
|
238
|
-
|
239
|
-
klass.artists_with_weight.should == [
|
240
|
-
['aaron', 2],
|
241
|
-
['andrew', 1],
|
242
|
-
['andy', 3],
|
243
|
-
['grant', 1],
|
244
|
-
['greg', 1],
|
245
|
-
['jeff', 1],
|
246
|
-
['mandy', 2]
|
247
|
-
]
|
248
|
-
end
|
249
|
-
end
|
250
|
-
|
251
|
-
context "on create then update" do
|
252
|
-
before :each do
|
253
|
-
m1 = klass.create!(user: "user1", tags: "food ant bee", artists: "jeff greg mandy aaron andy")
|
254
|
-
m2 = klass.create!(user: "user1", tags: "juice food bee zip", artists: "grant andrew andy")
|
255
|
-
m3 = klass.create!(user: "user2", tags: "honey strip food", artists: "mandy aaron andy")
|
256
|
-
|
257
|
-
m1.tags = m1.tags + %w[honey strip shoe]
|
258
|
-
m1.save!
|
259
|
-
|
260
|
-
m3.artists = m3.artists + %w[grant greg gory]
|
261
|
-
m3.save!
|
262
|
-
end
|
263
|
-
|
264
|
-
it "should retrieve the list of all saved tags distinct and ordered" do
|
265
|
-
klass.tags.should == %w[ant bee food honey juice shoe strip zip]
|
266
|
-
klass.artists.should == %w[aaron andrew andy gory grant greg jeff mandy]
|
267
|
-
end
|
268
|
-
|
269
|
-
it "should retrieve a list of tags with weight" do
|
270
|
-
klass.tags_with_weight.should == [
|
271
|
-
['ant', 1],
|
272
|
-
['bee', 2],
|
273
|
-
['food', 3],
|
274
|
-
['honey', 2],
|
275
|
-
['juice', 1],
|
276
|
-
['shoe', 1],
|
277
|
-
['strip', 2],
|
278
|
-
['zip', 1]
|
279
|
-
]
|
280
|
-
|
281
|
-
klass.artists_with_weight.should == [
|
282
|
-
['aaron', 2],
|
283
|
-
['andrew', 1],
|
284
|
-
['andy', 3],
|
285
|
-
['gory', 1],
|
286
|
-
['grant', 2],
|
287
|
-
['greg', 2],
|
288
|
-
['jeff', 1],
|
289
|
-
['mandy', 2]
|
290
|
-
]
|
291
|
-
end
|
292
|
-
end
|
293
|
-
|
294
|
-
context "on create, update, then destroy" do
|
295
|
-
before :each do
|
296
|
-
m1 = klass.create!(user: "user1", tags: "food ant bee", artists: "jeff greg mandy aaron andy")
|
297
|
-
m2 = klass.create!(user: "user1", tags: "juice food bee zip", artists: "grant andrew andy")
|
298
|
-
m3 = klass.create!(user: "user2", tags: "honey strip food", artists: "mandy aaron andy")
|
299
|
-
|
300
|
-
m1.tags = m1.tags + %w[honey strip shoe] - %w[food]
|
301
|
-
m1.save!
|
302
|
-
|
303
|
-
m3.artists = m3.artists + %w[grant greg gory] - %w[andy]
|
304
|
-
m3.save!
|
305
|
-
|
306
|
-
m2.destroy
|
307
|
-
end
|
308
|
-
|
309
|
-
it "should retrieve the list of all saved tags distinct and ordered" do
|
310
|
-
klass.tags.should == %w[ant bee food honey shoe strip]
|
311
|
-
klass.artists.should == %w[aaron andy gory grant greg jeff mandy]
|
312
|
-
end
|
313
|
-
|
314
|
-
it "should retrieve a list of tags with weight" do
|
315
|
-
klass.tags_with_weight.should == [
|
316
|
-
['ant', 1],
|
317
|
-
['bee', 1],
|
318
|
-
['food', 1],
|
319
|
-
['honey', 2],
|
320
|
-
['shoe', 1],
|
321
|
-
['strip', 2]
|
322
|
-
]
|
323
|
-
|
324
|
-
klass.artists_with_weight.should == [
|
325
|
-
['aaron', 2],
|
326
|
-
['andy', 1],
|
327
|
-
['gory', 1],
|
328
|
-
['grant', 1],
|
329
|
-
['greg', 2],
|
330
|
-
['jeff', 1],
|
331
|
-
['mandy', 2]
|
332
|
-
]
|
333
|
-
end
|
334
|
-
end
|
335
|
-
end
|
336
|
-
end
|
337
|
-
|
338
|
-
context "map-reduce aggregation" do
|
339
|
-
let(:klass) { M1 }
|
340
|
-
it_should_behave_like "aggregation"
|
341
|
-
|
342
|
-
it "should generate the tags aggregation collection name correctly" do
|
343
|
-
klass.aggregation_collection_for(:tags).should == "m1s_tags_aggregation"
|
344
|
-
end
|
345
|
-
|
346
|
-
it "should generate the artists aggregation collection name correctly" do
|
347
|
-
klass.aggregation_collection_for(:artists).should == "m1s_artists_aggregation"
|
348
|
-
end
|
349
|
-
end
|
350
|
-
|
351
|
-
context "realtime aggregation" do
|
352
|
-
let(:klass) { M2 }
|
353
|
-
it_should_behave_like "aggregation"
|
354
|
-
|
355
|
-
it "should generate the tags aggregation collection name correctly" do
|
356
|
-
klass.aggregation_collection_for(:tags).should == "m2s_tags_aggregation"
|
357
|
-
end
|
358
|
-
|
359
|
-
it "should generate the artists aggregation collection name correctly" do
|
360
|
-
klass.aggregation_collection_for(:artists).should == "m2s_artists_aggregation"
|
361
|
-
end
|
362
|
-
end
|
363
|
-
|
364
|
-
context "realtime aggregation group by" do
|
365
|
-
let(:klass) { M3 }
|
366
|
-
it_should_behave_like "aggregation"
|
367
|
-
|
368
|
-
it "should have artists_group_by_field value :user" do
|
369
|
-
klass.artists_group_by_field.should == :user
|
370
|
-
end
|
371
|
-
|
372
|
-
it "should generate the tags aggregation collection name correctly" do
|
373
|
-
klass.aggregation_collection_for(:tags).should == "m3s_tags_aggregation"
|
374
|
-
end
|
375
|
-
|
376
|
-
it "should generate the artists aggregation collection name correctly" do
|
377
|
-
klass.aggregation_collection_for(:artists).should == "m3s_artists_aggregation"
|
378
|
-
end
|
379
|
-
|
380
|
-
context "for groupings" do
|
381
|
-
before :each do
|
382
|
-
klass.create!(user: "user1", tags: "food ant bee", artists: "jeff greg mandy aaron andy")
|
383
|
-
klass.create!(user: "user1", tags: "juice food bee zip", artists: "grant andrew andy")
|
384
|
-
klass.create!(user: "user2", tags: "honey strip food", artists: "mandy aaron andy")
|
385
|
-
end
|
386
|
-
|
387
|
-
it "should retrieve the list of all saved tags distinct and ordered" do
|
388
|
-
klass.tags("user1").should == %w[ant bee food juice zip]
|
389
|
-
klass.tags("user2").should == %w[food honey strip]
|
390
|
-
|
391
|
-
klass.artists("user1").should == %w[aaron andrew andy grant greg jeff mandy]
|
392
|
-
klass.artists("user2").should == %w[aaron andy mandy]
|
393
|
-
end
|
394
|
-
|
395
|
-
it "should retrieve a list of tags with weight" do
|
396
|
-
klass.tags_with_weight("user1").should == [
|
397
|
-
['ant', 1],
|
398
|
-
['bee', 2],
|
399
|
-
['food', 2],
|
400
|
-
['juice', 1],
|
401
|
-
['zip', 1]
|
402
|
-
]
|
403
|
-
|
404
|
-
klass.tags_with_weight("user2").should == [
|
405
|
-
['food', 1],
|
406
|
-
['honey', 1],
|
407
|
-
['strip', 1]
|
408
|
-
]
|
409
|
-
|
410
|
-
klass.artists_with_weight("user1").should == [
|
411
|
-
['aaron', 1],
|
412
|
-
['andrew', 1],
|
413
|
-
['andy', 2],
|
414
|
-
['grant', 1],
|
415
|
-
['greg', 1],
|
416
|
-
['jeff', 1],
|
417
|
-
['mandy', 1]
|
418
|
-
]
|
419
|
-
|
420
|
-
klass.artists_with_weight("user2").should == [
|
421
|
-
['aaron', 1],
|
422
|
-
['andy', 1],
|
423
|
-
['mandy', 1]
|
424
|
-
]
|
425
|
-
end
|
426
|
-
end
|
427
|
-
end
|
428
|
-
|
429
|
-
context "removed options" do
|
430
|
-
it "should throw error if :field option is specified" do
|
431
|
-
expect do
|
432
|
-
class Invalid
|
433
|
-
include Mongoid::Document
|
434
|
-
include Mongoid::TaggableWithContext
|
435
|
-
taggable field: :foobar
|
436
|
-
end
|
437
|
-
end.to raise_error
|
438
|
-
end
|
439
|
-
it "should throw error if :string_method option is specified" do
|
440
|
-
expect do
|
441
|
-
class Invalid
|
442
|
-
include Mongoid::Document
|
443
|
-
include Mongoid::TaggableWithContext
|
444
|
-
taggable string_method: :foobar
|
445
|
-
end
|
446
|
-
end.to raise_error
|
447
|
-
end
|
448
|
-
it "should throw error if GroupBy::TaggableWithContext module is included" do
|
449
|
-
expect do
|
450
|
-
class Invalid
|
451
|
-
include Mongoid::Document
|
452
|
-
include Mongoid::TaggableWithContext::GroupBy::TaggableWithContext
|
453
|
-
end
|
454
|
-
end.to raise_error
|
455
|
-
end
|
456
|
-
it "should throw error if GroupBy::AggregationStrategy::RealTime module is included" do
|
457
|
-
expect do
|
458
|
-
class Invalid
|
459
|
-
include Mongoid::Document
|
460
|
-
include Mongoid::TaggableWithContext::GroupBy::AggregationStrategy::RealTime
|
461
|
-
end
|
462
|
-
end.to raise_error
|
463
|
-
end
|
464
|
-
end
|
465
|
-
end
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
class MyModel
|
4
|
+
include Mongoid::Document
|
5
|
+
include Mongoid::TaggableWithContext
|
6
|
+
|
7
|
+
taggable
|
8
|
+
taggable :artists
|
9
|
+
taggable :albums, default: []
|
10
|
+
end
|
11
|
+
|
12
|
+
class M1
|
13
|
+
include Mongoid::Document
|
14
|
+
include Mongoid::TaggableWithContext
|
15
|
+
include Mongoid::TaggableWithContext::AggregationStrategy::MapReduce
|
16
|
+
|
17
|
+
taggable
|
18
|
+
taggable :a, as: :artists
|
19
|
+
end
|
20
|
+
|
21
|
+
class M2
|
22
|
+
include Mongoid::Document
|
23
|
+
include Mongoid::TaggableWithContext
|
24
|
+
include Mongoid::TaggableWithContext::AggregationStrategy::RealTime
|
25
|
+
|
26
|
+
taggable
|
27
|
+
taggable :artists
|
28
|
+
end
|
29
|
+
|
30
|
+
class M3
|
31
|
+
include Mongoid::Document
|
32
|
+
include Mongoid::TaggableWithContext
|
33
|
+
include Mongoid::TaggableWithContext::AggregationStrategy::RealTimeGroupBy
|
34
|
+
|
35
|
+
field :user
|
36
|
+
taggable group_by_field: :user
|
37
|
+
taggable :artists, group_by_field: :user
|
38
|
+
end
|
39
|
+
|
40
|
+
describe Mongoid::TaggableWithContext do
|
41
|
+
|
42
|
+
context "default field value" do
|
43
|
+
before :each do
|
44
|
+
@m = MyModel.new
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should be nil for artists" do
|
48
|
+
@m.artists.should eql nil
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should be array for albums" do
|
52
|
+
@m.albums.should eql []
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
context "saving tags from plain text" do
|
57
|
+
before :each do
|
58
|
+
@m = MyModel.new
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should set tags from string" do
|
62
|
+
@m.tags = "some new tag"
|
63
|
+
@m.tags.should == %w[some new tag]
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should set artists tags from string" do
|
67
|
+
@m.artists = "some new tag"
|
68
|
+
@m.artists.should == %w[some new tag]
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should retrieve tags string" do
|
72
|
+
@m.tags = %w[some new tags]
|
73
|
+
@m.tags_string.should == "some new tags"
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should set tags from tags string" do
|
77
|
+
tags = "some cool tags"
|
78
|
+
@m.albums_string = tags
|
79
|
+
@m.albums.should == tags.split(' ')
|
80
|
+
end
|
81
|
+
|
82
|
+
it "should retrieve artists string" do
|
83
|
+
@m.artists = %w[some new tags]
|
84
|
+
@m.artists_string.should == "some new tags"
|
85
|
+
end
|
86
|
+
|
87
|
+
it "should strip tags before put in array" do
|
88
|
+
@m.tags = "now with some spaces in places "
|
89
|
+
@m.tags.should == %w[now with some spaces in places]
|
90
|
+
end
|
91
|
+
|
92
|
+
it "should remove repeated tags from string" do
|
93
|
+
@m.tags = "some new tags some new tags"
|
94
|
+
@m.tags.should == %w[some new tags]
|
95
|
+
end
|
96
|
+
|
97
|
+
it "should remove repeated tags from array" do
|
98
|
+
@m.tags = %w[some new tags some new tags]
|
99
|
+
@m.tags.should == %w[some new tags]
|
100
|
+
end
|
101
|
+
|
102
|
+
it "should remove nil tags from array" do
|
103
|
+
@m.tags = ["some", nil, "new", nil, "tags"]
|
104
|
+
@m.tags.should == %w[some new tags]
|
105
|
+
end
|
106
|
+
|
107
|
+
it "should allow tags to be set to nil" do
|
108
|
+
@m.tags = nil
|
109
|
+
@m.tags.should == nil
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
context "saving tags from array" do
|
114
|
+
before :each do
|
115
|
+
@m = MyModel.new
|
116
|
+
end
|
117
|
+
|
118
|
+
it "should remove repeated tags from array" do
|
119
|
+
@m.tags = %w[some new tags some new tags]
|
120
|
+
@m.tags.should == %w[some new tags]
|
121
|
+
end
|
122
|
+
|
123
|
+
it "should remove nil tags from array" do
|
124
|
+
@m.tags = ["some", nil, "new", nil, "tags"]
|
125
|
+
@m.tags.should == %w[some new tags]
|
126
|
+
end
|
127
|
+
|
128
|
+
it "should remove empty strings from array" do
|
129
|
+
@m.tags = ["some", "", "new", "", "tags"]
|
130
|
+
@m.tags.should == %w[some new tags]
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
context "tagged_with" do
|
135
|
+
before :each do
|
136
|
+
@m1 = MyModel.create!(tags: "food ant bee", artists: "jeff greg mandy aaron andy")
|
137
|
+
@m2 = MyModel.create!(tags: "juice food bee zip", artists: "grant andrew andy")
|
138
|
+
@m3 = MyModel.create!(tags: "honey strip food", artists: "mandy aaron andy")
|
139
|
+
end
|
140
|
+
|
141
|
+
it "should retrieve a list of documents" do
|
142
|
+
(MyModel.tags_tagged_with("food").to_a - [@m1, @m2, @m3]).should be_empty
|
143
|
+
(MyModel.artists_tagged_with("aaron").to_a - [@m1, @m3]).should be_empty
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
context "no aggregation" do
|
148
|
+
it "should raise AggregationStrategyMissing exception when retreiving tags" do
|
149
|
+
lambda{ MyModel.tags }.should raise_error(Mongoid::TaggableWithContext::AggregationStrategyMissing)
|
150
|
+
end
|
151
|
+
|
152
|
+
it "should raise AggregationStrategyMissing exception when retreiving tags with weights" do
|
153
|
+
lambda{ MyModel.tags_with_weight }.should raise_error(Mongoid::TaggableWithContext::AggregationStrategyMissing)
|
154
|
+
end
|
155
|
+
|
156
|
+
end
|
157
|
+
|
158
|
+
shared_examples_for "aggregation" do
|
159
|
+
context "retrieving index" do
|
160
|
+
context "when there's no tags'" do
|
161
|
+
it "should return an empty array" do
|
162
|
+
klass.tags.should == []
|
163
|
+
klass.artists.should == []
|
164
|
+
|
165
|
+
klass.tags_with_weight.should == []
|
166
|
+
klass.artists_with_weight == []
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
context "on create directly" do
|
171
|
+
before :each do
|
172
|
+
klass.create!(user: "user1", tags: "food ant bee", artists: "jeff greg mandy aaron andy")
|
173
|
+
klass.create!(user: "user1", tags: "juice food bee zip", artists: "grant andrew andy")
|
174
|
+
klass.create!(user: "user2", tags: "honey strip food", artists: "mandy aaron andy")
|
175
|
+
end
|
176
|
+
|
177
|
+
it "should retrieve the list of all saved tags distinct and ordered" do
|
178
|
+
klass.tags.should == %w[ant bee food honey juice strip zip]
|
179
|
+
klass.artists.should == %w[aaron andrew andy grant greg jeff mandy]
|
180
|
+
end
|
181
|
+
|
182
|
+
it "should retrieve a list of tags with weight" do
|
183
|
+
klass.tags_with_weight.should == [
|
184
|
+
['ant', 1],
|
185
|
+
['bee', 2],
|
186
|
+
['food', 3],
|
187
|
+
['honey', 1],
|
188
|
+
['juice', 1],
|
189
|
+
['strip', 1],
|
190
|
+
['zip', 1]
|
191
|
+
]
|
192
|
+
|
193
|
+
klass.artists_with_weight.should == [
|
194
|
+
['aaron', 2],
|
195
|
+
['andrew', 1],
|
196
|
+
['andy', 3],
|
197
|
+
['grant', 1],
|
198
|
+
['greg', 1],
|
199
|
+
['jeff', 1],
|
200
|
+
['mandy', 2]
|
201
|
+
]
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
context "on new then change attributes directly" do
|
206
|
+
before :each do
|
207
|
+
m = klass.new
|
208
|
+
m.tags = "food ant bee"
|
209
|
+
m.artists = "jeff greg mandy aaron andy"
|
210
|
+
m.save!
|
211
|
+
|
212
|
+
m = klass.new
|
213
|
+
m.tags = "juice food bee zip"
|
214
|
+
m.artists = "grant andrew andy"
|
215
|
+
m.save!
|
216
|
+
|
217
|
+
m = klass.new
|
218
|
+
m.tags = "honey strip food"
|
219
|
+
m.artists = "mandy aaron andy"
|
220
|
+
m.save!
|
221
|
+
end
|
222
|
+
|
223
|
+
it "should retrieve the list of all saved tags distinct and ordered" do
|
224
|
+
klass.tags.should == %w[ant bee food honey juice strip zip]
|
225
|
+
klass.artists.should == %w[aaron andrew andy grant greg jeff mandy]
|
226
|
+
end
|
227
|
+
|
228
|
+
it "should retrieve a list of tags with weight" do
|
229
|
+
klass.tags_with_weight.should == [
|
230
|
+
['ant', 1],
|
231
|
+
['bee', 2],
|
232
|
+
['food', 3],
|
233
|
+
['honey', 1],
|
234
|
+
['juice', 1],
|
235
|
+
['strip', 1],
|
236
|
+
['zip', 1]
|
237
|
+
]
|
238
|
+
|
239
|
+
klass.artists_with_weight.should == [
|
240
|
+
['aaron', 2],
|
241
|
+
['andrew', 1],
|
242
|
+
['andy', 3],
|
243
|
+
['grant', 1],
|
244
|
+
['greg', 1],
|
245
|
+
['jeff', 1],
|
246
|
+
['mandy', 2]
|
247
|
+
]
|
248
|
+
end
|
249
|
+
end
|
250
|
+
|
251
|
+
context "on create then update" do
|
252
|
+
before :each do
|
253
|
+
m1 = klass.create!(user: "user1", tags: "food ant bee", artists: "jeff greg mandy aaron andy")
|
254
|
+
m2 = klass.create!(user: "user1", tags: "juice food bee zip", artists: "grant andrew andy")
|
255
|
+
m3 = klass.create!(user: "user2", tags: "honey strip food", artists: "mandy aaron andy")
|
256
|
+
|
257
|
+
m1.tags = m1.tags + %w[honey strip shoe]
|
258
|
+
m1.save!
|
259
|
+
|
260
|
+
m3.artists = m3.artists + %w[grant greg gory]
|
261
|
+
m3.save!
|
262
|
+
end
|
263
|
+
|
264
|
+
it "should retrieve the list of all saved tags distinct and ordered" do
|
265
|
+
klass.tags.should == %w[ant bee food honey juice shoe strip zip]
|
266
|
+
klass.artists.should == %w[aaron andrew andy gory grant greg jeff mandy]
|
267
|
+
end
|
268
|
+
|
269
|
+
it "should retrieve a list of tags with weight" do
|
270
|
+
klass.tags_with_weight.should == [
|
271
|
+
['ant', 1],
|
272
|
+
['bee', 2],
|
273
|
+
['food', 3],
|
274
|
+
['honey', 2],
|
275
|
+
['juice', 1],
|
276
|
+
['shoe', 1],
|
277
|
+
['strip', 2],
|
278
|
+
['zip', 1]
|
279
|
+
]
|
280
|
+
|
281
|
+
klass.artists_with_weight.should == [
|
282
|
+
['aaron', 2],
|
283
|
+
['andrew', 1],
|
284
|
+
['andy', 3],
|
285
|
+
['gory', 1],
|
286
|
+
['grant', 2],
|
287
|
+
['greg', 2],
|
288
|
+
['jeff', 1],
|
289
|
+
['mandy', 2]
|
290
|
+
]
|
291
|
+
end
|
292
|
+
end
|
293
|
+
|
294
|
+
context "on create, update, then destroy" do
|
295
|
+
before :each do
|
296
|
+
m1 = klass.create!(user: "user1", tags: "food ant bee", artists: "jeff greg mandy aaron andy")
|
297
|
+
m2 = klass.create!(user: "user1", tags: "juice food bee zip", artists: "grant andrew andy")
|
298
|
+
m3 = klass.create!(user: "user2", tags: "honey strip food", artists: "mandy aaron andy")
|
299
|
+
|
300
|
+
m1.tags = m1.tags + %w[honey strip shoe] - %w[food]
|
301
|
+
m1.save!
|
302
|
+
|
303
|
+
m3.artists = m3.artists + %w[grant greg gory] - %w[andy]
|
304
|
+
m3.save!
|
305
|
+
|
306
|
+
m2.destroy
|
307
|
+
end
|
308
|
+
|
309
|
+
it "should retrieve the list of all saved tags distinct and ordered" do
|
310
|
+
klass.tags.should == %w[ant bee food honey shoe strip]
|
311
|
+
klass.artists.should == %w[aaron andy gory grant greg jeff mandy]
|
312
|
+
end
|
313
|
+
|
314
|
+
it "should retrieve a list of tags with weight" do
|
315
|
+
klass.tags_with_weight.should == [
|
316
|
+
['ant', 1],
|
317
|
+
['bee', 1],
|
318
|
+
['food', 1],
|
319
|
+
['honey', 2],
|
320
|
+
['shoe', 1],
|
321
|
+
['strip', 2]
|
322
|
+
]
|
323
|
+
|
324
|
+
klass.artists_with_weight.should == [
|
325
|
+
['aaron', 2],
|
326
|
+
['andy', 1],
|
327
|
+
['gory', 1],
|
328
|
+
['grant', 1],
|
329
|
+
['greg', 2],
|
330
|
+
['jeff', 1],
|
331
|
+
['mandy', 2]
|
332
|
+
]
|
333
|
+
end
|
334
|
+
end
|
335
|
+
end
|
336
|
+
end
|
337
|
+
|
338
|
+
context "map-reduce aggregation" do
|
339
|
+
let(:klass) { M1 }
|
340
|
+
it_should_behave_like "aggregation"
|
341
|
+
|
342
|
+
it "should generate the tags aggregation collection name correctly" do
|
343
|
+
klass.aggregation_collection_for(:tags).should == "m1s_tags_aggregation"
|
344
|
+
end
|
345
|
+
|
346
|
+
it "should generate the artists aggregation collection name correctly" do
|
347
|
+
klass.aggregation_collection_for(:artists).should == "m1s_artists_aggregation"
|
348
|
+
end
|
349
|
+
end
|
350
|
+
|
351
|
+
context "realtime aggregation" do
|
352
|
+
let(:klass) { M2 }
|
353
|
+
it_should_behave_like "aggregation"
|
354
|
+
|
355
|
+
it "should generate the tags aggregation collection name correctly" do
|
356
|
+
klass.aggregation_collection_for(:tags).should == "m2s_tags_aggregation"
|
357
|
+
end
|
358
|
+
|
359
|
+
it "should generate the artists aggregation collection name correctly" do
|
360
|
+
klass.aggregation_collection_for(:artists).should == "m2s_artists_aggregation"
|
361
|
+
end
|
362
|
+
end
|
363
|
+
|
364
|
+
context "realtime aggregation group by" do
|
365
|
+
let(:klass) { M3 }
|
366
|
+
it_should_behave_like "aggregation"
|
367
|
+
|
368
|
+
it "should have artists_group_by_field value :user" do
|
369
|
+
klass.artists_group_by_field.should == :user
|
370
|
+
end
|
371
|
+
|
372
|
+
it "should generate the tags aggregation collection name correctly" do
|
373
|
+
klass.aggregation_collection_for(:tags).should == "m3s_tags_aggregation"
|
374
|
+
end
|
375
|
+
|
376
|
+
it "should generate the artists aggregation collection name correctly" do
|
377
|
+
klass.aggregation_collection_for(:artists).should == "m3s_artists_aggregation"
|
378
|
+
end
|
379
|
+
|
380
|
+
context "for groupings" do
|
381
|
+
before :each do
|
382
|
+
klass.create!(user: "user1", tags: "food ant bee", artists: "jeff greg mandy aaron andy")
|
383
|
+
klass.create!(user: "user1", tags: "juice food bee zip", artists: "grant andrew andy")
|
384
|
+
klass.create!(user: "user2", tags: "honey strip food", artists: "mandy aaron andy")
|
385
|
+
end
|
386
|
+
|
387
|
+
it "should retrieve the list of all saved tags distinct and ordered" do
|
388
|
+
klass.tags("user1").should == %w[ant bee food juice zip]
|
389
|
+
klass.tags("user2").should == %w[food honey strip]
|
390
|
+
|
391
|
+
klass.artists("user1").should == %w[aaron andrew andy grant greg jeff mandy]
|
392
|
+
klass.artists("user2").should == %w[aaron andy mandy]
|
393
|
+
end
|
394
|
+
|
395
|
+
it "should retrieve a list of tags with weight" do
|
396
|
+
klass.tags_with_weight("user1").should == [
|
397
|
+
['ant', 1],
|
398
|
+
['bee', 2],
|
399
|
+
['food', 2],
|
400
|
+
['juice', 1],
|
401
|
+
['zip', 1]
|
402
|
+
]
|
403
|
+
|
404
|
+
klass.tags_with_weight("user2").should == [
|
405
|
+
['food', 1],
|
406
|
+
['honey', 1],
|
407
|
+
['strip', 1]
|
408
|
+
]
|
409
|
+
|
410
|
+
klass.artists_with_weight("user1").should == [
|
411
|
+
['aaron', 1],
|
412
|
+
['andrew', 1],
|
413
|
+
['andy', 2],
|
414
|
+
['grant', 1],
|
415
|
+
['greg', 1],
|
416
|
+
['jeff', 1],
|
417
|
+
['mandy', 1]
|
418
|
+
]
|
419
|
+
|
420
|
+
klass.artists_with_weight("user2").should == [
|
421
|
+
['aaron', 1],
|
422
|
+
['andy', 1],
|
423
|
+
['mandy', 1]
|
424
|
+
]
|
425
|
+
end
|
426
|
+
end
|
427
|
+
end
|
428
|
+
|
429
|
+
context "removed options" do
|
430
|
+
it "should throw error if :field option is specified" do
|
431
|
+
expect do
|
432
|
+
class Invalid
|
433
|
+
include Mongoid::Document
|
434
|
+
include Mongoid::TaggableWithContext
|
435
|
+
taggable field: :foobar
|
436
|
+
end
|
437
|
+
end.to raise_error
|
438
|
+
end
|
439
|
+
it "should throw error if :string_method option is specified" do
|
440
|
+
expect do
|
441
|
+
class Invalid
|
442
|
+
include Mongoid::Document
|
443
|
+
include Mongoid::TaggableWithContext
|
444
|
+
taggable string_method: :foobar
|
445
|
+
end
|
446
|
+
end.to raise_error
|
447
|
+
end
|
448
|
+
it "should throw error if GroupBy::TaggableWithContext module is included" do
|
449
|
+
expect do
|
450
|
+
class Invalid
|
451
|
+
include Mongoid::Document
|
452
|
+
include Mongoid::TaggableWithContext::GroupBy::TaggableWithContext
|
453
|
+
end
|
454
|
+
end.to raise_error
|
455
|
+
end
|
456
|
+
it "should throw error if GroupBy::AggregationStrategy::RealTime module is included" do
|
457
|
+
expect do
|
458
|
+
class Invalid
|
459
|
+
include Mongoid::Document
|
460
|
+
include Mongoid::TaggableWithContext::GroupBy::AggregationStrategy::RealTime
|
461
|
+
end
|
462
|
+
end.to raise_error
|
463
|
+
end
|
464
|
+
end
|
465
|
+
end
|