acts-as-taggable-array-on 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +14 -0
- data/lib/acts-as-taggable-array-on/taggable.rb +10 -4
- data/lib/acts-as-taggable-array-on/version.rb +1 -1
- data/spec/acts_as_tag_pgarray/taggable_spec.rb +13 -3
- data/spec/spec_helper.rb +1 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 080185fe1e3f5faf0542afb12518cc740f669098
|
4
|
+
data.tar.gz: f7647f52be3c7c26f2697e14e1bd35e53cdb1014
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1540c2ee1a7b21a74065186193f142b41f7ca080d7e35229b39b2b706e018e8ed4614327b3379f6746933a121c3d2c20382e3f3b1a9f03f943226fc355c427d7
|
7
|
+
data.tar.gz: 26607de532bacdba54128430953e060c03bb282b5cd9c1284c039e78b385624d5c4cd627add5ae46050442f6653036dbae484b7c74fb157c94d0a80c2221f966
|
data/README.md
CHANGED
@@ -116,6 +116,12 @@ User.tags_cloud
|
|
116
116
|
# [['awesome' => 1], ['slick' => 2]]
|
117
117
|
```
|
118
118
|
|
119
|
+
Tag cloud calculation uses subquery internally. To add scopes to the query, use block.
|
120
|
+
|
121
|
+
```ruby
|
122
|
+
User.tags_cloud { where name: ['ken', 'tom'] }
|
123
|
+
```
|
124
|
+
|
119
125
|
### All Tags
|
120
126
|
|
121
127
|
Can get all tags easily.
|
@@ -125,6 +131,14 @@ User.all_tags
|
|
125
131
|
# ['awesome', 'slick']
|
126
132
|
```
|
127
133
|
|
134
|
+
As the same to tag cloud calculation, you can use block to add scopes to the query.
|
135
|
+
|
136
|
+
|
137
|
+
```ruby
|
138
|
+
User.all_tags { where name: ['ken', 'tom'] }
|
139
|
+
```
|
140
|
+
|
141
|
+
|
128
142
|
## Contributing
|
129
143
|
|
130
144
|
1. Fork it ( http://github.com/<my-github-username>/acts-as-taggable-array-on/fork )
|
@@ -14,12 +14,18 @@ module ActsAsTaggableArrayOn
|
|
14
14
|
scope :"without_all_#{tag_name}", ->(* tags){where.not("#{tag_name} @> ARRAY[?]", tags)}
|
15
15
|
|
16
16
|
self.class.class_eval do
|
17
|
-
define_method :"all_#{tag_name}" do
|
18
|
-
all
|
17
|
+
define_method :"all_#{tag_name}" do |options = {}, &block|
|
18
|
+
query_scope = all
|
19
|
+
query_scope = query_scope.merge(instance_eval(&block)) if block
|
20
|
+
|
21
|
+
query_scope.uniq.pluck("unnest(#{table_name}.#{tag_name})")
|
19
22
|
end
|
20
23
|
|
21
|
-
define_method :"#{tag_name}_cloud" do
|
22
|
-
|
24
|
+
define_method :"#{tag_name}_cloud" do |options = {}, &block|
|
25
|
+
query_scope = select("unnest(#{table_name}.#{tag_name}) as tag")
|
26
|
+
query_scope = query_scope.merge(instance_eval(&block)) if block
|
27
|
+
|
28
|
+
from(query_scope).group('tag').order('tag').pluck('tag, count(*)')
|
23
29
|
end
|
24
30
|
end
|
25
31
|
end
|
@@ -2,9 +2,9 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe ActsAsTaggableArrayOn::Taggable do
|
4
4
|
before do
|
5
|
-
@user1 = User.create colors: ['red', 'blue']
|
6
|
-
@user2 = User.create colors: ['black', 'white', 'red']
|
7
|
-
@user3 = User.create colors: ['black', 'blue']
|
5
|
+
@user1 = User.create name: 'Tom', colors: ['red', 'blue']
|
6
|
+
@user2 = User.create name: 'Ken', colors: ['black', 'white', 'red']
|
7
|
+
@user3 = User.create name: 'Joe', colors: ['black', 'blue']
|
8
8
|
|
9
9
|
User.acts_as_taggable_array_on :colors
|
10
10
|
end
|
@@ -52,6 +52,10 @@ describe ActsAsTaggableArrayOn::Taggable do
|
|
52
52
|
it "returns all of tag_name" do
|
53
53
|
expect(User.all_colors).to match_array([@user1,@user2,@user3].map(&:colors).flatten.uniq)
|
54
54
|
end
|
55
|
+
|
56
|
+
it "returns filtered tags for tag_name with block" do
|
57
|
+
expect(User.all_colors{where(name: ["Ken", "Joe"])}).to match_array([@user2,@user3].map(&:colors).flatten.uniq)
|
58
|
+
end
|
55
59
|
end
|
56
60
|
|
57
61
|
describe "#colors_cloud" do
|
@@ -60,6 +64,12 @@ describe ActsAsTaggableArrayOn::Taggable do
|
|
60
64
|
[@user1,@user2,@user3].map(&:colors).flatten.group_by(&:to_s).map{|k,v| [k,v.count]}
|
61
65
|
)
|
62
66
|
end
|
67
|
+
|
68
|
+
it "returns filtered tag cloud for tag_name with block" do
|
69
|
+
expect(User.colors_cloud{where(name: ["Ken", "Joe"])}).to match_array(
|
70
|
+
[@user2,@user3].map(&:colors).flatten.group_by(&:to_s).map{|k,v| [k,v.count]}
|
71
|
+
)
|
72
|
+
end
|
63
73
|
end
|
64
74
|
|
65
75
|
describe "with complex scope" do
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts-as-taggable-array-on
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Takuya Miyamoto
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-05-
|
11
|
+
date: 2014-05-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|