activerecord_any_of 1.0.2 → 1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +48 -13
- data/Rakefile +9 -0
- data/lib/activerecord_any_of/version.rb +1 -1
- data/lib/activerecord_any_of.rb +59 -36
- data/test/activerecord_any_of_test.rb +60 -17
- data/test/{dummy → dummy_rails3}/README.rdoc +0 -0
- data/test/{dummy → dummy_rails3}/Rakefile +0 -0
- data/test/{dummy → dummy_rails3}/app/assets/javascripts/application.js +0 -0
- data/test/{dummy → dummy_rails3}/app/assets/stylesheets/application.css +0 -0
- data/test/{dummy → dummy_rails3}/app/controllers/application_controller.rb +0 -0
- data/test/{dummy → dummy_rails3}/app/helpers/application_helper.rb +0 -0
- data/test/{dummy → dummy_rails3}/app/models/author.rb +0 -0
- data/test/{dummy → dummy_rails3}/app/models/post.rb +0 -0
- data/test/{dummy → dummy_rails3}/app/models/special_post.rb +0 -0
- data/test/{dummy → dummy_rails3}/app/models/sti_post.rb +0 -0
- data/test/{dummy → dummy_rails3}/app/views/layouts/application.html.erb +0 -0
- data/test/{dummy → dummy_rails3}/config/application.rb +0 -0
- data/test/{dummy → dummy_rails3}/config/boot.rb +0 -0
- data/test/{dummy → dummy_rails3}/config/database.yml +0 -0
- data/test/{dummy → dummy_rails3}/config/environment.rb +0 -0
- data/test/{dummy → dummy_rails3}/config/environments/development.rb +0 -0
- data/test/{dummy → dummy_rails3}/config/environments/production.rb +0 -0
- data/test/{dummy → dummy_rails3}/config/environments/test.rb +0 -0
- data/test/{dummy → dummy_rails3}/config/initializers/backtrace_silencers.rb +0 -0
- data/test/{dummy → dummy_rails3}/config/initializers/inflections.rb +0 -0
- data/test/{dummy → dummy_rails3}/config/initializers/mime_types.rb +0 -0
- data/test/{dummy → dummy_rails3}/config/initializers/secret_token.rb +0 -0
- data/test/{dummy → dummy_rails3}/config/initializers/session_store.rb +0 -0
- data/test/{dummy → dummy_rails3}/config/initializers/wrap_parameters.rb +0 -0
- data/test/{dummy → dummy_rails3}/config/locales/en.yml +0 -0
- data/test/{dummy → dummy_rails3}/config/routes.rb +0 -0
- data/test/{dummy → dummy_rails3}/config.ru +0 -0
- data/test/{dummy → dummy_rails3}/db/development.sqlite3 +0 -0
- data/test/{dummy → dummy_rails3}/db/migrate/20130617172335_create_authors.rb +0 -0
- data/test/{dummy → dummy_rails3}/db/migrate/20130617173313_create_posts.rb +0 -0
- data/test/{dummy → dummy_rails3}/db/schema.rb +0 -0
- data/test/dummy_rails3/db/test.sqlite3 +0 -0
- data/test/{dummy → dummy_rails3}/log/development.log +10 -0
- data/test/{dummy → dummy_rails3}/log/test.log +288 -0
- data/test/{dummy → dummy_rails3}/public/404.html +0 -0
- data/test/{dummy → dummy_rails3}/public/422.html +0 -0
- data/test/{dummy → dummy_rails3}/public/500.html +0 -0
- data/test/{dummy → dummy_rails3}/public/favicon.ico +0 -0
- data/test/{dummy → dummy_rails3}/script/rails +0 -0
- data/test/{dummy → dummy_rails3}/test/fixtures/authors.yml +0 -0
- data/test/{dummy → dummy_rails3}/test/fixtures/posts.yml +0 -0
- data/test/{dummy → dummy_rails3}/test/unit/author_test.rb +0 -0
- data/test/{dummy → dummy_rails3}/test/unit/post_test.rb +0 -0
- data/test/dummy_rails4/README.rdoc +28 -0
- data/test/dummy_rails4/Rakefile +6 -0
- data/test/dummy_rails4/app/assets/javascripts/application.js +13 -0
- data/test/dummy_rails4/app/assets/stylesheets/application.css +13 -0
- data/test/dummy_rails4/app/controllers/application_controller.rb +5 -0
- data/test/dummy_rails4/app/helpers/application_helper.rb +2 -0
- data/test/dummy_rails4/app/models/author.rb +3 -0
- data/test/dummy_rails4/app/models/post.rb +3 -0
- data/test/dummy_rails4/app/models/special_post.rb +2 -0
- data/test/dummy_rails4/app/models/sti_post.rb +2 -0
- data/test/dummy_rails4/app/views/layouts/application.html.erb +14 -0
- data/test/dummy_rails4/bin/bundle +3 -0
- data/test/dummy_rails4/bin/rails +4 -0
- data/test/dummy_rails4/bin/rake +4 -0
- data/test/dummy_rails4/config/application.rb +23 -0
- data/test/dummy_rails4/config/boot.rb +5 -0
- data/test/dummy_rails4/config/database.yml +25 -0
- data/test/dummy_rails4/config/environment.rb +5 -0
- data/test/dummy_rails4/config/environments/development.rb +29 -0
- data/test/dummy_rails4/config/environments/production.rb +80 -0
- data/test/dummy_rails4/config/environments/test.rb +36 -0
- data/test/dummy_rails4/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy_rails4/config/initializers/filter_parameter_logging.rb +4 -0
- data/test/dummy_rails4/config/initializers/inflections.rb +16 -0
- data/test/dummy_rails4/config/initializers/mime_types.rb +5 -0
- data/test/dummy_rails4/config/initializers/secret_token.rb +12 -0
- data/test/dummy_rails4/config/initializers/session_store.rb +3 -0
- data/test/dummy_rails4/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy_rails4/config/locales/en.yml +23 -0
- data/test/dummy_rails4/config/routes.rb +56 -0
- data/test/dummy_rails4/config.ru +4 -0
- data/test/dummy_rails4/db/development.sqlite3 +0 -0
- data/test/dummy_rails4/db/migrate/20130617172335_create_authors.rb +9 -0
- data/test/dummy_rails4/db/migrate/20130617173313_create_posts.rb +12 -0
- data/test/dummy_rails4/db/schema.rb +31 -0
- data/test/{dummy → dummy_rails4}/db/test.sqlite3 +0 -0
- data/test/dummy_rails4/log/development.log +21 -0
- data/test/dummy_rails4/log/test.log +437 -0
- data/test/dummy_rails4/public/404.html +58 -0
- data/test/dummy_rails4/public/422.html +58 -0
- data/test/dummy_rails4/public/500.html +57 -0
- data/test/dummy_rails4/public/favicon.ico +0 -0
- data/test/dummy_rails4/test/fixtures/authors.yml +7 -0
- data/test/dummy_rails4/test/fixtures/posts.yml +13 -0
- data/test/dummy_rails4/test/unit/author_test.rb +7 -0
- data/test/dummy_rails4/test/unit/post_test.rb +7 -0
- data/test/test_helper.rb +4 -4
- metadata +180 -88
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a9596994399dde0f68fd7f66190597b3d1342aea
|
4
|
+
data.tar.gz: 92c72d9afc6d5748552c2dd582aae86aa0763f0c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5d801f39fdd970de448edeb11bf1abbc3bd41e6c69a94fd56a8b51f8664c8247e25ea86044859671a7ad508feac9e842ccce3a59f69951660e27b97536eef299
|
7
|
+
data.tar.gz: 2621b8ebbb70ad7933c4df55f3b676d752b856da623aa7bd1d2b5acc9b93f45b542f99132343a2b4d2283e4f6203e0a3a6ac3ec3b3755098fcdfb7d0ef7e4239
|
data/README.md
CHANGED
@@ -7,18 +7,17 @@ This gem provides `#any_of` and `#none_of` on ActiveRecord.
|
|
7
7
|
It allows to compute an `OR` like query that leverages AR's `#where` syntax:
|
8
8
|
|
9
9
|
```ruby
|
10
|
-
users = User.any_of("email like '%@example.com'", {banned: true}).destroy_all
|
10
|
+
users = User.where.any_of("email like '%@example.com'", {banned: true}).destroy_all
|
11
11
|
# DELETE FROM users WHERE email LIKE '%@example.com' OR banned = '1';
|
12
12
|
```
|
13
13
|
|
14
|
-
It can be used
|
15
|
-
behind an other relation.
|
14
|
+
It can be used anywhere `#where` is valid :
|
16
15
|
|
17
16
|
```ruby
|
18
17
|
manual_removal = User.where(id: params[:users][:destroy_ids])
|
19
|
-
User.any_of(manual_removal, "email like '%@example.com'", {banned: true})
|
20
|
-
@company.users.any_of(manual_removal, "email like '%@example.com'", {banned: true})
|
21
|
-
User.where(offline: false).any_of( manual_removal, "email like '%@example.com'", {banned: true})
|
18
|
+
User.where.any_of(manual_removal, "email like '%@example.com'", {banned: true})
|
19
|
+
@company.users.where.any_of(manual_removal, "email like '%@example.com'", {banned: true})
|
20
|
+
User.where(offline: false).where.any_of( manual_removal, "email like '%@example.com'", {banned: true})
|
22
21
|
```
|
23
22
|
|
24
23
|
Its main purpose is to both :
|
@@ -31,9 +30,20 @@ Its main purpose is to both :
|
|
31
30
|
```ruby
|
32
31
|
banned_users = User.where(banned: true)
|
33
32
|
unconfirmed_users = User.where("confirmed_at IS NULL")
|
34
|
-
active_users = User.none_of(banned_users, unconfirmed_users)
|
33
|
+
active_users = User.where.none_of(banned_users, unconfirmed_users)
|
35
34
|
```
|
36
35
|
|
36
|
+
## Rails-3
|
37
|
+
|
38
|
+
`activerecord_any_of` uses WhereChain, which has been introduced in rails-4. In
|
39
|
+
rails-3, simply call `#any_of` and `#none_of` directly, without using `#where` :
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
manual_removal = User.where(id: params[:users][:destroy_ids])
|
43
|
+
User.any_of(manual_removal, "email like '%@example.com'", {banned: true})
|
44
|
+
@company.users.any_of(manual_removal, "email like '%@example.com'", {banned: true})
|
45
|
+
User.where(offline: false).any_of( manual_removal, "email like '%@example.com'", {banned: true})
|
46
|
+
```
|
37
47
|
|
38
48
|
## Installation
|
39
49
|
|
@@ -53,9 +63,12 @@ pass for rails < 3.2.13, I may edit gem dependencies).
|
|
53
63
|
User.where( "email LIKE '%@example.com" ).where( active: true ).or( offline: true )
|
54
64
|
```
|
55
65
|
|
56
|
-
What does this query do ? `where (email LIKE '%@example.com' AND active = '1' )
|
66
|
+
What does this query do ? `where (email LIKE '%@example.com' AND active = '1' )
|
67
|
+
OR offline = '1'` ? Or `where email LIKE '%@example.com' AND ( active = '1' OR
|
68
|
+
offline = '1' )` ? This can quickly get messy and counter intuitive.
|
57
69
|
|
58
|
-
The MongoId solution is quite elegant. Using `#any_of`, it is made clear which
|
70
|
+
The MongoId solution is quite elegant. Using `#any_of`, it is made clear which
|
71
|
+
conditions are grouped through `OR` and which are grouped through `AND` :
|
59
72
|
|
60
73
|
* `User.where( "email LIKE '%@example.com" ).any_of({ active: true }, { offline: true })`
|
61
74
|
* `fakes = User.where( "email LIKE '%@example.com'" ).where( active: true ); User.any_of( fakes, { offline: true })`
|
@@ -68,14 +81,36 @@ You can [say it there](https://github.com/rails/rails/pull/10891).
|
|
68
81
|
|
69
82
|
## Running test
|
70
83
|
|
84
|
+
Activerecord_any_of allows to run tests against both rails-3 and rails-4. You
|
85
|
+
have to run them seperately, but it's ok to use the same directory / machine to
|
86
|
+
run both.
|
87
|
+
|
88
|
+
### Running tests with rails-4
|
89
|
+
|
71
90
|
```shell
|
72
91
|
# One time setup
|
73
|
-
|
74
|
-
|
75
|
-
rake db:
|
92
|
+
bundle install --gemfile Gemfile.rails4
|
93
|
+
cd test/dummy_rails4
|
94
|
+
BUNDLE_GEMFILE=../../Gemfile.rails4 bundle exec rake db:migrate
|
95
|
+
BUNDLE_GEMFILE=../../Gemfile.rails4 bundle exec rake db:test:prepare
|
76
96
|
cd ../..
|
97
|
+
|
98
|
+
# Then
|
99
|
+
bundle exec rake test
|
100
|
+
```
|
101
|
+
|
102
|
+
### Running tests with rails-3
|
103
|
+
|
104
|
+
```shell
|
105
|
+
# One time setup
|
106
|
+
bundle install --gemfile Gemfile.rails3
|
107
|
+
cd test/dummy_rails3
|
108
|
+
BUNDLE_GEMFILE=../../Gemfile.rails3 bundle exec rake db:migrate
|
109
|
+
BUNDLE_GEMFILE=../../Gemfile.rails3 bundle exec rake db:test:prepare
|
110
|
+
cd ../..
|
111
|
+
|
77
112
|
# Then
|
78
|
-
rake test
|
113
|
+
RAILS_VERSION=3 bundle exec rake test
|
79
114
|
```
|
80
115
|
|
81
116
|
|
data/Rakefile
CHANGED
@@ -1,4 +1,13 @@
|
|
1
1
|
#!/usr/bin/env rake
|
2
|
+
|
3
|
+
rails_version = ENV[ 'RAILS_VERSION' ] || '4'
|
4
|
+
|
5
|
+
if rails_version == '3'
|
6
|
+
ENV[ 'BUNDLE_GEMFILE' ] = 'Gemfile.rails3'
|
7
|
+
else
|
8
|
+
ENV[ 'BUNDLE_GEMFILE' ] = 'Gemfile.rails4'
|
9
|
+
end
|
10
|
+
|
2
11
|
begin
|
3
12
|
require 'bundler/setup'
|
4
13
|
rescue LoadError
|
data/lib/activerecord_any_of.rb
CHANGED
@@ -1,43 +1,65 @@
|
|
1
1
|
require 'activerecord_any_of/alternative_builder'
|
2
2
|
|
3
3
|
module ActiverecordAnyOf
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
4
|
+
module Chained
|
5
|
+
# Returns a new relation, which includes results matching any of the conditions
|
6
|
+
# passed as parameters. You can think of it as a sql <tt>OR</tt> implementation.
|
7
|
+
#
|
8
|
+
# Each #any_of parameter is the same set you would have passed to #where :
|
9
|
+
#
|
10
|
+
# Client.any_of("orders_count = '2'", ["name = ?", 'Joe'], {email: 'joe@example.com'})
|
11
|
+
#
|
12
|
+
# You can as well pass #any_of to other relations :
|
13
|
+
#
|
14
|
+
# Client.where("orders_count = '2'").any_of({ email: 'joe@example.com' }, { email: 'john@example.com' })
|
15
|
+
#
|
16
|
+
# And with associations :
|
17
|
+
#
|
18
|
+
# User.find(1).posts.any_of({published: false}, "user_id IS NULL")
|
19
|
+
#
|
20
|
+
# The best part is that #any_of accepts other relations as parameter, to help compute
|
21
|
+
# dynamic <tt>OR</tt> queries :
|
22
|
+
#
|
23
|
+
# banned_users = User.where(banned: true)
|
24
|
+
# unconfirmed_users = User.where("confirmed_at IS NULL")
|
25
|
+
# inactive_users = User.any_of(banned_users, unconfirmed_users)
|
26
|
+
def any_of(*queries)
|
27
|
+
raise ArgumentError, 'Called any_of() with no arguments.' if queries.none?
|
28
|
+
AlternativeBuilder.new(:positive, @scope, *queries).build
|
29
|
+
end
|
30
|
+
|
31
|
+
# Returns a new relation, which includes results not matching any of the conditions
|
32
|
+
# passed as parameters. It's the negative version of <tt>#any_of</tt>.
|
33
|
+
#
|
34
|
+
# This will return all active users :
|
35
|
+
#
|
36
|
+
# banned_users = User.where(banned: true)
|
37
|
+
# unconfirmed_users = User.where("confirmed_at IS NULL")
|
38
|
+
# active_users = User.none_of(banned_users, unconfirmed_users)
|
39
|
+
def none_of(*queries)
|
40
|
+
raise ArgumentError, 'Called none_of() with no arguments.' if queries.none?
|
41
|
+
AlternativeBuilder.new(:negative, @scope, *queries).build
|
42
|
+
end
|
28
43
|
end
|
29
44
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
45
|
+
module Deprecated
|
46
|
+
def any_of(*queries)
|
47
|
+
if Rails.version >= '4'
|
48
|
+
ActiveSupport::Deprecation.warn( "Calling #any_of directly is deprecated and will be removed in activerecord_any_of-1.2.\nPlease call it with #where : User.where.any_of(cond1, cond2)." )
|
49
|
+
end
|
50
|
+
|
51
|
+
raise ArgumentError, 'Called any_of() with no arguments.' if queries.none?
|
52
|
+
AlternativeBuilder.new(:positive, self, *queries).build
|
53
|
+
end
|
54
|
+
|
55
|
+
def none_of(*queries)
|
56
|
+
if Rails.version >= '4'
|
57
|
+
ActiveSupport::Deprecation.warn( "Calling #none_of directly is deprecated and will be removed in activerecord_any_of-1.2.\nPlease call it with #where : User.where.none_of(cond1, cond2)." )
|
58
|
+
end
|
59
|
+
|
60
|
+
raise ArgumentError, 'Called none_of() with no arguments.' if queries.none?
|
61
|
+
AlternativeBuilder.new(:negative, self, *queries).build
|
62
|
+
end
|
41
63
|
end
|
42
64
|
end
|
43
65
|
|
@@ -53,5 +75,6 @@ else
|
|
53
75
|
end
|
54
76
|
end
|
55
77
|
|
56
|
-
ActiveRecord::Relation.send(:include, ActiverecordAnyOf)
|
78
|
+
ActiveRecord::Relation.send(:include, ActiverecordAnyOf::Deprecated)
|
79
|
+
ActiveRecord::Relation::WhereChain.send(:include, ActiverecordAnyOf::Chained) if Rails.version >= '4'
|
57
80
|
ActiveRecord::Base.send(:extend, ActiverecordAnyOfDelegation)
|
@@ -4,16 +4,21 @@ class ActiverecordAnyOfTest < ActiveSupport::TestCase
|
|
4
4
|
fixtures :authors, :posts
|
5
5
|
|
6
6
|
test 'finding with alternate conditions' do
|
7
|
-
assert_equal ['David', 'Mary'], Author.any_of({name: 'David'}, {name: 'Mary'}).map(&:name)
|
8
|
-
assert_equal ['David', 'Mary'], Author.any_of({name: 'David'}, ['name = ?', 'Mary']).map(&:name)
|
9
|
-
|
10
|
-
davids = Author.where(name: 'David')
|
11
|
-
assert_equal ['David', 'Mary', 'Bob'], Author.any_of(davids, ['name = ?', 'Mary'], {name: 'Bob'}).map(&:name)
|
12
|
-
assert_equal ['David', 'Mary', 'Bob'], Author.any_of(davids, "name = 'Mary'", {name: 'Bob', id: 3}).map(&:name)
|
13
|
-
|
14
7
|
if Rails.version >= '4'
|
15
|
-
assert_equal ['David'], Author.where.
|
8
|
+
assert_equal ['David', 'Mary'], Author.where.any_of({name: 'David'}, {name: 'Mary'}).map(&:name)
|
9
|
+
assert_equal ['David', 'Mary'], Author.where.any_of({name: 'David'}, ['name = ?', 'Mary']).map(&:name)
|
10
|
+
|
11
|
+
davids = Author.where(name: 'David')
|
12
|
+
assert_equal ['David', 'Mary', 'Bob'], Author.where.any_of(davids, ['name = ?', 'Mary'], {name: 'Bob'}).map(&:name)
|
13
|
+
assert_equal ['David', 'Mary', 'Bob'], Author.where.any_of(davids, "name = 'Mary'", {name: 'Bob', id: 3}).map(&:name)
|
14
|
+
assert_equal ['David'], Author.where.not(name: 'Mary').where.any_of(davids, ['name = ?', 'Mary']).map(&:name)
|
16
15
|
else
|
16
|
+
assert_equal ['David', 'Mary'], Author.any_of({name: 'David'}, {name: 'Mary'}).map(&:name)
|
17
|
+
assert_equal ['David', 'Mary'], Author.any_of({name: 'David'}, ['name = ?', 'Mary']).map(&:name)
|
18
|
+
|
19
|
+
davids = Author.where(name: 'David')
|
20
|
+
assert_equal ['David', 'Mary', 'Bob'], Author.any_of(davids, ['name = ?', 'Mary'], {name: 'Bob'}).map(&:name)
|
21
|
+
assert_equal ['David', 'Mary', 'Bob'], Author.any_of(davids, "name = 'Mary'", {name: 'Bob', id: 3}).map(&:name)
|
17
22
|
assert_equal ['David'], Author.where("name IS NOT 'Mary'").any_of(davids, ['name = ?', 'Mary']).map(&:name)
|
18
23
|
end
|
19
24
|
end
|
@@ -22,46 +27,84 @@ class ActiverecordAnyOfTest < ActiveSupport::TestCase
|
|
22
27
|
david = Author.where(name: 'David').first
|
23
28
|
welcome = david.posts.where(body: 'Such a lovely day')
|
24
29
|
expected = ['Welcome to the weblog', 'So I was thinking']
|
25
|
-
|
30
|
+
|
31
|
+
if Rails.version >= '4'
|
32
|
+
assert_equal expected, david.posts.where.any_of(welcome, {type: 'SpecialPost'}).map(&:title)
|
33
|
+
else
|
34
|
+
assert_equal expected, david.posts.any_of(welcome, {type: 'SpecialPost'}).map(&:title)
|
35
|
+
end
|
26
36
|
end
|
27
37
|
|
28
38
|
test 'finding alternate dynamically with joined queries' do
|
29
39
|
david = Author.where(posts: { title: 'Welcome to the weblog' }).joins(:posts)
|
30
40
|
mary = Author.where(posts: { title: "eager loading with OR'd conditions" }).joins(:posts)
|
31
41
|
|
32
|
-
|
42
|
+
if Rails.version >= '4'
|
43
|
+
assert_equal ['David', 'Mary'], Author.where.any_of(david, mary).map(&:name)
|
44
|
+
else
|
45
|
+
assert_equal ['David', 'Mary'], Author.any_of(david, mary).map(&:name)
|
46
|
+
end
|
33
47
|
|
34
48
|
if Rails.version >= '4'
|
35
49
|
david = Author.where(posts: { title: 'Welcome to the weblog' }).includes(:posts).references(:posts)
|
36
50
|
mary = Author.where(posts: { title: "eager loading with OR'd conditions" }).includes(:posts).references(:posts)
|
51
|
+
assert_equal ['David', 'Mary'], Author.where.any_of(david, mary).map(&:name)
|
37
52
|
else
|
38
53
|
david = Author.where(posts: { title: 'Welcome to the weblog' }).includes(:posts)
|
39
54
|
mary = Author.where(posts: { title: "eager loading with OR'd conditions" }).includes(:posts)
|
55
|
+
assert_equal ['David', 'Mary'], Author.any_of(david, mary).map(&:name)
|
40
56
|
end
|
41
|
-
|
42
|
-
assert_equal ['David', 'Mary'], Author.any_of(david, mary).map(&:name)
|
43
57
|
end
|
44
58
|
|
45
59
|
test 'finding with alternate negative conditions' do
|
46
|
-
|
60
|
+
if Rails.version >= '4'
|
61
|
+
assert_equal ['Bob'], Author.where.none_of({name: 'David'}, {name: 'Mary'}).map(&:name)
|
62
|
+
else
|
63
|
+
assert_equal ['Bob'], Author.none_of({name: 'David'}, {name: 'Mary'}).map(&:name)
|
64
|
+
end
|
47
65
|
end
|
48
66
|
|
49
67
|
test 'finding with alternate negative conditions on association' do
|
50
68
|
david = Author.where(name: 'David').first
|
51
69
|
welcome = david.posts.where(body: 'Such a lovely day')
|
52
70
|
expected = ['sti comments', 'sti me', 'habtm sti test']
|
53
|
-
|
71
|
+
|
72
|
+
if Rails.version >= '4'
|
73
|
+
assert_equal expected, david.posts.where.none_of(welcome, {type: 'SpecialPost'}).map(&:title)
|
74
|
+
else
|
75
|
+
assert_equal expected, david.posts.none_of(welcome, {type: 'SpecialPost'}).map(&:title)
|
76
|
+
end
|
54
77
|
end
|
55
78
|
|
56
79
|
test 'calling #any_of with no argument raise exception' do
|
57
|
-
|
80
|
+
if Rails.version >= '4'
|
81
|
+
assert_raise(ArgumentError) { Author.where.any_of }
|
82
|
+
else
|
83
|
+
assert_raise(ArgumentError) { Author.any_of }
|
84
|
+
end
|
58
85
|
end
|
59
86
|
|
60
87
|
test 'calling #none_of with no argument raise exception' do
|
61
|
-
|
88
|
+
if Rails.version >= '4'
|
89
|
+
assert_raise(ArgumentError) { Author.where.none_of }
|
90
|
+
else
|
91
|
+
assert_raise(ArgumentError) { Author.none_of }
|
92
|
+
end
|
62
93
|
end
|
63
94
|
|
64
95
|
test 'calling #any_of after a wildcard query works' do
|
65
|
-
|
96
|
+
if Rails.version >= '4'
|
97
|
+
assert_equal ['David'], Author.where("name like '%av%'").where.any_of({name: 'David'}, {name: 'Mary'}).map(&:name)
|
98
|
+
else
|
99
|
+
assert_equal ['David'], Author.where("name like '%av%'").any_of({name: 'David'}, {name: 'Mary'}).map(&:name)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
if Rails.version >= '4'
|
104
|
+
test 'calling directly #any_of is deprecated in rails-4' do
|
105
|
+
assert_deprecated do
|
106
|
+
Author.any_of({name: 'David'}, {name: 'Mary'}).map(&:name)
|
107
|
+
end
|
108
|
+
end
|
66
109
|
end
|
67
110
|
end
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
Binary file
|
@@ -89,3 +89,13 @@ Connecting to database specified by database.yml
|
|
89
89
|
[1m[35m (288.7ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20130617173313')
|
90
90
|
[1m[36m (289.4ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20130617172335')[0m
|
91
91
|
Connecting to database specified by database.yml
|
92
|
+
Connecting to database specified by database.yml
|
93
|
+
[1m[36m (0.0ms)[0m [1mSELECT "schema_migrations"."version" FROM "schema_migrations" [0m
|
94
|
+
[1m[35m (0.1ms)[0m select sqlite_version(*)
|
95
|
+
[1m[36m (273.6ms)[0m [1mCREATE TABLE "authors" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL) [0m
|
96
|
+
[1m[35m (300.0ms)[0m CREATE TABLE "posts" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "body" text, "author_id" integer, "type" varchar(255), "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
|
97
|
+
[1m[36m (324.0ms)[0m [1mCREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL) [0m
|
98
|
+
[1m[35m (311.3ms)[0m CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
|
99
|
+
[1m[36m (0.1ms)[0m [1mSELECT version FROM "schema_migrations"[0m
|
100
|
+
[1m[35m (277.6ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20130617173313')
|
101
|
+
[1m[36m (300.5ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20130617172335')[0m
|