scoped_from 0.8.0 → 0.8.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +1 -1
- data/Gemfile.lock +18 -18
- data/README.mdown +17 -17
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/lib/scoped_from.rb +1 -1
- data/lib/scoped_from/active_record.rb +1 -1
- data/lib/scoped_from/query.rb +2 -2
- data/scoped_from.gemspec +1 -1
- data/spec/mocks/comment.rb +1 -1
- data/spec/mocks/comment_query.rb +1 -1
- data/spec/mocks/post.rb +1 -1
- data/spec/mocks/user.rb +1 -1
- data/spec/mocks/user_query.rb +1 -1
- data/spec/mocks/vote.rb +1 -1
- data/spec/mocks/vote_query.rb +1 -1
- data/spec/scoped_from/query_spec.rb +28 -28
- data/spec/spec_helper.rb +1 -1
- data/spec/support/bootsrap/database.rb +1 -1
- data/spec/support/macros/user_macro.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 420ecbe54d29a250ed5827afd36cb68f00658acd
|
4
|
+
data.tar.gz: 70718fa6858992001f51ac154f512ff5e87a0bc6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 478022cae387bc459a21330454c4cb9dad0a0355686e4c3ec0c07be1b8e3f4ab9a2a457952771cb326536a698d4dc443db4245138c8f43dd51feabeeacfdfe9a
|
7
|
+
data.tar.gz: f603d42533c9685f23996e114e7995cd1407d8eb1cf5b4a623d5ef633d398eb8e3087ea3eabd31a5e8a03c3615d4dd2216b2f555a887e8044397e777c02459a1
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,50 +1,50 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
scoped_from (0.8.
|
4
|
+
scoped_from (0.8.1)
|
5
5
|
activerecord (~> 4.0.0)
|
6
6
|
activesupport (~> 4.0.0)
|
7
7
|
|
8
8
|
GEM
|
9
9
|
remote: https://rubygems.org/
|
10
10
|
specs:
|
11
|
-
activemodel (4.0.
|
12
|
-
activesupport (= 4.0.
|
11
|
+
activemodel (4.0.2)
|
12
|
+
activesupport (= 4.0.2)
|
13
13
|
builder (~> 3.1.0)
|
14
|
-
activerecord (4.0.
|
15
|
-
activemodel (= 4.0.
|
14
|
+
activerecord (4.0.2)
|
15
|
+
activemodel (= 4.0.2)
|
16
16
|
activerecord-deprecated_finders (~> 1.0.2)
|
17
|
-
activesupport (= 4.0.
|
17
|
+
activesupport (= 4.0.2)
|
18
18
|
arel (~> 4.0.0)
|
19
19
|
activerecord-deprecated_finders (1.0.3)
|
20
|
-
activesupport (4.0.
|
20
|
+
activesupport (4.0.2)
|
21
21
|
i18n (~> 0.6, >= 0.6.4)
|
22
22
|
minitest (~> 4.2)
|
23
23
|
multi_json (~> 1.3)
|
24
24
|
thread_safe (~> 0.1)
|
25
25
|
tzinfo (~> 0.3.37)
|
26
|
-
arel (4.0.
|
27
|
-
atomic (1.1.
|
26
|
+
arel (4.0.1)
|
27
|
+
atomic (1.1.14)
|
28
28
|
builder (3.1.4)
|
29
|
-
diff-lcs (1.2.
|
30
|
-
i18n (0.6.
|
29
|
+
diff-lcs (1.2.5)
|
30
|
+
i18n (0.6.9)
|
31
31
|
minitest (4.7.5)
|
32
|
-
multi_json (1.
|
32
|
+
multi_json (1.8.2)
|
33
33
|
rake (10.1.0)
|
34
34
|
rspec (2.14.1)
|
35
35
|
rspec-core (~> 2.14.0)
|
36
36
|
rspec-expectations (~> 2.14.0)
|
37
37
|
rspec-mocks (~> 2.14.0)
|
38
|
-
rspec-core (2.14.
|
39
|
-
rspec-expectations (2.14.
|
38
|
+
rspec-core (2.14.7)
|
39
|
+
rspec-expectations (2.14.4)
|
40
40
|
diff-lcs (>= 1.1.3, < 2.0)
|
41
|
-
rspec-mocks (2.14.
|
42
|
-
sqlite3 (1.3.
|
41
|
+
rspec-mocks (2.14.4)
|
42
|
+
sqlite3 (1.3.8)
|
43
43
|
sqlite3-ruby (1.3.3)
|
44
44
|
sqlite3 (>= 1.3.3)
|
45
|
-
thread_safe (0.1.
|
45
|
+
thread_safe (0.1.3)
|
46
46
|
atomic
|
47
|
-
tzinfo (0.3.
|
47
|
+
tzinfo (0.3.38)
|
48
48
|
|
49
49
|
PLATFORMS
|
50
50
|
ruby
|
data/README.mdown
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# ScopedFrom
|
2
2
|
|
3
3
|
Provides a simple mapping between scopes and controller parameters for
|
4
|
-
[Ruby On Rails
|
4
|
+
[Ruby On Rails 4](http://rubyonrails.org/).
|
5
5
|
|
6
6
|
## Installation
|
7
7
|
|
@@ -16,35 +16,35 @@ Then, just run `bundle install`.
|
|
16
16
|
First, a model with some scopes:
|
17
17
|
|
18
18
|
class Post < ActiveRecord::Base
|
19
|
-
|
19
|
+
|
20
20
|
scope :commented, where('comments_count > 0')
|
21
21
|
|
22
22
|
scope :created_between, lambda { |after, before|
|
23
23
|
where('created_at >= ? AND created_at <= ?', after, before)
|
24
24
|
}
|
25
|
-
|
25
|
+
|
26
26
|
scope :search, lambda { |pattern|
|
27
27
|
where('body LIKE ?', "%#{pattern}%")
|
28
28
|
}
|
29
|
-
|
29
|
+
|
30
30
|
scope :with_category, lambda { |category_id|
|
31
31
|
where(:category_id, category_id)
|
32
32
|
}
|
33
|
-
|
33
|
+
|
34
34
|
end
|
35
|
-
|
35
|
+
|
36
36
|
After, a controller:
|
37
|
-
|
37
|
+
|
38
38
|
class PostsController < ActionController::Base
|
39
|
-
|
39
|
+
|
40
40
|
def index
|
41
41
|
@posts = Post.scoped_from(params)
|
42
42
|
end
|
43
|
-
|
43
|
+
|
44
44
|
end
|
45
|
-
|
45
|
+
|
46
46
|
Then, it just filter your model from params:
|
47
|
-
|
47
|
+
|
48
48
|
/posts?commented=1
|
49
49
|
/posts?search=rails
|
50
50
|
/posts?search=rails&commented=1&with_category=42
|
@@ -77,7 +77,7 @@ If you need to map an SQL order, just pass `order` parameter:
|
|
77
77
|
@posts = Post.scoped_from(order: 'created_at')
|
78
78
|
|
79
79
|
Order direction can be specified using a dot, space or `:` as delimiter:
|
80
|
-
|
80
|
+
|
81
81
|
@posts = Post.scoped_from(order: 'created_at.desc')
|
82
82
|
|
83
83
|
Note that order is SQL safe with `scoped_from` method (columns names are
|
@@ -89,9 +89,9 @@ If your provide an array as parameter value, scope is invoked with each item
|
|
89
89
|
of the array:
|
90
90
|
|
91
91
|
@posts = Post.scoped_from(search: ['bar', 'foo'])
|
92
|
-
|
92
|
+
|
93
93
|
is equivalent to
|
94
|
-
|
94
|
+
|
95
95
|
@posts = Post.search('bar').search('foo')
|
96
96
|
|
97
97
|
You may also not want to filter on columns, just specify `:exclude_columns`
|
@@ -118,15 +118,15 @@ subclass of `ScopedFrom::Query` named `#{RecordClassName}Query`. Here is an
|
|
118
118
|
example:
|
119
119
|
|
120
120
|
class PostQuery < ScopedFrom::Query
|
121
|
-
|
121
|
+
|
122
122
|
def category
|
123
123
|
Category.find_by_id(params[:with_category]) if params[:with_category]
|
124
124
|
end
|
125
|
-
|
125
|
+
|
126
126
|
end
|
127
127
|
|
128
128
|
This class has to be in load path.
|
129
|
-
|
129
|
+
|
130
130
|
Then into a view:
|
131
131
|
|
132
132
|
<% if @query.category %>
|
data/Rakefile
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.8.
|
1
|
+
0.8.1
|
data/lib/scoped_from.rb
CHANGED
data/lib/scoped_from/query.rb
CHANGED
@@ -110,7 +110,7 @@ module ScopedFrom
|
|
110
110
|
|
111
111
|
def parse_orders(values)
|
112
112
|
[].tap do |orders|
|
113
|
-
values.
|
113
|
+
values.each do |value|
|
114
114
|
order = parse_order(value)
|
115
115
|
orders << order if order.present? && !orders.any? { |o| o[:column] == order[:column] }
|
116
116
|
end
|
@@ -127,4 +127,4 @@ module ScopedFrom
|
|
127
127
|
|
128
128
|
end
|
129
129
|
|
130
|
-
end
|
130
|
+
end
|
data/scoped_from.gemspec
CHANGED
@@ -6,7 +6,7 @@ Gem::Specification.new do |s|
|
|
6
6
|
s.email = 'al@alweb.org'
|
7
7
|
s.homepage = 'https://github.com/alexistoulotte/scoped_from'
|
8
8
|
s.summary = 'Mapping between scopes and parameters for Rails'
|
9
|
-
s.description = 'Provides a simple mapping between Active Record scopes and controller parameters for Ruby On Rails
|
9
|
+
s.description = 'Provides a simple mapping between Active Record scopes and controller parameters for Ruby On Rails 4'
|
10
10
|
s.license = 'MIT'
|
11
11
|
|
12
12
|
s.rubyforge_project = 'scoped_from'
|
data/spec/mocks/comment.rb
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
class Comment < ActiveRecord::Base
|
2
|
-
end
|
2
|
+
end
|
data/spec/mocks/comment_query.rb
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
class CommentQuery < ScopedFrom::Query
|
2
|
-
end
|
2
|
+
end
|
data/spec/mocks/post.rb
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
class Post < ActiveRecord::Base
|
2
|
-
end
|
2
|
+
end
|
data/spec/mocks/user.rb
CHANGED
data/spec/mocks/user_query.rb
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
class UserQuery < String
|
2
|
-
end
|
2
|
+
end
|
data/spec/mocks/vote.rb
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
class Vote < ActiveRecord::Base
|
2
|
-
end
|
2
|
+
end
|
data/spec/mocks/vote_query.rb
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
module VoteQuery
|
2
|
-
end
|
2
|
+
end
|
@@ -171,17 +171,17 @@ describe ScopedFrom::Query do
|
|
171
171
|
describe '#params' do
|
172
172
|
|
173
173
|
it 'returns params specified at initialization' do
|
174
|
-
query(User, :
|
174
|
+
query(User, search: 'foo', 'enabled' => true).params.should == { 'search' => 'foo', 'enabled' => true }
|
175
175
|
end
|
176
176
|
|
177
177
|
it 'returns an hash with indifferent access' do
|
178
178
|
query(User, 'search' => 'bar').params.should be_a(ActiveSupport::HashWithIndifferentAccess)
|
179
179
|
query(User, 'search' => 'bar').params[:search].should == 'bar'
|
180
|
-
query(User, :
|
180
|
+
query(User, search: 'bar').params['search'].should == 'bar'
|
181
181
|
end
|
182
182
|
|
183
183
|
it 'can be converted to query string' do
|
184
|
-
query(User, :
|
184
|
+
query(User, search: ['foo', 'bar'], 'enabled' => '1').params.to_query.should == 'enabled=true&search%5B%5D=foo&search%5B%5D=bar'
|
185
185
|
end
|
186
186
|
|
187
187
|
end
|
@@ -193,7 +193,7 @@ describe ScopedFrom::Query do
|
|
193
193
|
end
|
194
194
|
|
195
195
|
it 'removes values that are not scopes' do
|
196
|
-
query(User, :
|
196
|
+
query(User, foo: 'bar', 'search' => 'foo', enabled: true).params.should == { 'search' => 'foo', 'enabled' => true }
|
197
197
|
end
|
198
198
|
|
199
199
|
it 'is case sensitive' do
|
@@ -293,16 +293,16 @@ describe ScopedFrom::Query do
|
|
293
293
|
|
294
294
|
it 'includes column values' do
|
295
295
|
query(User, 'firstname' => 'Jane', 'foo' => 'bar').params.should == { 'firstname' => 'Jane' }
|
296
|
-
query(User, :
|
296
|
+
query(User, firstname: 'Jane', 'foo' => 'bar').params.should == { 'firstname' => 'Jane' }
|
297
297
|
end
|
298
298
|
|
299
299
|
it 'exclude column values if :exclude_columns option is specified' do
|
300
|
-
query(User, { :
|
301
|
-
query(User, { :
|
300
|
+
query(User, { enabled: true, 'firstname' => 'Jane', 'foo' => 'bar' }, exclude_columns: true).params.should == { 'enabled' => true }
|
301
|
+
query(User, { enabled: true, firstname: 'Jane', foo: 'bar' }, exclude_columns: true).params.should == { 'enabled' => true }
|
302
302
|
end
|
303
303
|
|
304
304
|
it 'scopes have priority on columns' do
|
305
|
-
query(User, :
|
305
|
+
query(User, enabled: false).params.should == {}
|
306
306
|
end
|
307
307
|
|
308
308
|
it 'maps an "order"' do
|
@@ -331,7 +331,7 @@ describe ScopedFrom::Query do
|
|
331
331
|
end
|
332
332
|
|
333
333
|
it 'order can be specified as symbol' do
|
334
|
-
query(User, { :
|
334
|
+
query(User, { order: 'firstname.desc' }).params.should == { 'order' => 'firstname.desc' }
|
335
335
|
end
|
336
336
|
|
337
337
|
it "order is case sensitive" do
|
@@ -339,10 +339,10 @@ describe ScopedFrom::Query do
|
|
339
339
|
end
|
340
340
|
|
341
341
|
it 'many order can be specified' do
|
342
|
-
query(User, { 'order' => ['firstname.Asc', 'lastname.DESC'] }).params.should == { 'order' => ['
|
343
|
-
query(User, { 'order' => ['firstname.Asc', 'firstname.desc'] }).params.should == { 'order' => 'firstname.
|
344
|
-
query(User, { 'order' => ['firstname.Asc', 'lastname.DESC', 'firstname.desc'] }).params.should == { 'order' => ['firstname.
|
345
|
-
query(User, { 'order' => ['firstname.Asc', 'foo', 'lastname.DESC', 'firstname.desc'] }).params.should == { 'order' => ['firstname.
|
342
|
+
query(User, { 'order' => ['firstname.Asc', 'lastname.DESC'] }).params.should == { 'order' => ['firstname.asc', 'lastname.desc'] }
|
343
|
+
query(User, { 'order' => ['firstname.Asc', 'firstname.desc'] }).params.should == { 'order' => 'firstname.asc' }
|
344
|
+
query(User, { 'order' => ['firstname.Asc', 'lastname.DESC', 'firstname.desc'] }).params.should == { 'order' => ['firstname.asc', 'lastname.desc'] }
|
345
|
+
query(User, { 'order' => ['firstname.Asc', 'foo', 'lastname.DESC', 'firstname.desc'] }).params.should == { 'order' => ['firstname.asc', 'lastname.desc'] }
|
346
346
|
end
|
347
347
|
|
348
348
|
it 'order can be delimited by a space' do
|
@@ -368,18 +368,18 @@ describe ScopedFrom::Query do
|
|
368
368
|
|
369
369
|
it 'does not execute any query' do
|
370
370
|
User.should_not_receive(:connection)
|
371
|
-
query(User, :
|
371
|
+
query(User, enabled: true).relation
|
372
372
|
end
|
373
373
|
|
374
374
|
it 'works with scopes with a lambda without arguments' do
|
375
375
|
users(:jane).update_attribute(:created_at, 10.days.ago)
|
376
|
-
query(User, :
|
377
|
-
query(User, :
|
376
|
+
query(User, latest: true).relation.should == [users(:john)]
|
377
|
+
query(User, latest: false).relation.should == [users(:john), users(:jane)]
|
378
378
|
end
|
379
379
|
|
380
380
|
it 'does not modify relation specified at initialization' do
|
381
381
|
relation = User.search('foo')
|
382
|
-
q = query(relation, :
|
382
|
+
q = query(relation, enabled: true)
|
383
383
|
expect {
|
384
384
|
expect {
|
385
385
|
q.relation
|
@@ -393,27 +393,27 @@ describe ScopedFrom::Query do
|
|
393
393
|
end
|
394
394
|
|
395
395
|
it 'invokes many times relation if an array is given' do
|
396
|
-
query(User, :
|
397
|
-
query(User, :
|
398
|
-
query(User, :
|
396
|
+
query(User, search: ['John', 'Doe']).relation.should == [users(:john)]
|
397
|
+
query(User, search: ['John', 'Done']).relation.should == []
|
398
|
+
query(User, search: ['John', 'Doe']).params.should == { 'search' => ['John', 'Doe'] }
|
399
399
|
end
|
400
400
|
|
401
401
|
it 'invokes many times relation if given twice (as string & symbol)' do
|
402
|
-
query(User, :
|
403
|
-
query(User, :
|
402
|
+
query(User, search: 'John', 'search' => 'Done').params['search'].size.should be(2)
|
403
|
+
query(User, search: 'John', 'search' => 'Done').params['search'].should include('John', 'Done')
|
404
404
|
|
405
405
|
|
406
|
-
query(User, :
|
407
|
-
query(User, :
|
406
|
+
query(User, search: 'John', 'search' => ['Did', 'Done']).params['search'].size.should be(3)
|
407
|
+
query(User, search: 'John', 'search' => ['Did', 'Done']).params['search'].should include('John', 'Did', 'Done')
|
408
408
|
end
|
409
409
|
|
410
410
|
it 'invokes last order if an array is given' do
|
411
411
|
create_user(:jane2, firstname: 'Jane', lastname: 'Zoe')
|
412
412
|
|
413
|
-
query(User, :
|
414
|
-
query(User, :
|
415
|
-
query(User, :
|
416
|
-
query(User, :
|
413
|
+
query(User, order: ['lastname', 'firstname']).relation.should == [users(:jane), users(:john), users(:jane2)]
|
414
|
+
query(User, order: ['lastname', 'firstname.desc']).relation.should == [users(:john), users(:jane), users(:jane2)]
|
415
|
+
query(User, order: ['firstname', 'lastname.desc']).relation.should == [users(:jane2), users(:jane), users(:john)]
|
416
|
+
query(User, order: ['firstname.desc', 'lastname']).relation.order_values.should == [{ 'firstname' => :desc }, { 'lastname' => :asc }]
|
417
417
|
end
|
418
418
|
|
419
419
|
it 'defines #query method on returned relation' do
|
data/spec/spec_helper.rb
CHANGED
@@ -9,4 +9,4 @@ ActiveRecord::Base.connection.create_table(:users, force: true) do |t|
|
|
9
9
|
t.boolean :admin, null: false, default: false
|
10
10
|
t.timestamps
|
11
11
|
end
|
12
|
-
ActiveRecord::Base.connection.create_table(:votes, force: true)
|
12
|
+
ActiveRecord::Base.connection.create_table(:votes, force: true)
|
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.8.
|
4
|
+
version: 0.8.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexis Toulotte
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-12-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -81,7 +81,7 @@ dependencies:
|
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: 1.3.0
|
83
83
|
description: Provides a simple mapping between Active Record scopes and controller
|
84
|
-
parameters for Ruby On Rails
|
84
|
+
parameters for Ruby On Rails 4
|
85
85
|
email: al@alweb.org
|
86
86
|
executables: []
|
87
87
|
extensions: []
|
@@ -132,7 +132,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
132
132
|
version: '0'
|
133
133
|
requirements: []
|
134
134
|
rubyforge_project: scoped_from
|
135
|
-
rubygems_version: 2.0.
|
135
|
+
rubygems_version: 2.0.14
|
136
136
|
signing_key:
|
137
137
|
specification_version: 4
|
138
138
|
summary: Mapping between scopes and parameters for Rails
|