active_record_bitmask 0.0.2 → 0.0.6
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 +4 -4
- data/.gitignore +2 -0
- data/.rubocop.yml +1 -0
- data/.travis.yml +28 -2
- data/Appraisals +26 -0
- data/CHANGELOG.md +30 -0
- data/Gemfile +6 -5
- data/README.md +39 -7
- data/active_record_bitmask.gemspec +8 -4
- data/gemfiles/5.0_stable.gemfile +13 -0
- data/gemfiles/5.1_stable.gemfile +13 -0
- data/gemfiles/5.2_stable.gemfile +13 -0
- data/gemfiles/6.0_stable.gemfile +13 -0
- data/gemfiles/6.1_stable.gemfile +13 -0
- data/gemfiles/7.0_stable.gemfile +13 -0
- data/lib/active_record_bitmask/attribute_methods/query.rb +26 -5
- data/lib/active_record_bitmask/bitmask_type.rb +0 -2
- data/lib/active_record_bitmask/map.rb +6 -10
- data/lib/active_record_bitmask/model.rb +21 -3
- data/lib/active_record_bitmask/version.rb +1 -1
- data/lib/active_record_bitmask.rb +1 -1
- metadata +16 -46
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 92ef09e4ac0ca5c2da0db1869dc52776f5d990292e9c921bf02e364f1535925c
|
4
|
+
data.tar.gz: 5e48672686d4d15b10f547a2fccd0c017799dd259bf36f2d1184da9261bce2c8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 99696c27cf5fdcfede8e64be3791da418276f6dbd7b1edcb960b5dec585aa14587e82bf686e635d60cbccd0fbc4ab0c88e39150a24504c98e974b07048a55825
|
7
|
+
data.tar.gz: d3dfbe52f15833aada4cbe70559f93c3493511872a1af6f024c1f54d77c8697dab77f396debed19be074014bbe3e077442cfadb2e0a2da414a0126eecf89c07c
|
data/.gitignore
CHANGED
data/.rubocop.yml
CHANGED
data/.travis.yml
CHANGED
@@ -1,5 +1,31 @@
|
|
1
1
|
sudo: false
|
2
2
|
language: ruby
|
3
3
|
rvm:
|
4
|
-
- 2.
|
5
|
-
|
4
|
+
- 2.5.8
|
5
|
+
- 2.6.6
|
6
|
+
- 2.7.2
|
7
|
+
- 3.0.0
|
8
|
+
before_install: gem install bundler
|
9
|
+
gemfile:
|
10
|
+
- gemfiles/5.0_stable.gemfile
|
11
|
+
- gemfiles/5.1_stable.gemfile
|
12
|
+
- gemfiles/5.2_stable.gemfile
|
13
|
+
- gemfiles/6.0_stable.gemfile
|
14
|
+
- gemfiles/6.1_stable.gemfile
|
15
|
+
matrix:
|
16
|
+
exclude:
|
17
|
+
- rvm: 2.5.8
|
18
|
+
gemfile: gemfiles/6.1_stable.gemfile
|
19
|
+
- rvm: 2.6.6
|
20
|
+
gemfile: gemfiles/6.1_stable.gemfile
|
21
|
+
- rvm: 2.7.2
|
22
|
+
gemfile: gemfiles/6.1_stable.gemfile
|
23
|
+
- rvm: 3.0.0
|
24
|
+
gemfile: gemfiles/5.0_stable.gemfile
|
25
|
+
- rvm: 3.0.0
|
26
|
+
gemfile: gemfiles/5.1_stable.gemfile
|
27
|
+
- rvm: 3.0.0
|
28
|
+
gemfile: gemfiles/5.2_stable.gemfile
|
29
|
+
script:
|
30
|
+
- bundle exec rspec
|
31
|
+
- bundle exec rubocop
|
data/Appraisals
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
appraise '5.0-stable' do
|
4
|
+
gem 'activerecord', '~> 5.0.0'
|
5
|
+
gem 'sqlite3', '~> 1.3.6'
|
6
|
+
end
|
7
|
+
|
8
|
+
appraise '5.1-stable' do
|
9
|
+
gem 'activerecord', '~> 5.1.0'
|
10
|
+
end
|
11
|
+
|
12
|
+
appraise '5.2-stable' do
|
13
|
+
gem 'activerecord', '~> 5.2.0'
|
14
|
+
end
|
15
|
+
|
16
|
+
appraise '6.0-stable' do
|
17
|
+
gem 'activerecord', '~> 6.0.0'
|
18
|
+
end
|
19
|
+
|
20
|
+
appraise '6.1-stable' do
|
21
|
+
gem 'activerecord', '~> 6.1.0.rc1'
|
22
|
+
end
|
23
|
+
|
24
|
+
appraise '7.0-stable' do
|
25
|
+
gem 'activerecord', '7.0.0.alpha2'
|
26
|
+
end
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
## 0.0.6
|
2
|
+
|
3
|
+
* Deprecate `#attribute?(*args)` in favor of `#attribute_bitmask?(*args)`. #17
|
4
|
+
* Support Rails 7.0.0.alpha1 #18
|
5
|
+
|
6
|
+
## 0.0.5
|
7
|
+
|
8
|
+
* Support Rails 6.1.0 #14
|
9
|
+
|
10
|
+
## 0.0.4
|
11
|
+
|
12
|
+
* Fixes security alert #9
|
13
|
+
|
14
|
+
* Query methods supports string arguments #12
|
15
|
+
|
16
|
+
## 0.0.3
|
17
|
+
|
18
|
+
* Enumerize ActiveRecordBitmask::Map #4
|
19
|
+
|
20
|
+
* Support old rails #5
|
21
|
+
|
22
|
+
* Run RuboCop on CI. #6
|
23
|
+
|
24
|
+
## 0.0.2
|
25
|
+
|
26
|
+
* Support blank values for checkbox #3
|
27
|
+
|
28
|
+
## 0.0.1
|
29
|
+
|
30
|
+
* first release.
|
data/Gemfile
CHANGED
@@ -3,8 +3,9 @@ source 'https://rubygems.org'
|
|
3
3
|
gemspec
|
4
4
|
|
5
5
|
gem 'pry'
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
6
|
+
# Don't update rubocop automatically because sometimes it fails test by breaking changes.
|
7
|
+
gem 'rubocop', '0.86.0'
|
8
|
+
gem 'appraisal'
|
9
|
+
gem 'rake'
|
10
|
+
gem 'rspec'
|
11
|
+
gem 'sqlite3'
|
data/README.md
CHANGED
@@ -24,6 +24,8 @@ Simply declare an existing integer column as a bitmask.
|
|
24
24
|
|
25
25
|
```ruby
|
26
26
|
class Post < ApplicationRecord
|
27
|
+
# bitmask attribute is integer column.
|
28
|
+
# `t.integer :roles, null: false, default: 0`
|
27
29
|
bitmask(roles: [:administrator, :provider, :guest])
|
28
30
|
end
|
29
31
|
```
|
@@ -41,8 +43,8 @@ You can check bitmask
|
|
41
43
|
|
42
44
|
```ruby
|
43
45
|
post = Post.create(roles: [:provider, :guest])
|
44
|
-
post.
|
45
|
-
post.
|
46
|
+
post.roles_bitmask?(:provider) #=> false
|
47
|
+
post.roles_bitmask?(:guest, :provider) #=> true
|
46
48
|
```
|
47
49
|
|
48
50
|
You can get the definition of bitmask
|
@@ -55,15 +57,45 @@ map.values #=> [1, 2, 4]
|
|
55
57
|
|
56
58
|
### Scopes
|
57
59
|
|
58
|
-
|
60
|
+
Named scopes are defined when you call `.bitmask(attribute)`.
|
59
61
|
|
60
|
-
#### `
|
62
|
+
#### `.with_attribute`
|
61
63
|
|
62
|
-
|
64
|
+
```ruby
|
65
|
+
# all users with roles
|
66
|
+
User.with_roles
|
67
|
+
|
68
|
+
# all administrators
|
69
|
+
User.with_roles(:administrator)
|
70
|
+
|
71
|
+
# all users who are both administrator and partner
|
72
|
+
User.with_roles(:administrator, :partner)
|
73
|
+
```
|
74
|
+
|
75
|
+
#### `.with_any_attribute`
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
# all users who are administrator or partner
|
79
|
+
User.with_any_roles(:administrator, :partner)
|
80
|
+
```
|
63
81
|
|
64
|
-
#### `
|
82
|
+
#### `.without_attribute`, `.no_attribute`
|
65
83
|
|
66
|
-
|
84
|
+
```ruby
|
85
|
+
# all users without role
|
86
|
+
User.without_roles
|
87
|
+
User.no_roles
|
88
|
+
|
89
|
+
# all users who are not administrator
|
90
|
+
User.without_roles(:administrator)
|
91
|
+
```
|
92
|
+
|
93
|
+
#### `.with_exact_attribute`
|
94
|
+
|
95
|
+
```ruby
|
96
|
+
# all users who are both administrator and partner and nothing else
|
97
|
+
User.with_exact_roles(:administrator, :partner)
|
98
|
+
```
|
67
99
|
|
68
100
|
## Development
|
69
101
|
|
@@ -14,13 +14,17 @@ Gem::Specification.new do |spec|
|
|
14
14
|
spec.summary = 'Simple bitmask attribute support for ActiveRecord'
|
15
15
|
spec.homepage = 'https://github.com/alpaca-tc/active_record_bitmask'
|
16
16
|
|
17
|
-
spec.
|
17
|
+
spec.metadata = {
|
18
|
+
'homepage_uri' => 'https://github.com/alpaca-tc/active_record_bitmask',
|
19
|
+
'changelog_uri' => 'https://github.com/alpaca-tc/active_record_bitmask/blob/master/CHANGELOG.md',
|
20
|
+
'source_code_uri' => 'https://github.com/alpaca-tc/active_record_bitmask/',
|
21
|
+
'bug_tracker_uri' => 'https://github.com/alpaca-tc/active_record_bitmask/issues'
|
22
|
+
}
|
23
|
+
|
24
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
25
|
f.match(%r{^(test|spec|features)/})
|
19
26
|
end
|
20
27
|
spec.require_paths = ['lib']
|
21
28
|
|
22
29
|
spec.add_dependency 'activerecord', '>= 5.0'
|
23
|
-
spec.add_development_dependency 'bundler'
|
24
|
-
spec.add_development_dependency 'rake'
|
25
|
-
spec.add_development_dependency 'rspec'
|
26
30
|
end
|
@@ -1,24 +1,45 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'active_record'
|
4
|
+
require 'active_support/concern'
|
5
|
+
|
3
6
|
module ActiveRecordBitmask
|
4
7
|
module AttributeMethods
|
5
8
|
module Query
|
9
|
+
extend ActiveSupport::Concern
|
10
|
+
|
11
|
+
included do
|
12
|
+
attribute_method_suffix('_bitmask?')
|
13
|
+
end
|
14
|
+
|
6
15
|
private
|
7
16
|
|
8
17
|
def bitmask_attribute?(attribute_name)
|
9
18
|
self.class.bitmasks.key?(attribute_name.to_sym)
|
10
19
|
end
|
11
20
|
|
12
|
-
|
21
|
+
if ActiveRecord.gem_version < Gem::Version.create('7.0.0.alpha1')
|
22
|
+
# In Rails 7.0.0, calling attribute? with arguments is not working.
|
23
|
+
def attribute?(attribute_name, *values)
|
24
|
+
if bitmask_attribute?(attribute_name) && values.present?
|
25
|
+
ActiveSupport::Deprecation.warn("`#{attribute_name}?(*args)` is deprecated and will be removed in next major version. Call `#{attribute_name}_bitmask?(*args)` instead.")
|
26
|
+
attribute_bitmask?(attribute_name, *values)
|
27
|
+
else
|
28
|
+
super
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def attribute_bitmask?(attribute_name, *values)
|
13
34
|
if bitmask_attribute?(attribute_name) && values.present?
|
14
35
|
# assert bitmask values
|
15
36
|
map = self.class.bitmask_for(attribute_name)
|
16
|
-
map.attributes_to_bitmask(values)
|
37
|
+
expected_value = map.attributes_to_bitmask(values)
|
38
|
+
current_value = map.attributes_to_bitmask(attribute(attribute_name))
|
17
39
|
|
18
|
-
current_value
|
19
|
-
values.all? { |value| current_value.include?(value) }
|
40
|
+
(current_value & expected_value) == expected_value
|
20
41
|
else
|
21
|
-
|
42
|
+
attribute?(attribute_name)
|
22
43
|
end
|
23
44
|
end
|
24
45
|
end
|
@@ -1,7 +1,13 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'active_support/core_ext/module/delegation'
|
4
|
+
|
3
5
|
module ActiveRecordBitmask
|
4
6
|
class Map
|
7
|
+
include Enumerable
|
8
|
+
|
9
|
+
delegate(:each, :keys, :values, to: :mapping)
|
10
|
+
|
5
11
|
# Default blank values for checkbox
|
6
12
|
BLANK_VALUES = [:'', :'0'].freeze
|
7
13
|
|
@@ -64,16 +70,6 @@ module ActiveRecordBitmask
|
|
64
70
|
end
|
65
71
|
end
|
66
72
|
|
67
|
-
# @return [Array<Symbol>]
|
68
|
-
def keys
|
69
|
-
mapping.keys
|
70
|
-
end
|
71
|
-
|
72
|
-
# @return [Array<Integer>]
|
73
|
-
def values
|
74
|
-
mapping.values
|
75
|
-
end
|
76
|
-
|
77
73
|
private
|
78
74
|
|
79
75
|
def reject_blank_attributes(attributes)
|
@@ -9,7 +9,10 @@ module ActiveRecordBitmask
|
|
9
9
|
|
10
10
|
included do
|
11
11
|
include ActiveRecordBitmask::AttributeMethods::Query
|
12
|
-
class_attribute(:bitmasks, instance_accessor: false
|
12
|
+
class_attribute(:bitmasks, instance_accessor: false)
|
13
|
+
|
14
|
+
# `class_attribute` on Rails 5.0 doesn't support default option.
|
15
|
+
self.bitmasks = {}
|
13
16
|
end
|
14
17
|
|
15
18
|
class_methods do
|
@@ -48,11 +51,25 @@ module ActiveRecordBitmask
|
|
48
51
|
private
|
49
52
|
|
50
53
|
def define_bitmask_attribute(attribute, map)
|
51
|
-
|
52
|
-
|
54
|
+
if ActiveRecord.gem_version >= Gem::Version.new('7.0.0.alpha1')
|
55
|
+
# Greater than or equal to 7.0.0
|
56
|
+
attribute(attribute) do |subtype|
|
57
|
+
ActiveRecordBitmask::BitmaskType.new(attribute, map, subtype)
|
58
|
+
end
|
59
|
+
elsif ActiveRecord.gem_version >= Gem::Version.new('6.1.0')
|
60
|
+
# Equal to 6.1.0
|
61
|
+
decorate_attribute_type(attribute) do |subtype|
|
62
|
+
ActiveRecordBitmask::BitmaskType.new(attribute, map, subtype)
|
63
|
+
end
|
64
|
+
else
|
65
|
+
# Less than 6.1.0
|
66
|
+
decorate_attribute_type(attribute, :bitmask) do |subtype|
|
67
|
+
ActiveRecordBitmask::BitmaskType.new(attribute, map, subtype)
|
68
|
+
end
|
53
69
|
end
|
54
70
|
end
|
55
71
|
|
72
|
+
# rubocop:disable Metrics/CyclomaticComplexity
|
56
73
|
def define_bitmask_scopes(attribute)
|
57
74
|
blank = [0, nil].freeze
|
58
75
|
|
@@ -109,6 +126,7 @@ module ActiveRecordBitmask
|
|
109
126
|
where(attribute => blank)
|
110
127
|
}
|
111
128
|
end
|
129
|
+
# rubocop:enable Metrics/CyclomaticComplexity
|
112
130
|
end
|
113
131
|
end
|
114
132
|
end
|
@@ -5,11 +5,11 @@ require 'active_support/lazy_load_hooks'
|
|
5
5
|
module ActiveRecordBitmask
|
6
6
|
require 'active_record_bitmask/version'
|
7
7
|
require 'active_record_bitmask/attribute_methods'
|
8
|
-
require 'active_record_bitmask/bitmask_type'
|
9
8
|
require 'active_record_bitmask/map'
|
10
9
|
end
|
11
10
|
|
12
11
|
ActiveSupport.on_load :active_record do
|
13
12
|
require 'active_record_bitmask/model'
|
13
|
+
require 'active_record_bitmask/bitmask_type'
|
14
14
|
include(ActiveRecordBitmask::Model)
|
15
15
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_record_bitmask
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- alpaca-tc
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-09-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -24,48 +24,6 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '5.0'
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: bundler
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - ">="
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '0'
|
34
|
-
type: :development
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - ">="
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: '0'
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: rake
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - ">="
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: '0'
|
48
|
-
type: :development
|
49
|
-
prerelease: false
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
51
|
-
requirements:
|
52
|
-
- - ">="
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: '0'
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: rspec
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
58
|
-
requirements:
|
59
|
-
- - ">="
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: '0'
|
62
|
-
type: :development
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - ">="
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
version: '0'
|
69
27
|
description:
|
70
28
|
email:
|
71
29
|
- alpaca-tc@alpaca.tc
|
@@ -77,10 +35,18 @@ files:
|
|
77
35
|
- ".rspec"
|
78
36
|
- ".rubocop.yml"
|
79
37
|
- ".travis.yml"
|
38
|
+
- Appraisals
|
39
|
+
- CHANGELOG.md
|
80
40
|
- Gemfile
|
81
41
|
- README.md
|
82
42
|
- Rakefile
|
83
43
|
- active_record_bitmask.gemspec
|
44
|
+
- gemfiles/5.0_stable.gemfile
|
45
|
+
- gemfiles/5.1_stable.gemfile
|
46
|
+
- gemfiles/5.2_stable.gemfile
|
47
|
+
- gemfiles/6.0_stable.gemfile
|
48
|
+
- gemfiles/6.1_stable.gemfile
|
49
|
+
- gemfiles/7.0_stable.gemfile
|
84
50
|
- lib/active_record_bitmask.rb
|
85
51
|
- lib/active_record_bitmask/attribute_methods.rb
|
86
52
|
- lib/active_record_bitmask/attribute_methods/query.rb
|
@@ -91,7 +57,11 @@ files:
|
|
91
57
|
homepage: https://github.com/alpaca-tc/active_record_bitmask
|
92
58
|
licenses:
|
93
59
|
- MIT
|
94
|
-
metadata:
|
60
|
+
metadata:
|
61
|
+
homepage_uri: https://github.com/alpaca-tc/active_record_bitmask
|
62
|
+
changelog_uri: https://github.com/alpaca-tc/active_record_bitmask/blob/master/CHANGELOG.md
|
63
|
+
source_code_uri: https://github.com/alpaca-tc/active_record_bitmask/
|
64
|
+
bug_tracker_uri: https://github.com/alpaca-tc/active_record_bitmask/issues
|
95
65
|
post_install_message:
|
96
66
|
rdoc_options: []
|
97
67
|
require_paths:
|
@@ -107,7 +77,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
107
77
|
- !ruby/object:Gem::Version
|
108
78
|
version: '0'
|
109
79
|
requirements: []
|
110
|
-
rubygems_version: 3.
|
80
|
+
rubygems_version: 3.1.6
|
111
81
|
signing_key:
|
112
82
|
specification_version: 4
|
113
83
|
summary: Simple bitmask attribute support for ActiveRecord
|