scoped_from 0.9.0 → 1.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 +4 -4
- data/.gitignore +5 -5
- data/README.mdown +71 -41
- data/VERSION +1 -1
- data/lib/scoped_from/active_record.rb +1 -1
- data/scoped_from.gemspec +5 -5
- metadata +17 -17
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 4b56a3a6c6ec63c95e922a48d886843747725f4a
|
|
4
|
+
data.tar.gz: 44a3f95dcb98a946af1b6f20f0319f23fd372595
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3d1153e12c34cac02f2e43e2e73245602c9b06395e8077b50662edbc1fdfcabfd2d17c29cfa37a687cb6565fa74dac3fea9755e77d13e284a1078fc130e7eded
|
|
7
|
+
data.tar.gz: 8e349b84d70e4b3ab0f492d0689392b366290e85276e882a0e5d52b927a2ad1f622e3ca463aab0cc9fb0a9dc40259153123cbe6ea2c3c10b8e1689204e03a4c4
|
data/.gitignore
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
.DS_Store
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
Gemfile.lock
|
|
5
|
-
pkg
|
|
6
|
-
spec/test.sqlite3
|
|
2
|
+
/.bundle/
|
|
3
|
+
/.ruby-version
|
|
4
|
+
/Gemfile.lock
|
|
5
|
+
/pkg/
|
|
6
|
+
/spec/test.sqlite3
|
data/README.mdown
CHANGED
|
@@ -7,7 +7,9 @@ Provides a simple mapping between scopes and controller parameters for
|
|
|
7
7
|
|
|
8
8
|
Just add this into your `Gemfile`:
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
```ruby
|
|
11
|
+
gem 'scoped_from'
|
|
12
|
+
```
|
|
11
13
|
|
|
12
14
|
Then, just run `bundle install`.
|
|
13
15
|
|
|
@@ -15,39 +17,45 @@ Then, just run `bundle install`.
|
|
|
15
17
|
|
|
16
18
|
First, a model with some scopes:
|
|
17
19
|
|
|
18
|
-
|
|
20
|
+
```ruby
|
|
21
|
+
class Post < ActiveRecord::Base
|
|
19
22
|
|
|
20
|
-
|
|
23
|
+
scope :commented, where('comments_count > 0')
|
|
21
24
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
+
scope :created_between, lambda { |after, before|
|
|
26
|
+
where('created_at >= ? AND created_at <= ?', after, before)
|
|
27
|
+
}
|
|
25
28
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
+
scope :search, lambda { |pattern|
|
|
30
|
+
where('body LIKE ?', "%#{pattern}%")
|
|
31
|
+
}
|
|
29
32
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
+
scope :with_category, lambda { |category_id|
|
|
34
|
+
where(:category_id, category_id)
|
|
35
|
+
}
|
|
33
36
|
|
|
34
|
-
|
|
37
|
+
end
|
|
38
|
+
```
|
|
35
39
|
|
|
36
40
|
After, a controller:
|
|
37
41
|
|
|
38
|
-
|
|
42
|
+
```ruby
|
|
43
|
+
class PostsController < ActionController::Base
|
|
39
44
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
45
|
+
def index
|
|
46
|
+
@posts = Post.scoped_from(params)
|
|
47
|
+
end
|
|
43
48
|
|
|
44
|
-
|
|
49
|
+
end
|
|
50
|
+
```
|
|
45
51
|
|
|
46
52
|
Then, it just filter your model from params:
|
|
47
53
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
54
|
+
```
|
|
55
|
+
/posts?commented=1
|
|
56
|
+
/posts?search=rails
|
|
57
|
+
/posts?search=rails&commented=1&with_category=42
|
|
58
|
+
```
|
|
51
59
|
|
|
52
60
|
## Accepted scopes
|
|
53
61
|
|
|
@@ -64,21 +72,29 @@ Columns are also automatically scoped.
|
|
|
64
72
|
|
|
65
73
|
You can restrict mapping to some scopes with `:only` option:
|
|
66
74
|
|
|
67
|
-
|
|
75
|
+
```ruby
|
|
76
|
+
@posts = Post.scoped_from(params, only: ['commented', 'search'])
|
|
77
|
+
```
|
|
68
78
|
|
|
69
79
|
You can also exclude some scopes from mapping with `:except` option:
|
|
70
80
|
|
|
71
|
-
|
|
81
|
+
```ruby
|
|
82
|
+
@posts = Post.scoped_from(params, except: 'commented')
|
|
83
|
+
```
|
|
72
84
|
|
|
73
85
|
## Mapping order
|
|
74
86
|
|
|
75
87
|
If you need to map an SQL order, just pass `order` parameter:
|
|
76
88
|
|
|
77
|
-
|
|
89
|
+
```ruby
|
|
90
|
+
@posts = Post.scoped_from(order: 'created_at')
|
|
91
|
+
```
|
|
78
92
|
|
|
79
93
|
Order direction can be specified using a dot, space or `:` as delimiter:
|
|
80
94
|
|
|
81
|
-
|
|
95
|
+
```ruby
|
|
96
|
+
@posts = Post.scoped_from(order: 'created_at.desc')
|
|
97
|
+
```
|
|
82
98
|
|
|
83
99
|
Note that order is SQL safe with `scoped_from` method (columns names are
|
|
84
100
|
checked).
|
|
@@ -88,26 +104,36 @@ checked).
|
|
|
88
104
|
If your provide an array as parameter value, scope is invoked with each item
|
|
89
105
|
of the array:
|
|
90
106
|
|
|
91
|
-
|
|
107
|
+
```ruby
|
|
108
|
+
@posts = Post.scoped_from(search: ['bar', 'foo'])
|
|
109
|
+
```
|
|
92
110
|
|
|
93
111
|
is equivalent to
|
|
94
112
|
|
|
95
|
-
|
|
113
|
+
```ruby
|
|
114
|
+
@posts = Post.search('bar').search('foo')
|
|
115
|
+
```
|
|
96
116
|
|
|
97
117
|
You may also not want to filter on columns, just specify `:exclude_columns`
|
|
98
118
|
option:
|
|
99
119
|
|
|
100
|
-
|
|
120
|
+
```ruby
|
|
121
|
+
@posts = Post.scoped_from(params, exclude_columns: true)
|
|
122
|
+
```
|
|
101
123
|
|
|
102
124
|
A query string can also be given to `scoped_from` method:
|
|
103
125
|
|
|
104
|
-
|
|
126
|
+
```ruby
|
|
127
|
+
@posts = Post.scoped_from('with_category=24&search[]=foo&search[]=bar')
|
|
128
|
+
```
|
|
105
129
|
|
|
106
130
|
Returned scope from `scoped_from` method gives access to an internal query
|
|
107
131
|
object:
|
|
108
132
|
|
|
109
|
-
|
|
110
|
-
|
|
133
|
+
```ruby
|
|
134
|
+
@posts = Post.scoped_from(params)
|
|
135
|
+
@query = @posts.query
|
|
136
|
+
```
|
|
111
137
|
|
|
112
138
|
This query provides you some convenience methods like `params`, `order_column`
|
|
113
139
|
and `order_direction`. This object can also be used to save user's search into
|
|
@@ -117,23 +143,27 @@ But, you may also have to subclass this query class. You have to create a
|
|
|
117
143
|
subclass of `ScopedFrom::Query` named `#{RecordClassName}Query`. Here is an
|
|
118
144
|
example:
|
|
119
145
|
|
|
120
|
-
|
|
146
|
+
```ruby
|
|
147
|
+
class PostQuery < ScopedFrom::Query
|
|
121
148
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
149
|
+
def category
|
|
150
|
+
Category.find_by_id(params[:with_category]) if params[:with_category]
|
|
151
|
+
end
|
|
125
152
|
|
|
126
|
-
|
|
153
|
+
end
|
|
154
|
+
```
|
|
127
155
|
|
|
128
156
|
This class has to be in load path.
|
|
129
157
|
|
|
130
158
|
Then into a view:
|
|
131
159
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
160
|
+
```erb
|
|
161
|
+
<% if @query.category %>
|
|
162
|
+
<p>All posts of category <%= @query.category.name %></p>
|
|
163
|
+
<% else %>
|
|
164
|
+
<p>All posts</p>
|
|
165
|
+
<% end %>
|
|
166
|
+
```
|
|
137
167
|
|
|
138
168
|
## Executing test suite
|
|
139
169
|
|
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
0.
|
|
1
|
+
1.0.0
|
data/scoped_from.gemspec
CHANGED
|
@@ -16,11 +16,11 @@ Gem::Specification.new do |s|
|
|
|
16
16
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
|
17
17
|
s.require_paths = ['lib']
|
|
18
18
|
|
|
19
|
-
s.add_dependency 'activerecord', '>=
|
|
20
|
-
s.add_dependency 'activesupport', '>=
|
|
19
|
+
s.add_dependency 'activerecord', '>= 5.0.0', '< 5.1.0'
|
|
20
|
+
s.add_dependency 'activesupport', '>= 5.0.0', '< 5.1.0'
|
|
21
21
|
|
|
22
|
-
s.add_development_dependency 'byebug', '>= 3.2.0', '<
|
|
23
|
-
s.add_development_dependency 'rake', '>= 10.3.0', '<
|
|
24
|
-
s.add_development_dependency 'rspec', '>= 3.1.0', '< 3.
|
|
22
|
+
s.add_development_dependency 'byebug', '>= 3.2.0', '< 10.0.0'
|
|
23
|
+
s.add_development_dependency 'rake', '>= 10.3.0', '< 12.0.0'
|
|
24
|
+
s.add_development_dependency 'rspec', '>= 3.1.0', '< 3.6.0'
|
|
25
25
|
s.add_development_dependency 'sqlite3-ruby', '>= 1.3.0', '< 1.4.0'
|
|
26
26
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: scoped_from
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 1.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Alexis Toulotte
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2016-08-10 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: activerecord
|
|
@@ -16,40 +16,40 @@ dependencies:
|
|
|
16
16
|
requirements:
|
|
17
17
|
- - ">="
|
|
18
18
|
- !ruby/object:Gem::Version
|
|
19
|
-
version:
|
|
19
|
+
version: 5.0.0
|
|
20
20
|
- - "<"
|
|
21
21
|
- !ruby/object:Gem::Version
|
|
22
|
-
version:
|
|
22
|
+
version: 5.1.0
|
|
23
23
|
type: :runtime
|
|
24
24
|
prerelease: false
|
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
|
26
26
|
requirements:
|
|
27
27
|
- - ">="
|
|
28
28
|
- !ruby/object:Gem::Version
|
|
29
|
-
version:
|
|
29
|
+
version: 5.0.0
|
|
30
30
|
- - "<"
|
|
31
31
|
- !ruby/object:Gem::Version
|
|
32
|
-
version:
|
|
32
|
+
version: 5.1.0
|
|
33
33
|
- !ruby/object:Gem::Dependency
|
|
34
34
|
name: activesupport
|
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
|
36
36
|
requirements:
|
|
37
37
|
- - ">="
|
|
38
38
|
- !ruby/object:Gem::Version
|
|
39
|
-
version:
|
|
39
|
+
version: 5.0.0
|
|
40
40
|
- - "<"
|
|
41
41
|
- !ruby/object:Gem::Version
|
|
42
|
-
version:
|
|
42
|
+
version: 5.1.0
|
|
43
43
|
type: :runtime
|
|
44
44
|
prerelease: false
|
|
45
45
|
version_requirements: !ruby/object:Gem::Requirement
|
|
46
46
|
requirements:
|
|
47
47
|
- - ">="
|
|
48
48
|
- !ruby/object:Gem::Version
|
|
49
|
-
version:
|
|
49
|
+
version: 5.0.0
|
|
50
50
|
- - "<"
|
|
51
51
|
- !ruby/object:Gem::Version
|
|
52
|
-
version:
|
|
52
|
+
version: 5.1.0
|
|
53
53
|
- !ruby/object:Gem::Dependency
|
|
54
54
|
name: byebug
|
|
55
55
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -59,7 +59,7 @@ dependencies:
|
|
|
59
59
|
version: 3.2.0
|
|
60
60
|
- - "<"
|
|
61
61
|
- !ruby/object:Gem::Version
|
|
62
|
-
version:
|
|
62
|
+
version: 10.0.0
|
|
63
63
|
type: :development
|
|
64
64
|
prerelease: false
|
|
65
65
|
version_requirements: !ruby/object:Gem::Requirement
|
|
@@ -69,7 +69,7 @@ dependencies:
|
|
|
69
69
|
version: 3.2.0
|
|
70
70
|
- - "<"
|
|
71
71
|
- !ruby/object:Gem::Version
|
|
72
|
-
version:
|
|
72
|
+
version: 10.0.0
|
|
73
73
|
- !ruby/object:Gem::Dependency
|
|
74
74
|
name: rake
|
|
75
75
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -79,7 +79,7 @@ dependencies:
|
|
|
79
79
|
version: 10.3.0
|
|
80
80
|
- - "<"
|
|
81
81
|
- !ruby/object:Gem::Version
|
|
82
|
-
version:
|
|
82
|
+
version: 12.0.0
|
|
83
83
|
type: :development
|
|
84
84
|
prerelease: false
|
|
85
85
|
version_requirements: !ruby/object:Gem::Requirement
|
|
@@ -89,7 +89,7 @@ dependencies:
|
|
|
89
89
|
version: 10.3.0
|
|
90
90
|
- - "<"
|
|
91
91
|
- !ruby/object:Gem::Version
|
|
92
|
-
version:
|
|
92
|
+
version: 12.0.0
|
|
93
93
|
- !ruby/object:Gem::Dependency
|
|
94
94
|
name: rspec
|
|
95
95
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -99,7 +99,7 @@ dependencies:
|
|
|
99
99
|
version: 3.1.0
|
|
100
100
|
- - "<"
|
|
101
101
|
- !ruby/object:Gem::Version
|
|
102
|
-
version: 3.
|
|
102
|
+
version: 3.6.0
|
|
103
103
|
type: :development
|
|
104
104
|
prerelease: false
|
|
105
105
|
version_requirements: !ruby/object:Gem::Requirement
|
|
@@ -109,7 +109,7 @@ dependencies:
|
|
|
109
109
|
version: 3.1.0
|
|
110
110
|
- - "<"
|
|
111
111
|
- !ruby/object:Gem::Version
|
|
112
|
-
version: 3.
|
|
112
|
+
version: 3.6.0
|
|
113
113
|
- !ruby/object:Gem::Dependency
|
|
114
114
|
name: sqlite3-ruby
|
|
115
115
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -181,7 +181,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
181
181
|
version: '0'
|
|
182
182
|
requirements: []
|
|
183
183
|
rubyforge_project: scoped_from
|
|
184
|
-
rubygems_version: 2.
|
|
184
|
+
rubygems_version: 2.5.1
|
|
185
185
|
signing_key:
|
|
186
186
|
specification_version: 4
|
|
187
187
|
summary: Mapping between scopes and parameters for Rails
|