activerecord_dynamic_scope 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/workflows/ci.yml +52 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +3 -0
- data/LICENSE +21 -0
- data/README.md +90 -0
- data/Rakefile +6 -0
- data/activerecord_dynamic_scope.gemspec +29 -0
- data/gemfiles/rails60.gemfile +6 -0
- data/gemfiles/rails61.gemfile +5 -0
- data/gemfiles/rails70.gemfile +5 -0
- data/lib/activerecord_dynamic_scope/extension.rb +18 -0
- data/lib/activerecord_dynamic_scope/railtie.rb +7 -0
- data/lib/activerecord_dynamic_scope/version.rb +3 -0
- data/lib/activerecord_dynamic_scope.rb +31 -0
- metadata +170 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 9c089df0d3694598d4deff6a7b70ce1f09dcfc0eed24e0e6abf67ba2b20c696a
|
4
|
+
data.tar.gz: dfeb08e59e4fe2430ce324028cbefed06a1e228d0c975b753a1218ca568c4677
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 11c125ba8667bb8c8442696d87fd342717f50dc4fb1df408fe82adc591bcb3e453739ecc2d1e645ff6a8ad74df6f24b6c05127deaa9a5b63d265aa3801527039
|
7
|
+
data.tar.gz: e95e40f5dd9cc72c0e07c6d6412dbaef4a4e24932f626f63d1037c9c64650ac695c3295faa335671b4d5c6a524529a7d289062be5413c15fac49c6f924eb6a3f
|
@@ -0,0 +1,52 @@
|
|
1
|
+
name: CI
|
2
|
+
|
3
|
+
on: [push, pull_request]
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
test:
|
7
|
+
runs-on: ubuntu-20.04
|
8
|
+
services:
|
9
|
+
postgres:
|
10
|
+
image: postgres:9.5
|
11
|
+
env:
|
12
|
+
POSTGRES_USER: postgres
|
13
|
+
POSTGRES_PASSWORD: postgres
|
14
|
+
ports:
|
15
|
+
- 5432:5432
|
16
|
+
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
|
17
|
+
mysql:
|
18
|
+
image: mysql:5.7
|
19
|
+
env:
|
20
|
+
MYSQL_ALLOW_EMPTY_PASSWORD: yes
|
21
|
+
ports:
|
22
|
+
- 3306:3306
|
23
|
+
options: --health-cmd "mysqladmin ping -h localhost" --health-interval 20s --health-timeout 10s --health-retries 10
|
24
|
+
strategy:
|
25
|
+
fail-fast: false
|
26
|
+
matrix:
|
27
|
+
ruby: [2.7, '3.0', 3.1, 3.2]
|
28
|
+
gemfile: ['rails60', 'rails61', 'rails70']
|
29
|
+
database: ['sqlite', 'mysql', 'postgresql']
|
30
|
+
|
31
|
+
name: ruby ${{ matrix.ruby }}, ${{ matrix.gemfile }}, ${{ matrix.database }}
|
32
|
+
|
33
|
+
env:
|
34
|
+
DATABASE: ${{ matrix.database }}
|
35
|
+
POSTGRES_USER: postgres
|
36
|
+
POSTGRES_PASSWORD: postgres
|
37
|
+
BUNDLE_GEMFILE: gemfiles/${{ matrix.gemfile }}.gemfile
|
38
|
+
|
39
|
+
steps:
|
40
|
+
- uses: actions/checkout@v3
|
41
|
+
- uses: ruby/setup-ruby@v1
|
42
|
+
with:
|
43
|
+
ruby-version: ${{ matrix.ruby }}
|
44
|
+
bundler-cache: true
|
45
|
+
- name: Prepare test
|
46
|
+
run: |
|
47
|
+
cd spec/dummy
|
48
|
+
BUNDLE_GEMFILE=../../${{ env.BUNDLE_GEMFILE }} RAILS_ENV=test bundle exec rake db:create db:migrate db:seed
|
49
|
+
cd ../..
|
50
|
+
- name: Run test
|
51
|
+
run: |
|
52
|
+
bundle exec rspec
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2023 Yoshikazu Kaneta
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
# ActiveRecordDynamicScope
|
2
|
+
|
3
|
+
Handling dynamic scope for ActiveRecord.
|
4
|
+
|
5
|
+
## Dependencies
|
6
|
+
|
7
|
+
* ruby 2.7+
|
8
|
+
* activerecord 6.0+
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'activerecord_dynamic_scope'
|
16
|
+
```
|
17
|
+
|
18
|
+
Then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
Define a name of dynamic scope in your model using `dynamic_scope`:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
class Item < ActiveRecord::Base
|
28
|
+
dynamic_scope :partition
|
29
|
+
end
|
30
|
+
```
|
31
|
+
|
32
|
+
The name of dynamic scope should be same as an attribute name because the name is used as a key of query.
|
33
|
+
|
34
|
+
Execute your query using `ActiveRecordDynamicScope.with` as follows:
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
ActiveRecordDynamicScope.with(partition: 1) do
|
38
|
+
Item.all #=> SELECT "items".* FROM "items" WHERE "items"."partition" = 1
|
39
|
+
end
|
40
|
+
```
|
41
|
+
|
42
|
+
You can clear dynamic scope using `ActiveRecordDynamicScope.without` as follows:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
ActiveRecordDynamicScope.with(partition: 1) do
|
46
|
+
Item.all.to_a #=> SELECT "items".* FROM "items" WHERE "items"."partition" = 1
|
47
|
+
ActiveRecordDynamicScope.without(:partition) do
|
48
|
+
Item.all #=> SELECT "items".* FROM "items"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
```
|
52
|
+
|
53
|
+
### Nested scope
|
54
|
+
|
55
|
+
You can also nest dynamic scope:
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
class Item < ActiveRecord::Base
|
59
|
+
dynamic_scope :partition1
|
60
|
+
dynamic_scope :partition2
|
61
|
+
end
|
62
|
+
|
63
|
+
ActiveRecordDynamicScope.with(partition1: 1) do
|
64
|
+
ActiveRecordDynamicScope.with(partition2: 2) do
|
65
|
+
Item.all #=> SELECT "items".* FROM "items" WHERE "items"."partition1" = 1 AND "items"."partition2" = 2
|
66
|
+
end
|
67
|
+
end
|
68
|
+
```
|
69
|
+
|
70
|
+
### Alias name
|
71
|
+
|
72
|
+
You can use alias name if the name of dynamic scope is different from an attribute name:
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
class Item < ActiveRecord::Base
|
76
|
+
dynamic_scope :account, as: :partition
|
77
|
+
end
|
78
|
+
|
79
|
+
ActiveRecordDynamicScope.with(account: 1) do
|
80
|
+
Item.all #=> SELECT "items".* FROM "items" WHERE "items"."partition1" = 1 AND "items"."partition2" = 2
|
81
|
+
end
|
82
|
+
```
|
83
|
+
|
84
|
+
## Contributing
|
85
|
+
|
86
|
+
Bug reports and pull requests are welcome at https://github.com/kanety/activerecord_dynamic_scope.
|
87
|
+
|
88
|
+
## License
|
89
|
+
|
90
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'activerecord_dynamic_scope/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "activerecord_dynamic_scope"
|
8
|
+
spec.version = ActiveRecordDynamicScope::VERSION
|
9
|
+
spec.authors = ["Yoshikazu Kaneta"]
|
10
|
+
spec.email = ["kaneta@sitebridge.co.jp"]
|
11
|
+
spec.summary = %q{Handling dynamic scope for ActiveRecord.}
|
12
|
+
spec.description = %q{Handling dynamic scope for ActiveRecord.}
|
13
|
+
spec.homepage = "https://github.com/kanety/activerecord_dynamic_scope"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
16
|
+
spec.bindir = "exe"
|
17
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_dependency "activerecord", ">= 6.0"
|
21
|
+
spec.add_dependency "activesupport", ">= 6.0"
|
22
|
+
|
23
|
+
spec.add_development_dependency "rails", ">= 6.0"
|
24
|
+
spec.add_development_dependency "mysql2"
|
25
|
+
spec.add_development_dependency "pg"
|
26
|
+
spec.add_development_dependency "sqlite3"
|
27
|
+
spec.add_development_dependency "rspec-rails"
|
28
|
+
spec.add_development_dependency "simplecov"
|
29
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module ActiveRecordDynamicScope
|
2
|
+
module Extension
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
class_methods do
|
6
|
+
def dynamic_scope(name, as: nil)
|
7
|
+
default_scope -> {
|
8
|
+
current_scope = ActiveRecordDynamicScope.current(name)
|
9
|
+
if current_scope
|
10
|
+
where((as || name) => current_scope)
|
11
|
+
else
|
12
|
+
all
|
13
|
+
end
|
14
|
+
}
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
|
3
|
+
require_relative 'activerecord_dynamic_scope/version'
|
4
|
+
require_relative 'activerecord_dynamic_scope/extension'
|
5
|
+
require_relative 'activerecord_dynamic_scope/railtie' if defined?(Rails)
|
6
|
+
|
7
|
+
module ActiveRecordDynamicScope
|
8
|
+
KEY = :activerecord_dynamic_scope
|
9
|
+
|
10
|
+
class << self
|
11
|
+
def current(name)
|
12
|
+
Thread.current[KEY].to_h[name]
|
13
|
+
end
|
14
|
+
|
15
|
+
def with(hash = {})
|
16
|
+
old = Thread.current[KEY]
|
17
|
+
Thread.current[KEY] = Thread.current[KEY].to_h.merge(hash)
|
18
|
+
yield
|
19
|
+
ensure
|
20
|
+
Thread.current[KEY] = old
|
21
|
+
end
|
22
|
+
|
23
|
+
def without(keys)
|
24
|
+
old = Thread.current[KEY]
|
25
|
+
Thread.current[KEY] = Thread.current[KEY].to_h.except(keys)
|
26
|
+
yield
|
27
|
+
ensure
|
28
|
+
Thread.current[KEY] = old
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
metadata
ADDED
@@ -0,0 +1,170 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activerecord_dynamic_scope
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yoshikazu Kaneta
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-06-26 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: '6.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '6.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '6.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '6.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rails
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '6.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '6.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: mysql2
|
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
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pg
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: sqlite3
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rspec-rails
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: simplecov
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
description: Handling dynamic scope for ActiveRecord.
|
126
|
+
email:
|
127
|
+
- kaneta@sitebridge.co.jp
|
128
|
+
executables: []
|
129
|
+
extensions: []
|
130
|
+
extra_rdoc_files: []
|
131
|
+
files:
|
132
|
+
- ".github/workflows/ci.yml"
|
133
|
+
- ".gitignore"
|
134
|
+
- ".rspec"
|
135
|
+
- CHANGELOG.md
|
136
|
+
- Gemfile
|
137
|
+
- LICENSE
|
138
|
+
- README.md
|
139
|
+
- Rakefile
|
140
|
+
- activerecord_dynamic_scope.gemspec
|
141
|
+
- gemfiles/rails60.gemfile
|
142
|
+
- gemfiles/rails61.gemfile
|
143
|
+
- gemfiles/rails70.gemfile
|
144
|
+
- lib/activerecord_dynamic_scope.rb
|
145
|
+
- lib/activerecord_dynamic_scope/extension.rb
|
146
|
+
- lib/activerecord_dynamic_scope/railtie.rb
|
147
|
+
- lib/activerecord_dynamic_scope/version.rb
|
148
|
+
homepage: https://github.com/kanety/activerecord_dynamic_scope
|
149
|
+
licenses: []
|
150
|
+
metadata: {}
|
151
|
+
post_install_message:
|
152
|
+
rdoc_options: []
|
153
|
+
require_paths:
|
154
|
+
- lib
|
155
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - ">="
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: '0'
|
160
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
161
|
+
requirements:
|
162
|
+
- - ">="
|
163
|
+
- !ruby/object:Gem::Version
|
164
|
+
version: '0'
|
165
|
+
requirements: []
|
166
|
+
rubygems_version: 3.3.3
|
167
|
+
signing_key:
|
168
|
+
specification_version: 4
|
169
|
+
summary: Handling dynamic scope for ActiveRecord.
|
170
|
+
test_files: []
|