acts-as-taggable-array-on 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 639451573fc58e8ea095163c65dfe35caa3d7992
4
+ data.tar.gz: bc74321efd697736996fbbc64037b94d6c0aa440
5
+ SHA512:
6
+ metadata.gz: 3332d1a58d337ed088ae59668752c8eb5464fcf8fbe7598680e89ef1cadba9a7cea114fe6ce047821f65da3830440a8d09a38ddd10e18e56e6f7631a8f8c3e41
7
+ data.tar.gz: 80a1c4c05c8372a2ea98b86fb641b30d4b657bbc8e77d3febc52039c5969d1f61544e329014ceb17c9078137ad9611c6aaa12cfcbab05e792f5548261bf4e3ae
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,15 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 2.1.1
5
+ addons:
6
+ postgresql: 9.3
7
+ gemfile:
8
+ - gemfiles/rails_4.0.gemfile
9
+ - gemfiles/rails_4.1.gemfile
10
+ before_script:
11
+ - createuser -d acts-as-taggable-array-on -U postgres
12
+ - createdb --username=acts-as-taggable-array-on acts-as-taggable-array-on_test
13
+ script:
14
+ - bundle
15
+ - bundle exec rspec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in acts-as-pgarray-taggable-on.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,5 @@
1
+ guard 'rspec', cmd: 'rspec --drb --color' do
2
+ watch(%r{^spec/.+_spec\.rb})
3
+ watch(%r{^lib/(.+)\.rb}) { |m| "spec/lib/#{m[1]}_spec.rb" }
4
+ watch('spec/spec_helper.rb') { "spec" }
5
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 tmiyamon
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,134 @@
1
+ # ActsAsTaggableArrayOn
2
+ [![Build Status](https://travis-ci.org/tmiyamon/acts-as-taggable-array-on.svg?branch=master)](https://travis-ci.org/tmiyamon/acts-as-taggable-array-on)
3
+
4
+ A simple implementation for tagging sysytem with postgres array.
5
+
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'acts-as-taggable-array-on'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ ```shell
18
+ bundle
19
+ ```
20
+
21
+
22
+ ## Setup
23
+
24
+ To use it, you need to have an array column to act as taggable.
25
+
26
+ ```ruby
27
+ class CreateUser < ActiveRecord::Migration
28
+ def change
29
+ create_table :users do |t|
30
+ t.text :tags, array: true, default: '{}'
31
+ t.timestamps
32
+ end
33
+ add_index :users, :tags, using: 'gin'
34
+ end
35
+ end
36
+ ```
37
+
38
+ and bundle:
39
+
40
+ ```shell
41
+ rake db:migrate
42
+ ```
43
+
44
+ then
45
+
46
+ ```ruby
47
+ class User < ActiveRecord::Base
48
+ acts_as_taggable_array_on :tags
49
+ end
50
+ @user = User.new(:name => "Bobby")
51
+ ```
52
+
53
+ acts_as_taggable_array_on defines 4 scope and 2 class methods as below.
54
+
55
+ ### scopes
56
+
57
+ - with_any_#{tag_name}
58
+ - with_all_#{tag_name}
59
+ - without_any_#{tag_name}
60
+ - without_all_#{tag_name}
61
+
62
+ ### class methods
63
+
64
+ - all_#{tag_name}
65
+ - #{tag_name}_cloud
66
+
67
+
68
+ ## Usage
69
+
70
+ Set, add and remove
71
+
72
+ ```ruby
73
+ #set
74
+ @user.tags = ["awesome", "slick"]
75
+ @user.tags = '{awesome,slick}'
76
+
77
+ #add
78
+ @user.tags += ["awesome"]
79
+ @user.tags += ["awesome", "slick"]
80
+
81
+ #remove
82
+ @user.tags -= ["awesome"]
83
+ @user.tags -= ["awesome", "slick"]
84
+ ```
85
+
86
+ ### Finding Tagged Objects
87
+
88
+ ```ruby
89
+ class User < ActiveRecord::Base
90
+ acts_as_taggable_array_on :tags
91
+ scope :by_join_date, ->{order("created_at DESC")}
92
+ end
93
+
94
+ # Find a user with all of the tags
95
+ User.with_all_tags("awesome")
96
+
97
+ # Find a user with any of the tags
98
+ User.with_any_tags("awesome")
99
+
100
+ # Find a user without all of the tags
101
+ User.without_all_tags("awesome")
102
+
103
+ # Find a user without any of the tags
104
+ User.without_any_tags("awesome")
105
+
106
+ # Chain with the other scopes
107
+ User.with_any_tags("awesome").without_any_tags("slick").by_join_date.paginate(:page => params[:page], :per_page => 20)
108
+ ```
109
+
110
+ ### Tag cloud calculations
111
+
112
+ Calculation to count for each tags is supported. Currently, it does not care its order.
113
+
114
+ ```ruby
115
+ User.tags_cloud
116
+ # [['awesome' => 1], ['slick' => 2]]
117
+ ```
118
+
119
+ ### All Tags
120
+
121
+ Can get all tags easily.
122
+
123
+ ```ruby
124
+ User.all_tags
125
+ # ['awesome', 'slick']
126
+ ```
127
+
128
+ ## Contributing
129
+
130
+ 1. Fork it ( http://github.com/<my-github-username>/acts-as-taggable-array-on/fork )
131
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
132
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
133
+ 4. Push to the branch (`git push origin my-new-feature`)
134
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'acts-as-taggable-array-on/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "acts-as-taggable-array-on"
8
+ spec.version = ActsAsTagPgarray::VERSION
9
+ spec.authors = ["Takuya Miyamoto"]
10
+ spec.email = ["miyamototakuya@gmail.com"]
11
+ spec.summary = %q{Simple tagging gem for Rails using postgres array.}
12
+ spec.description = %q{Simple tagging gem for Rails using postgres array.}
13
+ spec.homepage = "https://github.com/tmiyamon/acts-as-taggable-array-on"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency 'activerecord', ['>= 4', '< 5']
22
+ spec.add_runtime_dependency 'activesupport', ['>= 4', '< 5']
23
+
24
+ spec.add_development_dependency 'pg'
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.5"
27
+ spec.add_development_dependency "rake"
28
+ spec.add_development_dependency "rspec-rails"
29
+ spec.add_development_dependency "guard-rspec"
30
+ end
@@ -0,0 +1,28 @@
1
+ module ActsAsTaggableArrayOn
2
+ module Taggable
3
+ def self.included(base)
4
+ base.extend(ClassMethod)
5
+ end
6
+
7
+ module ClassMethod
8
+ def acts_as_taggable_array_on(*tag_def)
9
+ tag_name = tag_def.first
10
+
11
+ scope :"with_any_#{tag_name}", ->(* tags){where("#{tag_name} && ARRAY[?]", tags)}
12
+ scope :"with_all_#{tag_name}", ->(* tags){where("#{tag_name} @> ARRAY[?]", tags)}
13
+ scope :"without_any_#{tag_name}", ->(* tags){where.not("#{tag_name} && ARRAY[?]", tags)}
14
+ scope :"without_all_#{tag_name}", ->(* tags){where.not("#{tag_name} @> ARRAY[?]", tags)}
15
+
16
+ self.class.class_eval do
17
+ define_method :"all_#{tag_name}" do
18
+ all.uniq.pluck("unnest(#{tag_name})")
19
+ end
20
+
21
+ define_method :"#{tag_name}_cloud" do
22
+ from(select("unnest(#{tag_name}) as tag")).group('tag').order('tag').pluck('tag, count(*) as count')
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,3 @@
1
+ module ActsAsTagPgarray
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,8 @@
1
+ require 'active_record'
2
+
3
+ require "acts-as-taggable-array-on/version"
4
+ require "acts-as-taggable-array-on/taggable"
5
+
6
+ ActiveSupport.on_load(:active_record) do
7
+ include ActsAsTaggableArrayOn::Taggable
8
+ end
@@ -0,0 +1,70 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActsAsTaggableArrayOn::Taggable do
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']
8
+
9
+ User.acts_as_taggable_array_on :colors
10
+ end
11
+
12
+ describe "#acts_as_taggable_array_on" do
13
+ it "defines named scope to match any tags" do
14
+ expect(User).to respond_to(:with_any_colors)
15
+ end
16
+ it "defines named scope to match all tags" do
17
+ expect(User).to respond_to(:with_all_colors)
18
+ end
19
+ it "defines named scope not to match any tags" do
20
+ expect(User).to respond_to(:without_any_colors)
21
+ end
22
+ it "defines named scope not to match all tags" do
23
+ expect(User).to respond_to(:without_all_colors)
24
+ end
25
+ end
26
+
27
+ describe "#with_any_tags" do
28
+ it "returns users having any tags of args" do
29
+ expect(User.with_any_colors('red', 'blue')).to match_array([@user1,@user2,@user3])
30
+ end
31
+ end
32
+
33
+ describe "#with_all_tags" do
34
+ it "returns users having all tags of args" do
35
+ expect(User.with_all_colors('red', 'blue')).to match_array([@user1])
36
+ end
37
+ end
38
+
39
+ describe "#without_any_tags" do
40
+ it "returns users having any tags of args" do
41
+ expect(User.without_any_colors('red', 'blue')).to match_array([])
42
+ end
43
+ end
44
+
45
+ describe "#without_all_tags" do
46
+ it "returns users having all tags of args" do
47
+ expect(User.without_all_colors('red', 'blue')).to match_array([@user2,@user3])
48
+ end
49
+ end
50
+
51
+ describe "#all_colors" do
52
+ it "returns all of tag_name" do
53
+ expect(User.all_colors).to match_array([@user1,@user2,@user3].map(&:colors).flatten.uniq)
54
+ end
55
+ end
56
+
57
+ describe "#colors_cloud" do
58
+ it "returns tag cloud for tag_name" do
59
+ expect(User.colors_cloud).to match_array(
60
+ [@user1,@user2,@user3].map(&:colors).flatten.group_by(&:to_s).map{|k,v| [k,v.count]}
61
+ )
62
+ end
63
+ end
64
+
65
+ describe "with complex scope" do
66
+ it "works properly" do
67
+ expect(User.without_any_colors('white').with_any_colors('blue').order(:created_at).limit(10)).to eq [@user1, @user3]
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,49 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'active_record/railtie'
5
+ ActiveRecord::Base.logger = Logger.new(STDERR)
6
+ ActiveRecord::Base.logger.level = 3
7
+
8
+ require 'acts_as_taggable_array_on'
9
+
10
+ #Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
11
+
12
+ ActiveRecord::Migration.verbose = false
13
+
14
+ class User < ActiveRecord::Base; end
15
+
16
+ RSpec.configure do |config|
17
+ config.before(:all) do
18
+ ActiveRecord::Base.establish_connection(
19
+ adapter: "postgresql",
20
+ encoding: 'unicode',
21
+ database: "acts-as-taggable-array-on_test",
22
+ username: "acts-as-taggable-array-on"
23
+ )
24
+ create_database
25
+ end
26
+
27
+ config.after(:all) do
28
+ drop_database
29
+ end
30
+
31
+ config.after(:each) do
32
+ User.delete_all
33
+ end
34
+ end
35
+
36
+ def create_database
37
+ ActiveRecord::Schema.define(:version => 1) do
38
+ create_table :users do |t|
39
+ t.text :colors, array: true, defualt: '{}'
40
+ t.timestamps
41
+ end
42
+ end
43
+ end
44
+
45
+ def drop_database
46
+ ActiveRecord::Base.connection.tables.each do |table|
47
+ ActiveRecord::Base.connection.drop_table(table)
48
+ end
49
+ end
data/spec_helper.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+
3
+ RSpec::Matchers.define :my_matcher do |expected|
4
+ match do |actual|
5
+ true
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,170 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts-as-taggable-array-on
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Takuya Miyamoto
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '4'
20
+ - - <
21
+ - !ruby/object:Gem::Version
22
+ version: '5'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '4'
30
+ - - <
31
+ - !ruby/object:Gem::Version
32
+ version: '5'
33
+ - !ruby/object:Gem::Dependency
34
+ name: activesupport
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '4'
40
+ - - <
41
+ - !ruby/object:Gem::Version
42
+ version: '5'
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '4'
50
+ - - <
51
+ - !ruby/object:Gem::Version
52
+ version: '5'
53
+ - !ruby/object:Gem::Dependency
54
+ name: pg
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ type: :development
61
+ prerelease: false
62
+ version_requirements: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ - !ruby/object:Gem::Dependency
68
+ name: bundler
69
+ requirement: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ~>
72
+ - !ruby/object:Gem::Version
73
+ version: '1.5'
74
+ type: :development
75
+ prerelease: false
76
+ version_requirements: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ~>
79
+ - !ruby/object:Gem::Version
80
+ version: '1.5'
81
+ - !ruby/object:Gem::Dependency
82
+ name: rake
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ - !ruby/object:Gem::Dependency
96
+ name: rspec-rails
97
+ requirement: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ - !ruby/object:Gem::Dependency
110
+ name: guard-rspec
111
+ requirement: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - '>='
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ type: :development
117
+ prerelease: false
118
+ version_requirements: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ description: Simple tagging gem for Rails using postgres array.
124
+ email:
125
+ - miyamototakuya@gmail.com
126
+ executables: []
127
+ extensions: []
128
+ extra_rdoc_files: []
129
+ files:
130
+ - .gitignore
131
+ - .travis.yml
132
+ - Gemfile
133
+ - Guardfile
134
+ - LICENSE.txt
135
+ - README.md
136
+ - Rakefile
137
+ - acts-as-taggable-array-on.gemspec
138
+ - lib/acts-as-taggable-array-on/taggable.rb
139
+ - lib/acts-as-taggable-array-on/version.rb
140
+ - lib/acts_as_taggable_array_on.rb
141
+ - spec/acts_as_tag_pgarray/taggable_spec.rb
142
+ - spec/spec_helper.rb
143
+ - spec_helper.rb
144
+ homepage: https://github.com/tmiyamon/acts-as-taggable-array-on
145
+ licenses:
146
+ - MIT
147
+ metadata: {}
148
+ post_install_message:
149
+ rdoc_options: []
150
+ require_paths:
151
+ - lib
152
+ required_ruby_version: !ruby/object:Gem::Requirement
153
+ requirements:
154
+ - - '>='
155
+ - !ruby/object:Gem::Version
156
+ version: '0'
157
+ required_rubygems_version: !ruby/object:Gem::Requirement
158
+ requirements:
159
+ - - '>='
160
+ - !ruby/object:Gem::Version
161
+ version: '0'
162
+ requirements: []
163
+ rubyforge_project:
164
+ rubygems_version: 2.2.2
165
+ signing_key:
166
+ specification_version: 4
167
+ summary: Simple tagging gem for Rails using postgres array.
168
+ test_files:
169
+ - spec/acts_as_tag_pgarray/taggable_spec.rb
170
+ - spec/spec_helper.rb