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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8a1821ff8bc469e1985a7e6665d7b1f638496c39
4
- data.tar.gz: 04372ef127103e5776a70fdf1be68eaee568c9bb
3
+ metadata.gz: 080185fe1e3f5faf0542afb12518cc740f669098
4
+ data.tar.gz: f7647f52be3c7c26f2697e14e1bd35e53cdb1014
5
5
  SHA512:
6
- metadata.gz: f3525e2849380326dbe6dfe65d1537af4457090b8b4041bd9fa2b6d9c3470cf0119bd4850a1644a479739e1da161949d3a802dea91616d151016d05f85ef9978
7
- data.tar.gz: 6eec97ff89f380542bfb6c09cc733506df1b1dfed65f5ced049d722d5f8261d707d2a8747d99977206d92b94e6e478e86e043ff663af0c69cff329d2070cec44
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.uniq.pluck("unnest(#{tag_name})")
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
- from(select("unnest(#{tag_name}) as tag")).group('tag').order('tag').pluck('tag, count(*) as count')
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
@@ -1,3 +1,3 @@
1
1
  module ActsAsTagPgarray
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  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
@@ -36,6 +36,7 @@ end
36
36
  def create_database
37
37
  ActiveRecord::Schema.define(:version => 1) do
38
38
  create_table :users do |t|
39
+ t.string :name
39
40
  t.text :colors, array: true, defualt: '{}'
40
41
  t.timestamps
41
42
  end
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.1.1
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-02 00:00:00.000000000 Z
11
+ date: 2014-05-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord