scoped_from 0.7.0 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +27 -25
- data/README.mdown +6 -6
- data/VERSION +1 -1
- data/lib/scoped_from/active_record.rb +2 -2
- data/lib/scoped_from/query.rb +40 -32
- data/scoped_from.gemspec +5 -5
- data/spec/mocks/user.rb +4 -10
- data/spec/scoped_from/active_record_spec.rb +1 -1
- data/spec/scoped_from/query_spec.rb +89 -68
- metadata +10 -23
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4289bd8c3bbfa67e32434e234a812d6c7355722d
|
4
|
+
data.tar.gz: f8f519eba2ed19382bd26dac18ed29235b2f2f36
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d8b4baaf4d2624f44422b79bf2fe124e7f8f21dcfd9d097638f97e7b4832135eef8bcdf70d803317acb3a5f59dffc7de73f05747718d161473e3e7853f524afd
|
7
|
+
data.tar.gz: 5921566963a8c2e17538acd47b5a91f1fe4007405f0297f2310d67341c2a361fa0d4a7f550f35434fe9f1d2b4e7148ba60b135dcb9c9fb17aa78f114b2872233
|
data/Gemfile.lock
CHANGED
@@ -1,35 +1,36 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
scoped_from (0.
|
5
|
-
activerecord (~>
|
6
|
-
activesupport (~>
|
4
|
+
scoped_from (0.8.0)
|
5
|
+
activerecord (~> 4.0.0)
|
6
|
+
activesupport (~> 4.0.0)
|
7
7
|
|
8
8
|
GEM
|
9
9
|
remote: https://rubygems.org/
|
10
10
|
specs:
|
11
|
-
activemodel (
|
12
|
-
activesupport (=
|
13
|
-
builder (~> 3.
|
14
|
-
activerecord (
|
15
|
-
activemodel (=
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
11
|
+
activemodel (4.0.0)
|
12
|
+
activesupport (= 4.0.0)
|
13
|
+
builder (~> 3.1.0)
|
14
|
+
activerecord (4.0.0)
|
15
|
+
activemodel (= 4.0.0)
|
16
|
+
activerecord-deprecated_finders (~> 1.0.2)
|
17
|
+
activesupport (= 4.0.0)
|
18
|
+
arel (~> 4.0.0)
|
19
|
+
activerecord-deprecated_finders (1.0.3)
|
20
|
+
activesupport (4.0.0)
|
21
|
+
i18n (~> 0.6, >= 0.6.4)
|
22
|
+
minitest (~> 4.2)
|
23
|
+
multi_json (~> 1.3)
|
24
|
+
thread_safe (~> 0.1)
|
25
|
+
tzinfo (~> 0.3.37)
|
26
|
+
arel (4.0.0)
|
27
|
+
atomic (1.1.10)
|
28
|
+
builder (3.1.4)
|
29
29
|
diff-lcs (1.2.4)
|
30
|
-
i18n (0.6.
|
30
|
+
i18n (0.6.4)
|
31
|
+
minitest (4.7.5)
|
31
32
|
multi_json (1.7.7)
|
32
|
-
rake (10.0
|
33
|
+
rake (10.1.0)
|
33
34
|
rspec (2.14.1)
|
34
35
|
rspec-core (~> 2.14.0)
|
35
36
|
rspec-expectations (~> 2.14.0)
|
@@ -41,14 +42,15 @@ GEM
|
|
41
42
|
sqlite3 (1.3.7)
|
42
43
|
sqlite3-ruby (1.3.3)
|
43
44
|
sqlite3 (>= 1.3.3)
|
45
|
+
thread_safe (0.1.0)
|
46
|
+
atomic
|
44
47
|
tzinfo (0.3.37)
|
45
48
|
|
46
49
|
PLATFORMS
|
47
50
|
ruby
|
48
51
|
|
49
52
|
DEPENDENCIES
|
50
|
-
|
51
|
-
rake (~> 10.0.0)
|
53
|
+
rake (~> 10.1.0)
|
52
54
|
rspec (~> 2.14.0)
|
53
55
|
scoped_from!
|
54
56
|
sqlite3-ruby (~> 1.3.0)
|
data/README.mdown
CHANGED
@@ -64,21 +64,21 @@ Columns are also automatically scoped.
|
|
64
64
|
|
65
65
|
You can restrict mapping to some scopes with `:only` option:
|
66
66
|
|
67
|
-
@posts = Post.scoped_from(params, :
|
67
|
+
@posts = Post.scoped_from(params, only: ['commented', 'search'])
|
68
68
|
|
69
69
|
You can also exclude some scopes from mapping with `:except` option:
|
70
70
|
|
71
|
-
@posts = Post.scoped_from(params, :
|
71
|
+
@posts = Post.scoped_from(params, except: 'commented')
|
72
72
|
|
73
73
|
## Mapping order
|
74
74
|
|
75
75
|
If you need to map an SQL order, just pass `order` parameter:
|
76
76
|
|
77
|
-
@posts = Post.scoped_from(:
|
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
|
-
@posts = Post.scoped_from(:
|
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
|
84
84
|
checked).
|
@@ -88,7 +88,7 @@ checked).
|
|
88
88
|
If your provide an array as parameter value, scope is invoked with each item
|
89
89
|
of the array:
|
90
90
|
|
91
|
-
@posts = Post.scoped_from(:
|
91
|
+
@posts = Post.scoped_from(search: ['bar', 'foo'])
|
92
92
|
|
93
93
|
is equivalent to
|
94
94
|
|
@@ -97,7 +97,7 @@ is equivalent to
|
|
97
97
|
You may also not want to filter on columns, just specify `:exclude_columns`
|
98
98
|
option:
|
99
99
|
|
100
|
-
@posts = Post.scoped_from(params, :
|
100
|
+
@posts = Post.scoped_from(params, exclude_columns: true)
|
101
101
|
|
102
102
|
A query string can also be given to `scoped_from` method:
|
103
103
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.8.0
|
@@ -21,13 +21,13 @@ module ScopedFrom
|
|
21
21
|
end
|
22
22
|
|
23
23
|
def scope_without_argument?(name)
|
24
|
-
|
24
|
+
scope_arities[name] == 0
|
25
25
|
end
|
26
26
|
|
27
27
|
def scoped_from(params, options = {})
|
28
28
|
query_class = "#{name}Query".safe_constantize
|
29
29
|
query_class = Query unless query_class.is_a?(Class) && query_class.ancestors.include?(Query)
|
30
|
-
query_class.new(self, params, options).
|
30
|
+
query_class.new(self, params, options).relation
|
31
31
|
end
|
32
32
|
|
33
33
|
end
|
data/lib/scoped_from/query.rb
CHANGED
@@ -10,9 +10,9 @@ module ScopedFrom
|
|
10
10
|
|
11
11
|
# Available options are: - :only : to restrict to specified keys.
|
12
12
|
# - :except : to ignore specified keys.
|
13
|
-
def initialize(
|
14
|
-
|
15
|
-
|
13
|
+
def initialize(relation, params, options = {})
|
14
|
+
self.relation = relation
|
15
|
+
self.options = options
|
16
16
|
self.params = params
|
17
17
|
end
|
18
18
|
|
@@ -24,34 +24,52 @@ module ScopedFrom
|
|
24
24
|
parse_order(params['order'])[:direction]
|
25
25
|
end
|
26
26
|
|
27
|
-
def
|
28
|
-
|
27
|
+
def relation
|
28
|
+
relation = @relation
|
29
29
|
params.each do |name, value|
|
30
30
|
[value].flatten.each do |value|
|
31
|
-
|
31
|
+
relation = invoke_param(relation, name, value)
|
32
32
|
end
|
33
33
|
end
|
34
|
-
|
34
|
+
decorate_relation(relation)
|
35
35
|
end
|
36
36
|
|
37
37
|
protected
|
38
38
|
|
39
|
-
def
|
40
|
-
return
|
41
|
-
def
|
39
|
+
def decorate_relation(relation)
|
40
|
+
return relation if relation.respond_to?(:query)
|
41
|
+
def relation.query
|
42
42
|
@__query
|
43
43
|
end
|
44
|
-
|
45
|
-
|
44
|
+
relation.instance_variable_set('@__query', self)
|
45
|
+
relation
|
46
|
+
end
|
47
|
+
|
48
|
+
def invoke_param(relation, name, value)
|
49
|
+
if name.to_s == 'order'
|
50
|
+
relation.order(order_to_hash(value))
|
51
|
+
elsif relation.scope_with_one_argument?(name)
|
52
|
+
relation.send(name, value)
|
53
|
+
elsif relation.scope_without_argument?(name)
|
54
|
+
relation.send(name)
|
55
|
+
elsif relation.column_names.include?(name.to_s)
|
56
|
+
relation.where(name => value)
|
57
|
+
else
|
58
|
+
relation
|
59
|
+
end
|
46
60
|
end
|
47
61
|
|
48
62
|
def false?(value)
|
49
63
|
FALSE_VALUES.include?(value.to_s.strip.downcase)
|
50
64
|
end
|
51
65
|
|
52
|
-
def
|
66
|
+
def options=(options)
|
67
|
+
@options = options.symbolize_keys
|
68
|
+
end
|
69
|
+
|
70
|
+
def order_to_hash(value)
|
53
71
|
order = parse_order(value)
|
54
|
-
|
72
|
+
order.present? ? { order[:column] => order[:direction].downcase.to_sym } : {}
|
55
73
|
end
|
56
74
|
|
57
75
|
def params=(params)
|
@@ -64,13 +82,13 @@ module ScopedFrom
|
|
64
82
|
if name.to_s == 'order'
|
65
83
|
orders = parse_orders(values).map { |order| "#{order[:column]}.#{order[:direction]}" }
|
66
84
|
@params[name] = (orders.many? ? orders : orders.first) if orders.any?
|
67
|
-
elsif @
|
85
|
+
elsif @relation.scope_without_argument?(name)
|
68
86
|
@params[name] = true if values.all? { |value| true?(value) }
|
69
|
-
elsif @
|
87
|
+
elsif @relation.scope_with_one_argument?(name)
|
70
88
|
value = values.many? ? values : values.first
|
71
89
|
@params[name] = @params[name] ? [@params[name], value].flatten : value
|
72
|
-
elsif @options[:exclude_columns].blank? && @
|
73
|
-
if @
|
90
|
+
elsif @options[:exclude_columns].blank? && @relation.column_names.include?(name.to_s)
|
91
|
+
if @relation.columns_hash[name.to_s].type == :boolean
|
74
92
|
@params[name] = true if values.all? { |value| true?(value) }
|
75
93
|
@params[name] = false if values.all? { |value| false?(value) }
|
76
94
|
else
|
@@ -87,30 +105,20 @@ module ScopedFrom
|
|
87
105
|
column, direction = value.to_s.split(/[\.:\s]+/, 2)
|
88
106
|
direction = direction.to_s.downcase
|
89
107
|
direction = ORDER_DIRECTIONS.first unless ORDER_DIRECTIONS.include?(direction)
|
90
|
-
@
|
108
|
+
@relation.column_names.include?(column) ? { column: column, direction: direction } : {}
|
91
109
|
end
|
92
110
|
|
93
111
|
def parse_orders(values)
|
94
112
|
[].tap do |orders|
|
95
|
-
values.each do |value|
|
113
|
+
values.reverse.each do |value|
|
96
114
|
order = parse_order(value)
|
97
115
|
orders << order if order.present? && !orders.any? { |o| o[:column] == order[:column] }
|
98
116
|
end
|
99
117
|
end
|
100
118
|
end
|
101
119
|
|
102
|
-
def
|
103
|
-
|
104
|
-
scope.order(order_to_sql(value))
|
105
|
-
elsif scope.scope_with_one_argument?(name)
|
106
|
-
scope.send(name, value)
|
107
|
-
elsif scope.scope_without_argument?(name)
|
108
|
-
scope.send(name)
|
109
|
-
elsif scope.column_names.include?(name.to_s)
|
110
|
-
scope.scoped(conditions: { name => value })
|
111
|
-
else
|
112
|
-
scope
|
113
|
-
end
|
120
|
+
def relation=(relation)
|
121
|
+
@relation = relation.is_a?(Class) ? relation.all : relation
|
114
122
|
end
|
115
123
|
|
116
124
|
def true?(value)
|
data/scoped_from.gemspec
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'scoped_from'
|
3
|
-
s.version = File.read(File.
|
3
|
+
s.version = File.read("#{File.dirname(__FILE__)}/VERSION").strip
|
4
4
|
s.platform = Gem::Platform::RUBY
|
5
5
|
s.author = 'Alexis Toulotte'
|
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
9
|
s.description = 'Provides a simple mapping between Active Record scopes and controller parameters for Ruby On Rails 3'
|
10
|
+
s.license = 'MIT'
|
10
11
|
|
11
12
|
s.rubyforge_project = 'scoped_from'
|
12
13
|
|
@@ -15,11 +16,10 @@ Gem::Specification.new do |s|
|
|
15
16
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
16
17
|
s.require_paths = ['lib']
|
17
18
|
|
18
|
-
s.add_dependency 'activerecord', '~>
|
19
|
-
s.add_dependency 'activesupport', '~>
|
19
|
+
s.add_dependency 'activerecord', '~> 4.0.0'
|
20
|
+
s.add_dependency 'activesupport', '~> 4.0.0'
|
20
21
|
|
21
|
-
s.add_development_dependency '
|
22
|
-
s.add_development_dependency 'rake', '~> 10.0.0'
|
22
|
+
s.add_development_dependency 'rake', '~> 10.1.0'
|
23
23
|
s.add_development_dependency 'rspec', '~> 2.14.0'
|
24
24
|
s.add_development_dependency 'sqlite3-ruby', '~> 1.3.0'
|
25
25
|
end
|
data/spec/mocks/user.rb
CHANGED
@@ -1,14 +1,8 @@
|
|
1
1
|
class User < ActiveRecord::Base
|
2
2
|
|
3
|
-
scope :enabled, where(enabled: true)
|
4
|
-
scope :search,
|
5
|
-
|
6
|
-
}
|
7
|
-
scope :created_between, lambda { |after, before|
|
8
|
-
where('created_at >= ? AND created_at <= ?', after, before)
|
9
|
-
}
|
10
|
-
scope :latest, lambda {
|
11
|
-
where('created_at >= ?', 1.week.ago)
|
12
|
-
}
|
3
|
+
scope :enabled, -> { where(enabled: true) }
|
4
|
+
scope :search, -> (pattern) { where('firstname LIKE ? OR lastname LIKE ?', "%#{pattern}%", "%#{pattern}%") }
|
5
|
+
scope :created_between, -> (after, before) { where('created_at >= ? AND created_at <= ?', after, before) }
|
6
|
+
scope :latest, -> { where('created_at >= ?', 1.week.ago) }
|
13
7
|
|
14
8
|
end
|
@@ -64,7 +64,7 @@ describe ScopedFrom::ActiveRecord do
|
|
64
64
|
|
65
65
|
it 'just build a new query and return its scope' do
|
66
66
|
query = double(:query)
|
67
|
-
query.should_receive(:
|
67
|
+
query.should_receive(:relation).and_return(42)
|
68
68
|
ScopedFrom::Query.should_receive(:new).with(User, 'foo', except: 'bam').and_return(query)
|
69
69
|
User.scoped_from('foo', except: 'bam').should == 42
|
70
70
|
end
|
@@ -2,8 +2,8 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe ScopedFrom::Query do
|
4
4
|
|
5
|
-
def query(
|
6
|
-
ScopedFrom::Query.new(
|
5
|
+
def query(relation = User, params = {}, options = {})
|
6
|
+
ScopedFrom::Query.new(relation, params, options)
|
7
7
|
end
|
8
8
|
|
9
9
|
describe '#false?' do
|
@@ -63,11 +63,62 @@ describe ScopedFrom::Query do
|
|
63
63
|
|
64
64
|
describe '#initialize' do
|
65
65
|
|
66
|
-
it 'invokes
|
67
|
-
User.should_receive(:
|
66
|
+
it 'invokes .all method on given class' do
|
67
|
+
User.should_receive(:all)
|
68
68
|
ScopedFrom::Query.new(User, {})
|
69
69
|
end
|
70
70
|
|
71
|
+
it 'does not invokes .all method on given relation' do
|
72
|
+
relation = User.all
|
73
|
+
relation.should_not_receive(:all)
|
74
|
+
ScopedFrom::Query.new(relation, {})
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
describe '#invoke_param' do
|
80
|
+
|
81
|
+
it 'returns given scope if it has no scope with specified name' do
|
82
|
+
query.send(:invoke_param, User, :foo, true).should == User
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'returns given scope if scope takes more than 1 argument' do
|
86
|
+
query.send(:invoke_param, User, :created_between, true).should == User
|
87
|
+
end
|
88
|
+
|
89
|
+
it 'invokes scope without arguments if scope takes no arguments' do
|
90
|
+
query.send(:invoke_param, User.all, :enabled, true).should == [users(:john)]
|
91
|
+
query.send(:invoke_param, User.all, :enabled, ' 1 ').should == [users(:john)]
|
92
|
+
query.send(:invoke_param, User.all, :enabled, 'off').should == [users(:john)]
|
93
|
+
end
|
94
|
+
|
95
|
+
it 'invokes scope with value has argument if scope takes one argument' do
|
96
|
+
query.send(:invoke_param, User.all, :search, 'doe').should == [users(:john), users(:jane)]
|
97
|
+
query.send(:invoke_param, User.all, :search, 'john').should == [users(:john)]
|
98
|
+
query.send(:invoke_param, User.all, :search, 'jane').should == [users(:jane)]
|
99
|
+
end
|
100
|
+
|
101
|
+
it 'scope on column conditions' do
|
102
|
+
query.send(:invoke_param, User.all, :firstname, 'Jane').should == [users(:jane)]
|
103
|
+
end
|
104
|
+
|
105
|
+
it 'invokes "order"' do
|
106
|
+
query.send(:invoke_param, User.all, :order, 'firstname.asc').should == [users(:jane), users(:john)]
|
107
|
+
query.send(:invoke_param, User.all, :order, 'firstname.desc').should == [users(:john), users(:jane)]
|
108
|
+
end
|
109
|
+
|
110
|
+
end
|
111
|
+
|
112
|
+
describe '#options' do
|
113
|
+
|
114
|
+
it 'is set at initialization' do
|
115
|
+
ScopedFrom::Query.new(User, {}, bar: 'foo').instance_variable_get(:@options).should == { bar: 'foo' }
|
116
|
+
end
|
117
|
+
|
118
|
+
it 'keys are symbolized' do
|
119
|
+
ScopedFrom::Query.new(User, {}, 'bar' => 'foo').instance_variable_get(:@options).should == { bar: 'foo' }
|
120
|
+
end
|
121
|
+
|
71
122
|
end
|
72
123
|
|
73
124
|
describe '#order_column' do
|
@@ -288,10 +339,10 @@ describe ScopedFrom::Query do
|
|
288
339
|
end
|
289
340
|
|
290
341
|
it 'many order can be specified' do
|
291
|
-
query(User, { 'order' => ['firstname.Asc', 'lastname.DESC'] }).params.should == { 'order' => ['
|
292
|
-
query(User, { 'order' => ['firstname.Asc', 'firstname.desc'] }).params.should == { 'order' => 'firstname.
|
293
|
-
query(User, { 'order' => ['firstname.Asc', 'lastname.DESC', 'firstname.desc'] }).params.should == { 'order' => ['firstname.
|
294
|
-
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' => ['lastname.desc', 'firstname.asc'] }
|
343
|
+
query(User, { 'order' => ['firstname.Asc', 'firstname.desc'] }).params.should == { 'order' => 'firstname.desc' }
|
344
|
+
query(User, { 'order' => ['firstname.Asc', 'lastname.DESC', 'firstname.desc'] }).params.should == { 'order' => ['firstname.desc', 'lastname.desc'] }
|
345
|
+
query(User, { 'order' => ['firstname.Asc', 'foo', 'lastname.DESC', 'firstname.desc'] }).params.should == { 'order' => ['firstname.desc', 'lastname.desc'] }
|
295
346
|
end
|
296
347
|
|
297
348
|
it 'order can be delimited by a space' do
|
@@ -313,41 +364,41 @@ describe ScopedFrom::Query do
|
|
313
364
|
|
314
365
|
end
|
315
366
|
|
316
|
-
describe '#
|
367
|
+
describe '#relation' do
|
317
368
|
|
318
369
|
it 'does not execute any query' do
|
319
370
|
User.should_not_receive(:connection)
|
320
|
-
query(User, :enabled => true).
|
371
|
+
query(User, :enabled => true).relation
|
321
372
|
end
|
322
373
|
|
323
374
|
it 'works with scopes with a lambda without arguments' do
|
324
375
|
users(:jane).update_attribute(:created_at, 10.days.ago)
|
325
|
-
query(User, :latest => true).
|
326
|
-
query(User, :latest => false).
|
376
|
+
query(User, :latest => true).relation.should == [users(:john)]
|
377
|
+
query(User, :latest => false).relation.should == [users(:john), users(:jane)]
|
327
378
|
end
|
328
379
|
|
329
|
-
it 'does not modify
|
330
|
-
|
331
|
-
q = query(
|
380
|
+
it 'does not modify relation specified at initialization' do
|
381
|
+
relation = User.search('foo')
|
382
|
+
q = query(relation, :enabled => true)
|
332
383
|
expect {
|
333
384
|
expect {
|
334
|
-
q.
|
335
|
-
}.to_not change { q.instance_variable_get('@
|
336
|
-
}.to_not change {
|
385
|
+
q.relation
|
386
|
+
}.to_not change { q.instance_variable_get('@relation') }
|
387
|
+
}.to_not change { relation }
|
337
388
|
end
|
338
389
|
|
339
|
-
it 'returns
|
340
|
-
query.
|
341
|
-
query.
|
390
|
+
it 'returns relation specified at construction if params are empty' do
|
391
|
+
query.relation.should_not == User
|
392
|
+
query.relation.should == User.all
|
342
393
|
end
|
343
394
|
|
344
|
-
it 'invokes many times
|
345
|
-
query(User, :search => ['John', 'Doe']).
|
346
|
-
query(User, :search => ['John', 'Done']).
|
395
|
+
it 'invokes many times relation if an array is given' do
|
396
|
+
query(User, :search => ['John', 'Doe']).relation.should == [users(:john)]
|
397
|
+
query(User, :search => ['John', 'Done']).relation.should == []
|
347
398
|
query(User, :search => ['John', 'Doe']).params.should == { 'search' => ['John', 'Doe'] }
|
348
399
|
end
|
349
400
|
|
350
|
-
it 'invokes many times
|
401
|
+
it 'invokes many times relation if given twice (as string & symbol)' do
|
351
402
|
query(User, :search => 'John', 'search' => 'Done').params['search'].size.should be(2)
|
352
403
|
query(User, :search => 'John', 'search' => 'Done').params['search'].should include('John', 'Done')
|
353
404
|
|
@@ -357,59 +408,29 @@ describe ScopedFrom::Query do
|
|
357
408
|
end
|
358
409
|
|
359
410
|
it 'invokes last order if an array is given' do
|
360
|
-
|
361
|
-
|
362
|
-
query(User, :order => ['
|
411
|
+
create_user(:jane2, firstname: 'Jane', lastname: 'Zoe')
|
412
|
+
|
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 }]
|
363
417
|
end
|
364
418
|
|
365
|
-
it 'defines #query method on returned
|
366
|
-
query(User).
|
419
|
+
it 'defines #query method on returned relation' do
|
420
|
+
query(User).relation.should respond_to(:query)
|
367
421
|
end
|
368
422
|
|
369
|
-
it 'does not define #query method for future
|
370
|
-
query(User).
|
423
|
+
it 'does not define #query method for future relations' do
|
424
|
+
query(User).relation.query.should be_present
|
371
425
|
User.should_not respond_to(:query)
|
372
|
-
User.
|
426
|
+
User.all.should_not respond_to(:query)
|
373
427
|
User.enabled.should_not respond_to(:query)
|
374
428
|
end
|
375
429
|
|
376
430
|
it 'defined #query method returns query' do
|
377
431
|
q = query(User)
|
378
|
-
q.
|
379
|
-
q.
|
380
|
-
end
|
381
|
-
|
382
|
-
end
|
383
|
-
|
384
|
-
describe '#scoped' do
|
385
|
-
|
386
|
-
it 'returns given scope if it has no scope with specified name' do
|
387
|
-
query.send(:scoped, User, :foo, true).should == User
|
388
|
-
end
|
389
|
-
|
390
|
-
it 'returns given scope if scope takes more than 1 argument' do
|
391
|
-
query.send(:scoped, User, :created_between, true).should == User
|
392
|
-
end
|
393
|
-
|
394
|
-
it 'invokes scope without arguments if scope takes no arguments' do
|
395
|
-
query.send(:scoped, User.scoped, :enabled, true).should == [users(:john)]
|
396
|
-
query.send(:scoped, User.scoped, :enabled, ' 1 ').should == [users(:john)]
|
397
|
-
query.send(:scoped, User.scoped, :enabled, 'off').should == [users(:john)]
|
398
|
-
end
|
399
|
-
|
400
|
-
it 'invokes scope with value has argument if scope takes one argument' do
|
401
|
-
query.send(:scoped, User.scoped, :search, 'doe').should == [users(:john), users(:jane)]
|
402
|
-
query.send(:scoped, User.scoped, :search, 'john').should == [users(:john)]
|
403
|
-
query.send(:scoped, User.scoped, :search, 'jane').should == [users(:jane)]
|
404
|
-
end
|
405
|
-
|
406
|
-
it 'scope on column conditions' do
|
407
|
-
query.send(:scoped, User.scoped, :firstname, 'Jane').should == [users(:jane)]
|
408
|
-
end
|
409
|
-
|
410
|
-
it 'invokes "order"' do
|
411
|
-
query.send(:scoped, User.scoped, :order, 'firstname.asc').should == [users(:jane), users(:john)]
|
412
|
-
query.send(:scoped, User.scoped, :order, 'firstname.desc').should == [users(:john), users(:jane)]
|
432
|
+
q.relation.query.should be_a(ScopedFrom::Query)
|
433
|
+
q.relation.query.should be(q)
|
413
434
|
end
|
414
435
|
|
415
436
|
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: 0.8.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: 2013-07-
|
11
|
+
date: 2013-07-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -16,56 +16,42 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ~>
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 4.0.0
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ~>
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 4.0.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: activesupport
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - ~>
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: 4.0.0
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - ~>
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: byebug
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - ~>
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: 1.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: 1.6.0
|
40
|
+
version: 4.0.0
|
55
41
|
- !ruby/object:Gem::Dependency
|
56
42
|
name: rake
|
57
43
|
requirement: !ruby/object:Gem::Requirement
|
58
44
|
requirements:
|
59
45
|
- - ~>
|
60
46
|
- !ruby/object:Gem::Version
|
61
|
-
version: 10.
|
47
|
+
version: 10.1.0
|
62
48
|
type: :development
|
63
49
|
prerelease: false
|
64
50
|
version_requirements: !ruby/object:Gem::Requirement
|
65
51
|
requirements:
|
66
52
|
- - ~>
|
67
53
|
- !ruby/object:Gem::Version
|
68
|
-
version: 10.
|
54
|
+
version: 10.1.0
|
69
55
|
- !ruby/object:Gem::Dependency
|
70
56
|
name: rspec
|
71
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -127,7 +113,8 @@ files:
|
|
127
113
|
- spec/support/bootsrap/database.rb
|
128
114
|
- spec/support/macros/user_macro.rb
|
129
115
|
homepage: https://github.com/alexistoulotte/scoped_from
|
130
|
-
licenses:
|
116
|
+
licenses:
|
117
|
+
- MIT
|
131
118
|
metadata: {}
|
132
119
|
post_install_message:
|
133
120
|
rdoc_options: []
|