mongoid-tags-arent-hard 1.0.6 → 1.0.7
Sign up to get free protection for your applications and to get access to all the features.
data/README.md
CHANGED
@@ -20,9 +20,9 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
-
To add tags to a model you need to first include the
|
23
|
+
To add tags to a model you need to first include the `Mongoid::TagsArentHard` module and then define what you want the field to be called using the `taggable_with` method.
|
24
24
|
|
25
|
-
|
25
|
+
```ruby
|
26
26
|
class Foo
|
27
27
|
include Mongoid::Document
|
28
28
|
include Mongoid::TagsArentHard
|
@@ -30,13 +30,13 @@ class Foo
|
|
30
30
|
taggable_with :tags
|
31
31
|
taggable_with :colors, separator: ";"
|
32
32
|
end
|
33
|
-
|
33
|
+
```
|
34
34
|
|
35
|
-
Now we have two different types of "tags"; the first being called
|
35
|
+
Now we have two different types of "tags"; the first being called `tags` and the second being called `colors`. We have also told the `colors` to use `";"` as its separator.
|
36
36
|
|
37
37
|
Now we can do fun things like this:
|
38
38
|
|
39
|
-
|
39
|
+
```ruby
|
40
40
|
# set with either a string or an array:
|
41
41
|
foo = Foo.new(tags: "a,b,c", colors: ["red", "blue"])
|
42
42
|
|
@@ -45,9 +45,9 @@ foo.tags #=> ["a", "b", "c"]
|
|
45
45
|
foo.colors #=> ["red", "blue"]
|
46
46
|
|
47
47
|
# append with either a string or an array:
|
48
|
-
foo.tags
|
48
|
+
foo.tags << "d,e"
|
49
49
|
foo.tags #=> ["a", "b", "c", "d", "e"]
|
50
|
-
foo.colors
|
50
|
+
foo.colors << ["green", "yellow"]
|
51
51
|
foo.colors #=> ["red", "blue", "green", "yellow"]
|
52
52
|
|
53
53
|
# set with either a string or an array:
|
@@ -55,13 +55,13 @@ foo.tags = ["x", "y", "z"]
|
|
55
55
|
foo.tags #=> ["x", "y", "z"]
|
56
56
|
foo.colors = "black;brown"
|
57
57
|
foo.colors #=> ["black", "brown"]
|
58
|
-
|
58
|
+
```
|
59
59
|
|
60
60
|
### Searching
|
61
61
|
|
62
62
|
There are a few scopes included that make it easy to find objects that have the tags you are looking for. These methods are generated using the name of the field you designed, so in our previous example we would have the following methods available to us:
|
63
63
|
|
64
|
-
|
64
|
+
```ruby
|
65
65
|
# Find objects with any of the values:
|
66
66
|
Foo.with_any_tags("a")
|
67
67
|
Foo.with_any_tags(["a", "b"])
|
@@ -77,7 +77,11 @@ Foo.with_all_tags("a, b")
|
|
77
77
|
Foo.with_all_colors("a")
|
78
78
|
Foo.with_all_colors(["a", "b"])
|
79
79
|
Foo.with_all_colors("a, b")
|
80
|
-
|
80
|
+
|
81
|
+
# Retrieve a distinct array of all tags
|
82
|
+
Foo.all_tags
|
83
|
+
Foo.where(name: 'test').all_tags
|
84
|
+
```
|
81
85
|
|
82
86
|
Again, notice that you can use either a string, an array, or a splatted list as values to these scopes.
|
83
87
|
|
@@ -89,3 +93,8 @@ Again, notice that you can use either a string, an array, or a splatted list as
|
|
89
93
|
4. Commit your changes (`git commit -am 'Add some feature'`)
|
90
94
|
5. Push to the branch (`git push origin my-new-feature`)
|
91
95
|
6. Create new Pull Request
|
96
|
+
|
97
|
+
## Contributers
|
98
|
+
|
99
|
+
* Mark Bates
|
100
|
+
* Carsten Block
|
@@ -33,6 +33,11 @@ module Mongoid
|
|
33
33
|
self.send("with_any_#{name}", *val)
|
34
34
|
end
|
35
35
|
|
36
|
+
self.class.send(:define_method, "all_#{name}") do
|
37
|
+
queryable.distinct(name.to_s)
|
38
|
+
end
|
39
|
+
|
40
|
+
|
36
41
|
self.class.send(:define_method, "with_any_#{name}") do |*val|
|
37
42
|
any_in(name => Mongoid::TagsArentHard::Tags.new(*val, {}).tag_list)
|
38
43
|
end
|
@@ -4,6 +4,8 @@ class Foo
|
|
4
4
|
include Mongoid::Document
|
5
5
|
include Mongoid::TagsArentHard
|
6
6
|
|
7
|
+
field :label
|
8
|
+
|
7
9
|
taggable_with :tags
|
8
10
|
taggable_with :colors, separator: ";"
|
9
11
|
end
|
@@ -94,11 +96,33 @@ describe Mongoid::TagsArentHard do
|
|
94
96
|
context 'class scopes' do
|
95
97
|
|
96
98
|
before(:each) do
|
97
|
-
@foo1 = Foo.create!(_name => "a#{_separator}b#{_separator}c")
|
99
|
+
@foo1 = Foo.create!(_name => "a#{_separator}b#{_separator}c", :label => 'test')
|
98
100
|
@foo2 = Foo.create!(_name => "b#{_separator}c#{_separator}f")
|
99
101
|
@foo3 = Foo.create!(_name => "d#{_separator}e#{_separator}f")
|
100
102
|
end
|
101
|
-
|
103
|
+
|
104
|
+
describe "all_#{_name}" do
|
105
|
+
it "returns all unique tag names as an array" do
|
106
|
+
results = Foo.send("all_#{_name}")
|
107
|
+
results.length.should be(6)
|
108
|
+
results.should include 'a'
|
109
|
+
results.should include 'b'
|
110
|
+
results.should include 'c'
|
111
|
+
results.should include 'd'
|
112
|
+
results.should include 'e'
|
113
|
+
results.should include 'f'
|
114
|
+
end
|
115
|
+
|
116
|
+
it "returns all unique tag names within the given scope" do
|
117
|
+
results = Foo.where(label: 'test').send("all_#{_name}")
|
118
|
+
results.length.should be(3)
|
119
|
+
results.should include 'a'
|
120
|
+
results.should include 'b'
|
121
|
+
results.should include 'c'
|
122
|
+
end
|
123
|
+
|
124
|
+
end
|
125
|
+
|
102
126
|
describe "with_#{_name}" do
|
103
127
|
|
104
128
|
it "returns all models with a specific #{_name} (splatted)" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid-tags-arent-hard
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.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-
|
12
|
+
date: 2012-10-02 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: mongoid
|