mongoid_taggable 0.1.6 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -1
- data/lib/mongoid/taggable.rb +16 -12
- data/mongoid_taggable.gemspec +2 -2
- data/spec/mongoid/taggable_spec.rb +34 -2
- metadata +2 -2
data/Rakefile
CHANGED
@@ -7,7 +7,7 @@ begin
|
|
7
7
|
require 'jeweler'
|
8
8
|
Jeweler::Tasks.new do |gemspec|
|
9
9
|
gemspec.name = "mongoid_taggable"
|
10
|
-
gemspec.version = "0.1.
|
10
|
+
gemspec.version = "0.1.7"
|
11
11
|
gemspec.summary = "Mongoid taggable behaviour"
|
12
12
|
gemspec.description = "Mongoid Taggable provides some helpers to create taggable documents."
|
13
13
|
gemspec.email = "wilkerlucio@gmail.com"
|
data/lib/mongoid/taggable.rb
CHANGED
@@ -15,17 +15,21 @@
|
|
15
15
|
module Mongoid::Taggable
|
16
16
|
def self.included(base)
|
17
17
|
# create fields for tags and index it
|
18
|
-
base.field :tags_array, :type => Array
|
18
|
+
base.field :tags_array, :type => Array, :default => []
|
19
19
|
base.index [['tags_array', Mongo::ASCENDING]]
|
20
20
|
|
21
21
|
# add callback to save tags index
|
22
22
|
base.after_save do |document|
|
23
|
-
document.
|
23
|
+
if document.tags_array_changed
|
24
|
+
document.class.save_tags_index!
|
25
|
+
document.tags_array_changed = false
|
26
|
+
end
|
24
27
|
end
|
25
28
|
|
26
29
|
# extend model
|
27
30
|
base.extend ClassMethods
|
28
31
|
base.send :include, InstanceMethods
|
32
|
+
base.send :attr_accessor, :tags_array_changed
|
29
33
|
|
30
34
|
# enable indexing as default
|
31
35
|
base.enable_tags_index!
|
@@ -41,21 +45,19 @@ module Mongoid::Taggable
|
|
41
45
|
def tagged_with_all(*tags)
|
42
46
|
self.all_in(:tags_array => tags.flatten)
|
43
47
|
end
|
44
|
-
|
48
|
+
|
45
49
|
def tagged_with_any(*tags)
|
46
50
|
self.any_in(:tags_array => tags.flatten)
|
47
51
|
end
|
48
52
|
|
49
53
|
def tags
|
50
|
-
|
51
|
-
db.collection(tags_index_collection).find.to_a.map{ |r| r["_id"] }
|
54
|
+
tags_index_collection.master.find.to_a.map{ |r| r["_id"] }
|
52
55
|
end
|
53
56
|
|
54
57
|
# retrieve the list of tags with weight (i.e. count), this is useful for
|
55
58
|
# creating tag clouds
|
56
59
|
def tags_with_weight
|
57
|
-
|
58
|
-
db.collection(tags_index_collection).find.to_a.map{ |r| [r["_id"], r["value"]] }
|
60
|
+
tags_index_collection.master.find.to_a.map{ |r| [r["_id"], r["value"]] }
|
59
61
|
end
|
60
62
|
|
61
63
|
def disable_tags_index!
|
@@ -71,16 +73,17 @@ module Mongoid::Taggable
|
|
71
73
|
@tags_separator || ','
|
72
74
|
end
|
73
75
|
|
74
|
-
def
|
76
|
+
def tags_index_collection_name
|
75
77
|
"#{collection_name}_tags_index"
|
76
78
|
end
|
77
79
|
|
80
|
+
def tags_index_collection
|
81
|
+
@@tags_index_collection ||= Mongoid::Collection.new(self, tags_index_collection_name)
|
82
|
+
end
|
83
|
+
|
78
84
|
def save_tags_index!
|
79
85
|
return unless @do_tags_index
|
80
86
|
|
81
|
-
db = Mongoid::Config.master
|
82
|
-
coll = db.collection(collection_name)
|
83
|
-
|
84
87
|
map = "function() {
|
85
88
|
if (!this.tags_array) {
|
86
89
|
return;
|
@@ -101,7 +104,7 @@ module Mongoid::Taggable
|
|
101
104
|
return count;
|
102
105
|
}"
|
103
106
|
|
104
|
-
|
107
|
+
self.collection.master.map_reduce(map, reduce, :out => tags_index_collection_name)
|
105
108
|
end
|
106
109
|
end
|
107
110
|
|
@@ -112,6 +115,7 @@ module Mongoid::Taggable
|
|
112
115
|
|
113
116
|
def tags=(tags)
|
114
117
|
self.tags_array = tags.split(self.class.tags_separator).map(&:strip).reject(&:blank?)
|
118
|
+
@tags_array_changed = true
|
115
119
|
end
|
116
120
|
end
|
117
121
|
end
|
data/mongoid_taggable.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{mongoid_taggable}
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.7"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Wilker Lucio", "Kris Kowalik"]
|
12
|
-
s.date = %q{2011-
|
12
|
+
s.date = %q{2011-07-01}
|
13
13
|
s.description = %q{Mongoid Taggable provides some helpers to create taggable documents.}
|
14
14
|
s.email = %q{wilkerlucio@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -17,10 +17,17 @@ require File.join(File.dirname(__FILE__), %w[.. spec_helper])
|
|
17
17
|
class MyModel
|
18
18
|
include Mongoid::Document
|
19
19
|
include Mongoid::Taggable
|
20
|
+
field :name
|
20
21
|
end
|
21
22
|
|
22
23
|
describe Mongoid::Taggable do
|
23
24
|
|
25
|
+
describe "default tags array value" do
|
26
|
+
it 'should be an empty array' do
|
27
|
+
MyModel.new.tags_array.should == []
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
24
31
|
context "finding" do
|
25
32
|
let(:model){MyModel.create!(:tags => "interesting,stuff,good,bad")}
|
26
33
|
context "by tagged_with" do
|
@@ -119,7 +126,15 @@ describe Mongoid::Taggable do
|
|
119
126
|
|
120
127
|
context "indexing tags" do
|
121
128
|
it "should generate the index collection name based on model" do
|
122
|
-
MyModel.
|
129
|
+
MyModel.tags_index_collection_name.should == "my_models_tags_index"
|
130
|
+
end
|
131
|
+
|
132
|
+
it "should generate the index collection model based on model" do
|
133
|
+
MyModel.tags_index_collection.should be_a Mongoid::Collection
|
134
|
+
end
|
135
|
+
|
136
|
+
it "should generate the index collection model based on model with the collection name" do
|
137
|
+
MyModel.tags_index_collection.name.should == "my_models_tags_index"
|
123
138
|
end
|
124
139
|
|
125
140
|
context "retrieving index" do
|
@@ -160,5 +175,22 @@ describe Mongoid::Taggable do
|
|
160
175
|
MyModel.tags.should == []
|
161
176
|
end
|
162
177
|
end
|
178
|
+
|
179
|
+
it 'should launch the map/reduce if index activate and tag_arrays change' do
|
180
|
+
m = MyModel.create!(:tags => "food,ant,bee")
|
181
|
+
m.tags = 'juice,food'
|
182
|
+
MyModel.collection.master.should_receive(:map_reduce)
|
183
|
+
m.save
|
184
|
+
end
|
185
|
+
|
186
|
+
it 'should not launch the map/reduce if index activate and tag_arrays not change' do
|
187
|
+
m = MyModel.create!(:tags => "food,ant,bee")
|
188
|
+
MyModel.collection.master.should_not_receive(:map_reduce)
|
189
|
+
m.save
|
190
|
+
m.name = 'hello'
|
191
|
+
m.save
|
192
|
+
end
|
193
|
+
|
163
194
|
end
|
164
|
-
|
195
|
+
|
196
|
+
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: mongoid_taggable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.1.
|
5
|
+
version: 0.1.7
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Wilker Lucio
|
@@ -11,7 +11,7 @@ autorequire:
|
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
13
|
|
14
|
-
date: 2011-
|
14
|
+
date: 2011-07-01 00:00:00 Z
|
15
15
|
dependencies: []
|
16
16
|
|
17
17
|
description: Mongoid Taggable provides some helpers to create taggable documents.
|