scopable 1.1.3 → 1.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -1
- data/Gemfile +2 -2
- data/README.md +45 -49
- data/lib/scopable/version.rb +1 -1
- data/scopable.gemspec +3 -3
- metadata +4 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2f2ceee764d1cb24f319eb1013a1e99af71f2e51
|
4
|
+
data.tar.gz: 8d79497d0ac281967f13c74c41d0f6b7c7ab7af3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cb49a404433b3bd4124fddc3843e0c2e374b9d8f82c1ff856fd70bf34de6d829cfe63a0049d7569485bb9688482ebec2048145a64ff14695a13cc7b7175654a7
|
7
|
+
data.tar.gz: e38b3c2f659179047f611aa63b74203e50eb2a77580aae139a446589c55e6b4a415c428f5cdff4858e5b12961e301eb730b28c75158aa79343241c4558c6ac8c
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
[![RubyGems](https://img.shields.io/gem/dt/scopable.svg)](https://rubygems.org/gems/scopable)
|
2
|
-
[![Build](https://img.shields.io/travis/
|
3
|
-
[![Code Climate](https://img.shields.io/codeclimate/github/
|
4
|
-
[![Test Coverage](https://img.shields.io/codeclimate/coverage/github/
|
1
|
+
[![RubyGems](https://img.shields.io/gem/dt/scopable.svg?style=flat-square)](https://rubygems.org/gems/scopable)
|
2
|
+
[![Build](https://img.shields.io/travis/corenzan/scopable.svg?style=flat-square)](https://travis-ci.org/corenzan/scopable)
|
3
|
+
[![Code Climate](https://img.shields.io/codeclimate/github/corenzan/scopable.svg?style=flat-square)](https://codeclimate.com/github/corenzan/scopable)
|
4
|
+
[![Test Coverage](https://img.shields.io/codeclimate/coverage/github/corenzan/scopable.svg?style=flat-square)](https://codeclimate.com/github/corenzan/scopable/coverage)
|
5
5
|
|
6
6
|
# Scopable
|
7
7
|
|
@@ -18,7 +18,7 @@ gem 'scopable'
|
|
18
18
|
And then execute:
|
19
19
|
|
20
20
|
```shell
|
21
|
-
$ bundle
|
21
|
+
$ bundle install
|
22
22
|
```
|
23
23
|
|
24
24
|
Or install it yourself with:
|
@@ -29,7 +29,7 @@ $ gem install scopable
|
|
29
29
|
|
30
30
|
## Usage
|
31
31
|
|
32
|
-
|
32
|
+
First you need to set scopes in your controller:
|
33
33
|
|
34
34
|
```ruby
|
35
35
|
class PostsController < ApplicationController
|
@@ -41,28 +41,48 @@ class PostsController < ApplicationController
|
|
41
41
|
end
|
42
42
|
```
|
43
43
|
|
44
|
-
Then apply
|
44
|
+
Then apply them when querying the model:
|
45
45
|
|
46
46
|
```ruby
|
47
|
-
|
48
|
-
|
47
|
+
class PostsController < ApplicationController
|
48
|
+
include Scopable
|
49
|
+
|
50
|
+
scope :search, param: :q
|
51
|
+
|
52
|
+
def index
|
53
|
+
@posts = scoped(Post, params)
|
54
|
+
end
|
49
55
|
end
|
50
56
|
```
|
51
57
|
|
52
|
-
Now whenever the parameter `q` is present in `params`, `#search` will be called on your model
|
58
|
+
Now whenever the parameter `q` is present in `params`, the scope `#search` will be called on your model and given the value of `params[:q]` as argument. Otherwise you would have to write something like this:
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
if params[:q].present?
|
62
|
+
@posts = Post.search(params[:q])
|
63
|
+
else
|
64
|
+
@posts = Post.all
|
65
|
+
end
|
66
|
+
```
|
53
67
|
|
54
|
-
|
68
|
+
What would be fine, except you usually have multiple scopes, that might get combined depending on the presence or absence of parameters to produce the final query. Look how simple it becomes when using Scopable:
|
55
69
|
|
56
70
|
```ruby
|
57
71
|
class PostController < ApplicationController
|
58
72
|
include Scopable
|
59
73
|
|
60
|
-
|
61
|
-
|
74
|
+
# Filter by category.
|
75
|
+
scope :category do |relation, value|
|
76
|
+
relation.where(category_id: value.to_i)
|
62
77
|
end
|
63
78
|
|
64
|
-
|
79
|
+
# Fix N+1.
|
80
|
+
scope :includes, force: :author
|
81
|
+
|
82
|
+
# Pagination.
|
83
|
+
scope :page, default: 1
|
65
84
|
|
85
|
+
# Sort by creation date.
|
66
86
|
scope :order, force: { created_at: :desc }
|
67
87
|
|
68
88
|
def index
|
@@ -71,39 +91,23 @@ class PostController < ApplicationController
|
|
71
91
|
end
|
72
92
|
```
|
73
93
|
|
74
|
-
Now say
|
94
|
+
Now say a request is made looking like this:
|
75
95
|
|
76
96
|
```
|
77
|
-
/posts?
|
97
|
+
/posts?category=2
|
78
98
|
```
|
79
99
|
|
80
|
-
The resulting
|
100
|
+
The resulting query would be:
|
81
101
|
|
82
102
|
```ruby
|
83
|
-
Post.where(
|
103
|
+
Post.where(category_id: 2).includes(:author).page(1).order(created_at: :desc)
|
84
104
|
```
|
85
105
|
|
86
|
-
|
106
|
+
Please note that **order matters**. The scopes will be applied in the same order they are configured.
|
87
107
|
|
88
|
-
Also
|
108
|
+
Also values like `true/false`, `on/off`, `yes/no` are **cast as boolean**, and when given a boolean value the scope is either called with no arguments or skipped entirely. For instance, if you set a scope like `scope :draft` then request the URL `/posts?draft=yes` it would be like just calling `Post.draft`. But if you request `/posts?draft=no` it does nothing.
|
89
109
|
|
90
|
-
|
91
|
-
scope :active
|
92
|
-
```
|
93
|
-
|
94
|
-
With a URL like this:
|
95
|
-
|
96
|
-
```ruby
|
97
|
-
/?active=yes
|
98
|
-
```
|
99
|
-
|
100
|
-
Would be equivalent to:
|
101
|
-
|
102
|
-
```ruby
|
103
|
-
Model.active
|
104
|
-
```
|
105
|
-
|
106
|
-
## Options
|
110
|
+
### Options
|
107
111
|
|
108
112
|
No option is required. By default it assumes both scope and parameter have the same name.
|
109
113
|
|
@@ -112,19 +116,11 @@ Key | Description
|
|
112
116
|
`:param` | Name of the parameter that activates the scope.
|
113
117
|
`:default` | Default value for the scope in case the parameter is missing.
|
114
118
|
`:force` | Force a value to the scope regardless of the request parameters.
|
115
|
-
`:required` | Calls `#none` on the model if parameter is absent and no default value
|
116
|
-
`:only` | The scope will only be applied to these actions.
|
117
|
-
`:except` | The scope will be applied to all actions except these.
|
118
|
-
`&block` | Block will be called in the context of the action and will be given the current relation and evaluated value.
|
119
|
+
`:required` | Calls `#none` on the model if parameter is absent (blank or nil) and there's no default value set.
|
120
|
+
`:only` | String, Symbol or an Array of those. The scope will **only** be applied to these actions.
|
121
|
+
`:except` | String, Symbol or an Array of those. The scope will be applied to all actions **except** these.
|
122
|
+
`&block` | Block will be called in the context of the controller's action and will be given two parameters: the current relation and evaluated value.
|
119
123
|
|
120
124
|
## License
|
121
125
|
|
122
126
|
See [LICENSE](LICENSE).
|
123
|
-
|
124
|
-
## Contributing
|
125
|
-
|
126
|
-
1. Fork it ( https://github.com/[my-github-username]/scopable/fork )
|
127
|
-
2. Create your feature branch (`git checkout -b my-new-feature`)
|
128
|
-
3. Commit your changes (`git commit -am 'Add some feature'`)
|
129
|
-
4. Push to the branch (`git push origin my-new-feature`)
|
130
|
-
5. Create a new Pull Request
|
data/lib/scopable/version.rb
CHANGED
data/scopable.gemspec
CHANGED
@@ -9,8 +9,8 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ['Arthur Corenzan']
|
10
10
|
spec.email = ['arthur@corenzan.com']
|
11
11
|
spec.summary = %q{Apply or skip model scopes based on options and request parameters.}
|
12
|
-
|
13
|
-
spec.homepage = 'https://github.com/
|
12
|
+
spec.description = %q{}
|
13
|
+
spec.homepage = 'https://github.com/corenzan/scopable'
|
14
14
|
spec.license = 'MIT'
|
15
15
|
|
16
16
|
spec.files = `git ls-files -z`.split("\x0")
|
@@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
|
|
20
20
|
|
21
21
|
spec.required_ruby_version = '>= 2.2.2'
|
22
22
|
|
23
|
-
spec.add_runtime_dependency 'activesupport', '>= 3.2'
|
23
|
+
spec.add_runtime_dependency 'activesupport', '>= 3.2'
|
24
24
|
|
25
25
|
spec.add_development_dependency 'bundler', '~> 1.7'
|
26
26
|
spec.add_development_dependency 'rake', '~> 10.0'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: scopable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Arthur Corenzan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-04-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -17,9 +17,6 @@ dependencies:
|
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '3.2'
|
20
|
-
- - "<="
|
21
|
-
- !ruby/object:Gem::Version
|
22
|
-
version: 5.0.0
|
23
20
|
type: :runtime
|
24
21
|
prerelease: false
|
25
22
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -27,9 +24,6 @@ dependencies:
|
|
27
24
|
- - ">="
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: '3.2'
|
30
|
-
- - "<="
|
31
|
-
- !ruby/object:Gem::Version
|
32
|
-
version: 5.0.0
|
33
27
|
- !ruby/object:Gem::Dependency
|
34
28
|
name: bundler
|
35
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -72,7 +66,7 @@ dependencies:
|
|
72
66
|
- - "~>"
|
73
67
|
- !ruby/object:Gem::Version
|
74
68
|
version: '3.5'
|
75
|
-
description:
|
69
|
+
description: ''
|
76
70
|
email:
|
77
71
|
- arthur@corenzan.com
|
78
72
|
executables: []
|
@@ -95,7 +89,7 @@ files:
|
|
95
89
|
- spec/spec_helper.rb
|
96
90
|
- spec/support/controller.rb
|
97
91
|
- spec/support/model.rb
|
98
|
-
homepage: https://github.com/
|
92
|
+
homepage: https://github.com/corenzan/scopable
|
99
93
|
licenses:
|
100
94
|
- MIT
|
101
95
|
metadata: {}
|