mongoid_taggable 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/README.textile +13 -9
- data/Rakefile +10 -12
- data/lib/mongoid/taggable.rb +14 -14
- data/mongoid_taggable.gemspec +7 -9
- data/spec/mongoid/taggable_spec.rb +18 -18
- data/spec/spec_helper.rb +18 -6
- metadata +6 -11
- data/install.rb +0 -1
- data/tasks/mongoid_taggable_tasks.rake +0 -4
- data/uninstall.rb +0 -1
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
.redcar
|
data/README.textile
CHANGED
@@ -8,6 +8,10 @@ You can simple install from rubygems:
|
|
8
8
|
|
9
9
|
bc. gem install mongoid_taggable
|
10
10
|
|
11
|
+
or in Gemfile:
|
12
|
+
|
13
|
+
bc. gem 'mongoid_taggable'
|
14
|
+
|
11
15
|
or as a Rails Plugin:
|
12
16
|
|
13
17
|
bc. script/plugin install git://github.com/wilkerlucio/mongoid_taggable.git
|
@@ -44,13 +48,13 @@ bc.. <% form_for @post do |f| %>
|
|
44
48
|
</p>
|
45
49
|
<% end %>
|
46
50
|
|
47
|
-
p. In this case, the text fields for tags should receive the list of tags separated by
|
51
|
+
p. In this case, the text fields for tags should receive the list of tags separated by comma (below in this document you will see how to change the separator)
|
48
52
|
|
49
|
-
p.
|
53
|
+
p. Then your document will have the @tags@ and @tags_array@ getter and setter. The @tags@ you use as a plain string with tags separated by comma, and the @tags_array@ is an array with tags. These two properties are automatically synchronized.
|
50
54
|
|
51
55
|
h2. Tags Indexing
|
52
56
|
|
53
|
-
This lib will
|
57
|
+
This lib will automatically create an index of tags and their counts for you after saving the document, useful for getting a list of all tags used in documents of this collection or to create a tag cloud. See the following example to understand how to use it:
|
54
58
|
|
55
59
|
bc.. Post.create!(:tags => "food,ant,bee")
|
56
60
|
Post.create!(:tags => "juice,food,bee,zip")
|
@@ -68,28 +72,28 @@ Post.tags_with_weight # will retrieve:
|
|
68
72
|
# ['zip', 1]
|
69
73
|
# ]
|
70
74
|
|
71
|
-
p. If you don't want to use this feature,
|
75
|
+
p. If you don't want to use this feature, it is good to disable it to improve performance:
|
72
76
|
|
73
77
|
bc.. class Post
|
74
78
|
include Mongoid::Document
|
75
79
|
include Mongoid::Taggable
|
76
|
-
|
80
|
+
|
77
81
|
disable_tags_index! # will disable index creation
|
78
|
-
|
82
|
+
|
79
83
|
field :title
|
80
84
|
field :content
|
81
85
|
end
|
82
86
|
|
83
87
|
h2. Changing default separator
|
84
88
|
|
85
|
-
To change default separator you
|
89
|
+
To change the default separator you may call the @tags_separator@ class method:
|
86
90
|
|
87
91
|
bc.. class Post
|
88
92
|
include Mongoid::Document
|
89
93
|
include Mongoid::Taggable
|
90
|
-
|
94
|
+
|
91
95
|
tags_separator ';' # will change tags separator to ;
|
92
|
-
|
96
|
+
|
93
97
|
field :title
|
94
98
|
field :content
|
95
99
|
end
|
data/Rakefile
CHANGED
@@ -1,32 +1,30 @@
|
|
1
1
|
require 'rake'
|
2
|
-
require 'rake/testtask'
|
3
2
|
require 'rake/rdoctask'
|
3
|
+
require 'rspec'
|
4
|
+
require 'rspec/core/rake_task'
|
4
5
|
|
5
6
|
begin
|
6
7
|
require 'jeweler'
|
7
8
|
Jeweler::Tasks.new do |gemspec|
|
8
9
|
gemspec.name = "mongoid_taggable"
|
9
|
-
gemspec.version = "0.1.
|
10
|
+
gemspec.version = "0.1.2"
|
10
11
|
gemspec.summary = "Mongoid taggable behaviour"
|
11
12
|
gemspec.description = "Mongoid Taggable provides some helpers to create taggable documents."
|
12
13
|
gemspec.email = "wilkerlucio@gmail.com"
|
13
|
-
gemspec.homepage = "http://github.com/
|
14
|
-
gemspec.authors = ["Wilker
|
14
|
+
gemspec.homepage = "http://github.com/wilkerlucio/mongo_taggable"
|
15
|
+
gemspec.authors = ["Wilker Lucio", "Kris Kowalik"]
|
15
16
|
end
|
17
|
+
Jeweler::GemcutterTasks.new
|
16
18
|
rescue LoadError
|
17
19
|
puts "Jeweler not available. Install it with: gem install jeweler"
|
18
20
|
end
|
19
21
|
|
20
22
|
|
21
23
|
desc 'Default: run unit tests.'
|
22
|
-
task :default => :
|
24
|
+
task :default => :spec
|
23
25
|
|
24
|
-
|
25
|
-
|
26
|
-
t.libs << 'lib'
|
27
|
-
t.libs << 'test'
|
28
|
-
t.pattern = 'test/**/*_test.rb'
|
29
|
-
t.verbose = true
|
26
|
+
Rspec::Core::RakeTask.new(:spec) do |spec|
|
27
|
+
spec.pattern = "spec/**/*_spec.rb"
|
30
28
|
end
|
31
29
|
|
32
30
|
desc 'Generate documentation for the mongoid_taggable plugin.'
|
@@ -34,6 +32,6 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
|
|
34
32
|
rdoc.rdoc_dir = 'rdoc'
|
35
33
|
rdoc.title = 'MongoidTaggable'
|
36
34
|
rdoc.options << '--line-numbers' << '--inline-source'
|
37
|
-
rdoc.rdoc_files.include('README')
|
35
|
+
rdoc.rdoc_files.include('README*')
|
38
36
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
39
37
|
end
|
data/lib/mongoid/taggable.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
# Copyright (c) 2010 Wilker Lúcio <wilkerlucio@gmail.com>
|
2
|
-
#
|
2
|
+
#
|
3
3
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
4
|
# you may not use this file except in compliance with the License.
|
5
5
|
# You may obtain a copy of the License at
|
6
|
-
#
|
6
|
+
#
|
7
7
|
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
-
#
|
8
|
+
#
|
9
9
|
# Unless required by applicable law or agreed to in writing, software
|
10
10
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
11
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
@@ -36,38 +36,38 @@ module Mongoid::Taggable
|
|
36
36
|
# an array of distinct ordered list of tags defined in all documents
|
37
37
|
# of this model
|
38
38
|
def tags
|
39
|
-
db = Mongoid::Config.
|
39
|
+
db = Mongoid::Config.master
|
40
40
|
db.collection(tags_index_collection).find.to_a.map{ |r| r["_id"] }
|
41
41
|
end
|
42
|
-
|
42
|
+
|
43
43
|
# retrieve the list of tags with weight(count), this is usefull for
|
44
44
|
# creating tag clouds
|
45
45
|
def tags_with_weight
|
46
|
-
db = Mongoid::Config.
|
46
|
+
db = Mongoid::Config.master
|
47
47
|
db.collection(tags_index_collection).find.to_a.map{ |r| [r["_id"], r["value"]] }
|
48
48
|
end
|
49
|
-
|
49
|
+
|
50
50
|
def disable_tags_index!
|
51
51
|
@do_tags_index = false
|
52
52
|
end
|
53
|
-
|
53
|
+
|
54
54
|
def enable_tags_index!
|
55
55
|
@do_tags_index = true
|
56
56
|
end
|
57
|
-
|
57
|
+
|
58
58
|
def tags_separator(separator = nil)
|
59
59
|
@tags_separator = separator if separator
|
60
60
|
@tags_separator || ','
|
61
61
|
end
|
62
|
-
|
62
|
+
|
63
63
|
def tags_index_collection
|
64
64
|
"#{collection_name}_tags_index"
|
65
65
|
end
|
66
66
|
|
67
67
|
def save_tags_index!
|
68
68
|
return unless @do_tags_index
|
69
|
-
|
70
|
-
db = Mongoid::Config.
|
69
|
+
|
70
|
+
db = Mongoid::Config.master
|
71
71
|
coll = db.collection(collection_name)
|
72
72
|
|
73
73
|
map = "function() {
|
@@ -93,12 +93,12 @@ module Mongoid::Taggable
|
|
93
93
|
coll.map_reduce(map, reduce, :out => tags_index_collection)
|
94
94
|
end
|
95
95
|
end
|
96
|
-
|
96
|
+
|
97
97
|
module InstanceMethods
|
98
98
|
def tags
|
99
99
|
(tags_array || []).join(self.class.tags_separator)
|
100
100
|
end
|
101
|
-
|
101
|
+
|
102
102
|
def tags=(tags)
|
103
103
|
self.tags_array = tags.split(self.class.tags_separator).map(&:strip)
|
104
104
|
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.2"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
-
s.authors = ["Wilker
|
12
|
-
s.date = %q{
|
11
|
+
s.authors = ["Wilker Lucio", "Kris Kowalik"]
|
12
|
+
s.date = %q{2011-01-25}
|
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,21 +17,19 @@ Gem::Specification.new do |s|
|
|
17
17
|
"README.textile"
|
18
18
|
]
|
19
19
|
s.files = [
|
20
|
-
"
|
20
|
+
".gitignore",
|
21
|
+
"LICENSE",
|
21
22
|
"README.textile",
|
22
23
|
"Rakefile",
|
23
24
|
"init.rb",
|
24
|
-
"install.rb",
|
25
25
|
"lib/mongoid/taggable.rb",
|
26
26
|
"lib/mongoid_taggable.rb",
|
27
27
|
"mongoid_taggable.gemspec",
|
28
28
|
"rails/init.rb",
|
29
29
|
"spec/mongoid/taggable_spec.rb",
|
30
|
-
"spec/spec_helper.rb"
|
31
|
-
"tasks/mongoid_taggable_tasks.rake",
|
32
|
-
"uninstall.rb"
|
30
|
+
"spec/spec_helper.rb"
|
33
31
|
]
|
34
|
-
s.homepage = %q{http://github.com/
|
32
|
+
s.homepage = %q{http://github.com/wilkerlucio/mongo_taggable}
|
35
33
|
s.rdoc_options = ["--charset=UTF-8"]
|
36
34
|
s.require_paths = ["lib"]
|
37
35
|
s.rubygems_version = %q{1.3.7}
|
@@ -1,11 +1,11 @@
|
|
1
1
|
# Copyright (c) 2010 Wilker Lúcio <wilkerlucio@gmail.com>
|
2
|
-
#
|
2
|
+
#
|
3
3
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
4
|
# you may not use this file except in compliance with the License.
|
5
5
|
# You may obtain a copy of the License at
|
6
|
-
#
|
6
|
+
#
|
7
7
|
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
-
#
|
8
|
+
#
|
9
9
|
# Unless required by applicable law or agreed to in writing, software
|
10
10
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
11
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
@@ -24,63 +24,63 @@ describe Mongoid::Taggable do
|
|
24
24
|
before :each do
|
25
25
|
@m = MyModel.new
|
26
26
|
end
|
27
|
-
|
27
|
+
|
28
28
|
it "should set tags array from string" do
|
29
29
|
@m.tags = "some,new,tag"
|
30
30
|
@m.tags_array.should == %w[some new tag]
|
31
31
|
end
|
32
|
-
|
32
|
+
|
33
33
|
it "should retrieve tags string from array" do
|
34
34
|
@m.tags_array = %w[some new tags]
|
35
35
|
@m.tags.should == "some,new,tags"
|
36
36
|
end
|
37
|
-
|
37
|
+
|
38
38
|
it "should strip tags before put in array" do
|
39
39
|
@m.tags = "now , with, some spaces , in places "
|
40
40
|
@m.tags_array.should == ["now", "with", "some spaces", "in places"]
|
41
41
|
end
|
42
42
|
end
|
43
|
-
|
43
|
+
|
44
44
|
context "changing separator" do
|
45
45
|
before :all do
|
46
46
|
MyModel.tags_separator ";"
|
47
47
|
end
|
48
|
-
|
48
|
+
|
49
49
|
after :all do
|
50
50
|
MyModel.tags_separator ","
|
51
51
|
end
|
52
|
-
|
52
|
+
|
53
53
|
before :each do
|
54
54
|
@m = MyModel.new
|
55
55
|
end
|
56
|
-
|
56
|
+
|
57
57
|
it "should split with custom separator" do
|
58
58
|
@m.tags = "some;other;separator"
|
59
59
|
@m.tags_array.should == %w[some other separator]
|
60
60
|
end
|
61
|
-
|
61
|
+
|
62
62
|
it "should join with custom separator" do
|
63
63
|
@m.tags_array = %w[some other sep]
|
64
64
|
@m.tags.should == "some;other;sep"
|
65
65
|
end
|
66
66
|
end
|
67
|
-
|
67
|
+
|
68
68
|
context "indexing tags" do
|
69
69
|
it "should generate the index collection name based on model" do
|
70
70
|
MyModel.tags_index_collection.should == "my_models_tags_index"
|
71
71
|
end
|
72
|
-
|
72
|
+
|
73
73
|
context "retriving index" do
|
74
74
|
before :each do
|
75
75
|
MyModel.create!(:tags => "food,ant,bee")
|
76
76
|
MyModel.create!(:tags => "juice,food,bee,zip")
|
77
77
|
MyModel.create!(:tags => "honey,strip,food")
|
78
78
|
end
|
79
|
-
|
79
|
+
|
80
80
|
it "should retrieve the list of all saved tags distinct and ordered" do
|
81
81
|
MyModel.tags.should == %w[ant bee food honey juice strip zip]
|
82
82
|
end
|
83
|
-
|
83
|
+
|
84
84
|
it "should retrieve a list of tags with weight" do
|
85
85
|
MyModel.tags_with_weight.should == [
|
86
86
|
['ant', 1],
|
@@ -93,16 +93,16 @@ describe Mongoid::Taggable do
|
|
93
93
|
]
|
94
94
|
end
|
95
95
|
end
|
96
|
-
|
96
|
+
|
97
97
|
context "avoiding index generation" do
|
98
98
|
before :all do
|
99
99
|
MyModel.disable_tags_index!
|
100
100
|
end
|
101
|
-
|
101
|
+
|
102
102
|
after :all do
|
103
103
|
MyModel.enable_tags_index!
|
104
104
|
end
|
105
|
-
|
105
|
+
|
106
106
|
it "should not generate index" do
|
107
107
|
MyModel.create!(:tags => "sample,tags")
|
108
108
|
MyModel.tags.should == []
|
data/spec/spec_helper.rb
CHANGED
@@ -1,8 +1,20 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
1
|
+
$: << File.expand_path("../../lib", __FILE__)
|
2
|
+
|
3
|
+
require 'database_cleaner'
|
4
|
+
|
5
|
+
RSpec.configure do |config|
|
6
|
+
config.before(:suite) do
|
7
|
+
DatabaseCleaner.strategy = :truncation
|
8
|
+
end
|
9
|
+
|
10
|
+
config.after(:each) do
|
11
|
+
DatabaseCleaner.clean
|
12
|
+
end
|
6
13
|
end
|
7
14
|
|
8
|
-
|
15
|
+
require 'mongoid'
|
16
|
+
require 'mongoid_taggable'
|
17
|
+
|
18
|
+
Mongoid.configure do |config|
|
19
|
+
config.master = Mongo::Connection.new.db("mongoid_taggable_test")
|
20
|
+
end
|
metadata
CHANGED
@@ -1,22 +1,21 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid_taggable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash: 25
|
5
4
|
prerelease: false
|
6
5
|
segments:
|
7
6
|
- 0
|
8
7
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
8
|
+
- 2
|
9
|
+
version: 0.1.2
|
11
10
|
platform: ruby
|
12
11
|
authors:
|
13
|
-
-
|
12
|
+
- Wilker Lucio
|
14
13
|
- Kris Kowalik
|
15
14
|
autorequire:
|
16
15
|
bindir: bin
|
17
16
|
cert_chain: []
|
18
17
|
|
19
|
-
date:
|
18
|
+
date: 2011-01-25 00:00:00 -03:00
|
20
19
|
default_executable:
|
21
20
|
dependencies: []
|
22
21
|
|
@@ -30,21 +29,19 @@ extra_rdoc_files:
|
|
30
29
|
- LICENSE
|
31
30
|
- README.textile
|
32
31
|
files:
|
32
|
+
- .gitignore
|
33
33
|
- LICENSE
|
34
34
|
- README.textile
|
35
35
|
- Rakefile
|
36
36
|
- init.rb
|
37
|
-
- install.rb
|
38
37
|
- lib/mongoid/taggable.rb
|
39
38
|
- lib/mongoid_taggable.rb
|
40
39
|
- mongoid_taggable.gemspec
|
41
40
|
- rails/init.rb
|
42
41
|
- spec/mongoid/taggable_spec.rb
|
43
42
|
- spec/spec_helper.rb
|
44
|
-
- tasks/mongoid_taggable_tasks.rake
|
45
|
-
- uninstall.rb
|
46
43
|
has_rdoc: true
|
47
|
-
homepage: http://github.com/
|
44
|
+
homepage: http://github.com/wilkerlucio/mongo_taggable
|
48
45
|
licenses: []
|
49
46
|
|
50
47
|
post_install_message:
|
@@ -57,7 +54,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
57
54
|
requirements:
|
58
55
|
- - ">="
|
59
56
|
- !ruby/object:Gem::Version
|
60
|
-
hash: 3
|
61
57
|
segments:
|
62
58
|
- 0
|
63
59
|
version: "0"
|
@@ -66,7 +62,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
66
62
|
requirements:
|
67
63
|
- - ">="
|
68
64
|
- !ruby/object:Gem::Version
|
69
|
-
hash: 3
|
70
65
|
segments:
|
71
66
|
- 0
|
72
67
|
version: "0"
|
data/install.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
# Install hook code here
|
data/uninstall.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
# Uninstall hook code here
|