mongoid_taggable 0.1.4 → 0.1.5
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/README.textile +13 -6
- data/Rakefile +1 -1
- data/lib/mongoid/taggable.rb +11 -8
- data/mongoid_taggable.gemspec +3 -3
- data/spec/mongoid/taggable_spec.rb +9 -1
- metadata +3 -5
data/README.textile
CHANGED
@@ -18,17 +18,17 @@ bc. script/plugin install git://github.com/wilkerlucio/mongoid_taggable.git
|
|
18
18
|
|
19
19
|
h2. Basic Usage
|
20
20
|
|
21
|
-
To make a document taggable you
|
21
|
+
To make a document taggable you need to include @Mongoid::Taggable@ into your document:
|
22
22
|
|
23
23
|
bc.. class Post
|
24
24
|
include Mongoid::Document
|
25
25
|
include Mongoid::Taggable
|
26
|
-
|
26
|
+
|
27
27
|
field :title
|
28
28
|
field :content
|
29
29
|
end
|
30
30
|
|
31
|
-
p.
|
31
|
+
p. And in your form:
|
32
32
|
|
33
33
|
bc.. <% form_for @post do |f| %>
|
34
34
|
<p>
|
@@ -48,13 +48,20 @@ bc.. <% form_for @post do |f| %>
|
|
48
48
|
</p>
|
49
49
|
<% end %>
|
50
50
|
|
51
|
-
p. In this case, the text fields for tags should receive the list of tags separated by comma (below
|
51
|
+
p. In this case, the text fields for tags should receive the list of tags separated by comma (See below for how to change the separator).
|
52
|
+
|
53
|
+
p. Then your document will have the @tags@ and @tags_array@ getter and setter. @tags@ is as a plain string with tags separated by comma, and @tags_array@ an array representation.
|
54
|
+
|
55
|
+
h2. Finding Objects by Tag
|
56
|
+
|
57
|
+
p. Tagged models get a scope called @tagged_with@:
|
52
58
|
|
53
|
-
|
59
|
+
bc.. MyModel.tagged_with('foo')
|
60
|
+
MyModel.published.tagged_with('foo').count
|
54
61
|
|
55
62
|
h2. Tags Indexing
|
56
63
|
|
57
|
-
This
|
64
|
+
This module will automatically create an index of tags and their counts for you after saving the document. This can be used for a tag cloud. See the following example to understand how to use it:
|
58
65
|
|
59
66
|
bc.. Post.create!(:tags => "food,ant,bee")
|
60
67
|
Post.create!(:tags => "juice,food,bee,zip")
|
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.5"
|
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
@@ -17,30 +17,33 @@ module Mongoid::Taggable
|
|
17
17
|
# create fields for tags and index it
|
18
18
|
base.field :tags_array, :type => Array
|
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
23
|
document.class.save_tags_index!
|
24
24
|
end
|
25
|
-
|
25
|
+
|
26
26
|
# extend model
|
27
27
|
base.extend ClassMethods
|
28
28
|
base.send :include, InstanceMethods
|
29
|
-
|
29
|
+
|
30
30
|
# enable indexing as default
|
31
31
|
base.enable_tags_index!
|
32
32
|
end
|
33
|
-
|
33
|
+
|
34
34
|
module ClassMethods
|
35
|
-
#
|
36
|
-
|
37
|
-
|
35
|
+
# returns an array of distinct ordered list of tags defined in all documents
|
36
|
+
|
37
|
+
def tagged_with(tag)
|
38
|
+
self.any_in(:tags_array => [tag])
|
39
|
+
end
|
40
|
+
|
38
41
|
def tags
|
39
42
|
db = Mongoid::Config.master
|
40
43
|
db.collection(tags_index_collection).find.to_a.map{ |r| r["_id"] }
|
41
44
|
end
|
42
45
|
|
43
|
-
# retrieve the list of tags with weight(count), this is
|
46
|
+
# retrieve the list of tags with weight (i.e. count), this is useful for
|
44
47
|
# creating tag clouds
|
45
48
|
def tags_with_weight
|
46
49
|
db = Mongoid::Config.master
|
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.5"
|
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-04-18}
|
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 = [
|
@@ -32,7 +32,7 @@ Gem::Specification.new do |s|
|
|
32
32
|
s.homepage = %q{http://github.com/wilkerlucio/mongoid_taggable}
|
33
33
|
s.rdoc_options = ["--charset=UTF-8"]
|
34
34
|
s.require_paths = ["lib"]
|
35
|
-
s.rubygems_version = %q{1.
|
35
|
+
s.rubygems_version = %q{1.7.2}
|
36
36
|
s.summary = %q{Mongoid taggable behaviour}
|
37
37
|
s.test_files = [
|
38
38
|
"spec/mongoid/taggable_spec.rb",
|
@@ -20,6 +20,14 @@ class MyModel
|
|
20
20
|
end
|
21
21
|
|
22
22
|
describe Mongoid::Taggable do
|
23
|
+
|
24
|
+
context "finding by tag" do
|
25
|
+
it "locates tagged objects" do
|
26
|
+
m = MyModel.create!(:tags => "interesting,stuff")
|
27
|
+
MyModel.tagged_with('interesting').include?(m).should be_true
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
23
31
|
context "saving tags from plain text" do
|
24
32
|
before :each do
|
25
33
|
@m = MyModel.new
|
@@ -75,7 +83,7 @@ describe Mongoid::Taggable do
|
|
75
83
|
MyModel.tags_index_collection.should == "my_models_tags_index"
|
76
84
|
end
|
77
85
|
|
78
|
-
context "
|
86
|
+
context "retrieving index" do
|
79
87
|
before :each do
|
80
88
|
MyModel.create!(:tags => "food,ant,bee")
|
81
89
|
MyModel.create!(:tags => "juice,food,bee,zip")
|
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.5
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Wilker Lucio
|
@@ -11,8 +11,7 @@ autorequire:
|
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
13
|
|
14
|
-
date: 2011-
|
15
|
-
default_executable:
|
14
|
+
date: 2011-04-18 00:00:00 Z
|
16
15
|
dependencies: []
|
17
16
|
|
18
17
|
description: Mongoid Taggable provides some helpers to create taggable documents.
|
@@ -36,7 +35,6 @@ files:
|
|
36
35
|
- rails/init.rb
|
37
36
|
- spec/mongoid/taggable_spec.rb
|
38
37
|
- spec/spec_helper.rb
|
39
|
-
has_rdoc: true
|
40
38
|
homepage: http://github.com/wilkerlucio/mongoid_taggable
|
41
39
|
licenses: []
|
42
40
|
|
@@ -60,7 +58,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
60
58
|
requirements: []
|
61
59
|
|
62
60
|
rubyforge_project:
|
63
|
-
rubygems_version: 1.
|
61
|
+
rubygems_version: 1.7.2
|
64
62
|
signing_key:
|
65
63
|
specification_version: 3
|
66
64
|
summary: Mongoid taggable behaviour
|