attr_array 1.0.0.0
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.
- checksums.yaml +7 -0
- data/CHANGELOG.md +9 -0
- data/README.md +99 -0
- data/lib/attr_array.rb +3 -0
- data/lib/attr_array/active_record.rb +1 -0
- data/lib/attr_array/attr_array.rb +47 -0
- data/lib/attr_array/version.rb +16 -0
- metadata +79 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 462ed282fbdbb842057a6f474bcf396a48e9786f
|
4
|
+
data.tar.gz: 8004ca3ce52a3b345734a05dd05ab008210c4a4a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ea070b1c64a61317c9d302d0e36135e1c27160e269aff6df5187117dae87454fcfd328f8abb2124fab5bb36c911bba5b3cbd43e4837e25a9040e4e3a437f21e1
|
7
|
+
data.tar.gz: 44920a292d02a2b760e93b305a35fbf17f8b7a49c327ebc85f3b660edaaf865929255357b31c5285382539c69dc769c51e3d2fc1a1012680dc065512c7c411e5
|
data/CHANGELOG.md
ADDED
data/README.md
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
# AttrArray
|
2
|
+
|
3
|
+
A high performance ActiveRecord concern for Rails using the PostgreSQL array type.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
To install add the line to your Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'attr_array', git: 'https://github.com/brightcommerce/attr_array'
|
11
|
+
```
|
12
|
+
|
13
|
+
And `bundle install`.
|
14
|
+
|
15
|
+
## Dependencies
|
16
|
+
|
17
|
+
AttrArray is developed as a ActiveRecord model concern, therefore it is dependent upon ActiveSupport. It is also developed only for use with PostgreSQL. It may work with other databases, but I haven't tried them.
|
18
|
+
|
19
|
+
## How To Use
|
20
|
+
|
21
|
+
To add AttrArray to a model, include the concern:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
class Post < ActiveRecord::Base
|
25
|
+
include AttrArray
|
26
|
+
|
27
|
+
attr_array :tags
|
28
|
+
end
|
29
|
+
```
|
30
|
+
|
31
|
+
To autoload AttrArray for all models, add the following to an initializer:
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
require 'attr_array/active_record'
|
35
|
+
```
|
36
|
+
|
37
|
+
You then don't need to `include AttrArray` in any model.
|
38
|
+
|
39
|
+
### Scopes
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
with_any_#{tag_name}
|
43
|
+
with_all_#{tag_name}
|
44
|
+
without_any_#{tag_name}
|
45
|
+
without_all_#{tag_name}
|
46
|
+
```
|
47
|
+
|
48
|
+
### Class methods
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
all_#{tag_name}
|
52
|
+
{tag_name}_cloud
|
53
|
+
```
|
54
|
+
|
55
|
+
### Setup
|
56
|
+
|
57
|
+
Add the model attributes you want to use with AttrArray in your migration:
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
class CreatePost < ActiveRecord::Migration[5.1]
|
61
|
+
def change
|
62
|
+
create_table :posts do |table|
|
63
|
+
table.column :tags, :string, array: true, default: [], index: {using: 'gin'}
|
64
|
+
table.column :authors, :string, array: true, default: [], index: {using: 'gin'}
|
65
|
+
table.timestamps
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
```
|
70
|
+
|
71
|
+
You can nominate multiple attributes in the `attr_array` class method:
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
class Post < ApplicationRecord
|
75
|
+
include AttrArray
|
76
|
+
|
77
|
+
attr_array :tags, :authors
|
78
|
+
end
|
79
|
+
```
|
80
|
+
|
81
|
+
### Usage
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
@post.tags = ["awesome", "slick"]
|
85
|
+
@post.authors = ["Roald Dahl"]
|
86
|
+
|
87
|
+
Post.with_any_authors("Roald Dahl")
|
88
|
+
Post.without_any_tags("slick")
|
89
|
+
```
|
90
|
+
|
91
|
+
## Acknowledgements
|
92
|
+
|
93
|
+
This gem is based on code from [pg_tags](https://github.com/yosriady/pg_tags). Modified to use ActiveSupport::Concern and doesn't automatically hook into ActiveRecord.
|
94
|
+
|
95
|
+
This gem is maintained by [Jurgen Jocubeit](https://github.com/JurgenJocubeit).
|
96
|
+
|
97
|
+
## Copyright
|
98
|
+
|
99
|
+
Copyright 2017 Pocket Business Co. All rights reserved.
|
data/lib/attr_array.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ActiveRecord::Base.send :include, AttrArray
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'active_support/all'
|
2
|
+
|
3
|
+
module AttrArray
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
class_methods do
|
7
|
+
def attr_array(*array_types)
|
8
|
+
array_types = array_types.to_a.flatten.compact.map(&:to_sym)
|
9
|
+
|
10
|
+
class_eval do
|
11
|
+
class_attribute :array_types
|
12
|
+
self.array_types = array_types
|
13
|
+
end
|
14
|
+
|
15
|
+
array_types.each do |array_type|
|
16
|
+
scope :"with_any_#{array_type}", -> (array) {
|
17
|
+
where("#{array_type} && ARRAY[?]::varchar[]", array)
|
18
|
+
}
|
19
|
+
scope :"with_all_#{array_type}", -> (array) {
|
20
|
+
where("#{array_type} @> ARRAY[?]::varchar[]", array)
|
21
|
+
}
|
22
|
+
scope :"without_any_#{array_type}", -> (array) {
|
23
|
+
where.not("#{array_type} && ARRAY[?]::varchar[]", array)
|
24
|
+
}
|
25
|
+
scope :"without_all_#{array_type}", -> (array) {
|
26
|
+
where.not("#{array_type} @> ARRAY[?]::varchar[]", array)
|
27
|
+
}
|
28
|
+
|
29
|
+
self.class.class_eval do
|
30
|
+
define_method :"all_#{array_type}" do |options = {}, &block|
|
31
|
+
subquery_scope = unscoped.select("unnest(#{table_name}.#{array_type}) as array").uniq
|
32
|
+
subquery_scope = subquery_scope.instance_eval(&block) if block
|
33
|
+
|
34
|
+
from(subquery_scope).pluck('array')
|
35
|
+
end
|
36
|
+
|
37
|
+
define_method :"#{array_type}_cloud" do |options = {}, &block|
|
38
|
+
subquery_scope = unscoped.select("unnest(#{table_name}.#{array_type}) as array")
|
39
|
+
subquery_scope = subquery_scope.instance_eval(&block) if block
|
40
|
+
|
41
|
+
from(subquery_scope).group('array').order('array').pluck('array, count(*) as count')
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module AttrArray
|
2
|
+
module Version
|
3
|
+
Major = 1
|
4
|
+
Minor = 0
|
5
|
+
Revision = 0
|
6
|
+
Prerelease = 0
|
7
|
+
Compact = [Major, Minor, Revision, Prerelease].compact.join('.')
|
8
|
+
Summary = "AttrArray v#{Compact}"
|
9
|
+
Description = "A high performance ActiveRecord concern for Rails using the PostgreSQL array type."
|
10
|
+
Author = "Jurgen Jocubeit"
|
11
|
+
Email = "support@brightcommerce.com"
|
12
|
+
Homepage = "https://github.com/brightcommerce/attr_array"
|
13
|
+
Metadata = {'copyright' => 'Copyright 2017 Pocket Business, Co. All Rights Reserved.'}
|
14
|
+
License = "UNLICENSED"
|
15
|
+
end
|
16
|
+
end
|
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: attr_array
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jurgen Jocubeit
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-11-06 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: 5.1.4
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 5.1.4
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 5.1.4
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 5.1.4
|
41
|
+
description: A high performance ActiveRecord concern for Rails using the PostgreSQL
|
42
|
+
array type.
|
43
|
+
email: support@brightcommerce.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- CHANGELOG.md
|
49
|
+
- README.md
|
50
|
+
- lib/attr_array.rb
|
51
|
+
- lib/attr_array/active_record.rb
|
52
|
+
- lib/attr_array/attr_array.rb
|
53
|
+
- lib/attr_array/version.rb
|
54
|
+
homepage: https://github.com/brightcommerce/attr_array
|
55
|
+
licenses:
|
56
|
+
- UNLICENSED
|
57
|
+
metadata:
|
58
|
+
copyright: Copyright 2017 Pocket Business, Co. All Rights Reserved.
|
59
|
+
post_install_message:
|
60
|
+
rdoc_options: []
|
61
|
+
require_paths:
|
62
|
+
- lib
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '2.3'
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
requirements: []
|
74
|
+
rubyforge_project:
|
75
|
+
rubygems_version: 2.6.11
|
76
|
+
signing_key:
|
77
|
+
specification_version: 4
|
78
|
+
summary: AttrArray v1.0.0.0
|
79
|
+
test_files: []
|