active_hash_relation 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +2 -0
- data/.travis.yml +15 -0
- data/README.md +68 -11
- data/active_hash_relation.gemspec +13 -0
- data/lib/active_hash_relation.rb +18 -12
- data/lib/active_hash_relation/aggregation.rb +3 -0
- data/lib/active_hash_relation/association_filters.rb +3 -0
- data/lib/active_hash_relation/column_filters.rb +45 -11
- data/lib/active_hash_relation/filter_applier.rb +37 -23
- data/lib/active_hash_relation/helpers.rb +22 -2
- data/lib/active_hash_relation/scope_filters.rb +8 -1
- data/lib/active_hash_relation/sort_filters.rb +12 -0
- data/lib/active_hash_relation/version.rb +1 -1
- data/lib/active_record/scope_names.rb +4 -19
- data/lib/generators/active_hash_relation/initialize_generator.rb +13 -0
- data/lib/generators/active_hash_relation/templates/active_hash_relation.rb +12 -0
- data/spec/config/initializers/active_hash_relation.rb +16 -0
- data/spec/db/migrate/001_basic_schema.rb +53 -0
- data/spec/dummy-rails4/.gitignore +13 -0
- data/spec/dummy-rails4/.ruby-version +1 -0
- data/spec/dummy-rails4/Gemfile +18 -0
- data/spec/dummy-rails4/README.md +3 -0
- data/spec/dummy-rails4/Rakefile +6 -0
- data/spec/dummy-rails4/app/controllers/application_controller.rb +5 -0
- data/spec/dummy-rails4/app/models/.keep +0 -0
- data/spec/dummy-rails4/app/models/address.rb +1 -0
- data/spec/dummy-rails4/app/models/micropost.rb +1 -0
- data/spec/dummy-rails4/app/models/relationship.rb +1 -0
- data/spec/dummy-rails4/app/models/user.rb +1 -0
- data/spec/dummy-rails4/bin/bundle +3 -0
- data/spec/dummy-rails4/bin/rails +4 -0
- data/spec/dummy-rails4/bin/rake +4 -0
- data/spec/dummy-rails4/bin/setup +29 -0
- data/spec/dummy-rails4/config.ru +4 -0
- data/spec/dummy-rails4/config/application.rb +35 -0
- data/spec/dummy-rails4/config/boot.rb +3 -0
- data/spec/dummy-rails4/config/database.yml +18 -0
- data/spec/dummy-rails4/config/environment.rb +5 -0
- data/spec/dummy-rails4/config/environments/development.rb +41 -0
- data/spec/dummy-rails4/config/environments/production.rb +79 -0
- data/spec/dummy-rails4/config/environments/test.rb +42 -0
- data/spec/dummy-rails4/config/initializers/active_hash_relation.rb +3 -0
- data/spec/dummy-rails4/config/initializers/assets.rb +11 -0
- data/spec/dummy-rails4/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy-rails4/config/initializers/cookies_serializer.rb +3 -0
- data/spec/dummy-rails4/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy-rails4/config/initializers/inflections.rb +16 -0
- data/spec/dummy-rails4/config/initializers/mime_types.rb +4 -0
- data/spec/dummy-rails4/config/initializers/session_store.rb +3 -0
- data/spec/dummy-rails4/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy-rails4/config/locales/en.yml +23 -0
- data/spec/dummy-rails4/config/routes.rb +56 -0
- data/spec/dummy-rails4/config/secrets.yml +22 -0
- data/spec/dummy-rails4/db/migrate/001_basic_schema.rb +1 -0
- data/spec/dummy-rails4/db/schema.rb +69 -0
- data/spec/dummy-rails4/spec/factories/factories.rb +1 -0
- data/spec/dummy-rails4/spec/rails_helper.rb +19 -0
- data/spec/dummy-rails4/spec/spec_helper.rb +99 -0
- data/spec/dummy-rails4/spec/support/support.rb +1 -0
- data/spec/dummy-rails4/spec/tests/tests_spec.rb +3 -0
- data/spec/dummy-rails5/.gitignore +17 -0
- data/spec/dummy-rails5/.rspec +2 -0
- data/spec/dummy-rails5/.ruby-version +1 -0
- data/spec/dummy-rails5/Gemfile +19 -0
- data/spec/dummy-rails5/README.md +3 -0
- data/spec/dummy-rails5/Rakefile +6 -0
- data/spec/dummy-rails5/app/controllers/application_controller.rb +3 -0
- data/spec/dummy-rails5/app/models/address.rb +1 -0
- data/spec/dummy-rails5/app/models/micropost.rb +1 -0
- data/spec/dummy-rails5/app/models/relationship.rb +1 -0
- data/spec/dummy-rails5/app/models/user.rb +1 -0
- data/spec/dummy-rails5/bin/bundle +3 -0
- data/spec/dummy-rails5/bin/rails +4 -0
- data/spec/dummy-rails5/bin/rake +4 -0
- data/spec/dummy-rails5/bin/setup +34 -0
- data/spec/dummy-rails5/bin/update +29 -0
- data/spec/dummy-rails5/config.ru +5 -0
- data/spec/dummy-rails5/config/application.rb +25 -0
- data/spec/dummy-rails5/config/boot.rb +3 -0
- data/spec/dummy-rails5/config/cable.yml +9 -0
- data/spec/dummy-rails5/config/database.yml +18 -0
- data/spec/dummy-rails5/config/environment.rb +5 -0
- data/spec/dummy-rails5/config/environments/development.rb +54 -0
- data/spec/dummy-rails5/config/environments/production.rb +86 -0
- data/spec/dummy-rails5/config/environments/test.rb +42 -0
- data/spec/dummy-rails5/config/initializers/active_hash_relation.rb +3 -0
- data/spec/dummy-rails5/config/initializers/application_controller_renderer.rb +6 -0
- data/spec/dummy-rails5/config/initializers/assets.rb +11 -0
- data/spec/dummy-rails5/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy-rails5/config/initializers/cookies_serializer.rb +5 -0
- data/spec/dummy-rails5/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy-rails5/config/initializers/inflections.rb +16 -0
- data/spec/dummy-rails5/config/initializers/mime_types.rb +4 -0
- data/spec/dummy-rails5/config/initializers/new_framework_defaults.rb +24 -0
- data/spec/dummy-rails5/config/initializers/session_store.rb +3 -0
- data/spec/dummy-rails5/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy-rails5/config/locales/en.yml +23 -0
- data/spec/dummy-rails5/config/puma.rb +47 -0
- data/spec/dummy-rails5/config/routes.rb +3 -0
- data/spec/dummy-rails5/config/secrets.yml +22 -0
- data/spec/dummy-rails5/config/spring.rb +6 -0
- data/spec/dummy-rails5/db/migrate/001_basic_schema.rb +2 -0
- data/spec/dummy-rails5/db/schema.rb +64 -0
- data/spec/dummy-rails5/spec/factories/factories.rb +1 -0
- data/spec/dummy-rails5/spec/rails_helper.rb +19 -0
- data/spec/dummy-rails5/spec/spec_helper.rb +99 -0
- data/spec/dummy-rails5/spec/support/support.rb +1 -0
- data/spec/dummy-rails5/spec/tests/tests_spec.rb +3 -0
- data/spec/factories/factories.rb +39 -0
- data/spec/models/address.rb +7 -0
- data/spec/models/application_record.rb +3 -0
- data/spec/models/micropost.rb +9 -0
- data/spec/models/relationship.rb +9 -0
- data/spec/models/user.rb +32 -0
- data/spec/support/support.rb +62 -0
- data/spec/tests/aggregations/avg_spec.rb +45 -0
- data/spec/tests/aggregations/max_spec.rb +45 -0
- data/spec/tests/aggregations/min_spec.rb +46 -0
- data/spec/tests/aggregations/sum_spec.rb +43 -0
- data/spec/tests/associations/belongs_to_spec.rb +96 -0
- data/spec/tests/associations/has_many_spec.rb +91 -0
- data/spec/tests/associations/has_one_spec.rb +89 -0
- data/spec/tests/booleans_spec.rb +29 -0
- data/spec/tests/limit_spec.rb +30 -0
- data/spec/tests/null_spec.rb +44 -0
- data/spec/tests/numbers_spec.rb +142 -0
- data/spec/tests/or_filter_spec.rb +98 -0
- data/spec/tests/primary_key_spec.rb +29 -0
- data/spec/tests/scopes_spec.rb +48 -0
- data/spec/tests/sorting_spec.rb +100 -0
- data/spec/tests/strings_spec.rb +86 -0
- metadata +392 -4
@@ -0,0 +1,89 @@
|
|
1
|
+
describe ActiveHashRelation do
|
2
|
+
include Helpers
|
3
|
+
|
4
|
+
context 'associations' do
|
5
|
+
context 'has_one' do
|
6
|
+
it "one where clause" do
|
7
|
+
hash = {address: {street: 'Sveavägen 4' }}
|
8
|
+
|
9
|
+
query = HelperClass.new.apply_filters(User.all, hash, include_associations: true).to_sql
|
10
|
+
expected_query = q(
|
11
|
+
"SELECT users.* FROM users",
|
12
|
+
"INNER JOIN addresses ON addresses.user_id = users.id",
|
13
|
+
"WHERE (addresses.street = 'Sveavägen 4')"
|
14
|
+
)
|
15
|
+
|
16
|
+
expect(strip(query)).to eq expected_query.to_s
|
17
|
+
end
|
18
|
+
|
19
|
+
it "multiple where clause" do
|
20
|
+
hash = {address: {street: 'Sveavägen 4', country: 'SE' }}
|
21
|
+
|
22
|
+
query = HelperClass.new.apply_filters(User.all, hash, include_associations: true).to_sql
|
23
|
+
expected_query = q(
|
24
|
+
"SELECT users.* FROM users",
|
25
|
+
"INNER JOIN addresses ON addresses.user_id = users.id",
|
26
|
+
"WHERE (addresses.street = 'Sveavägen 4')",
|
27
|
+
"AND (addresses.country = 'SE')"
|
28
|
+
)
|
29
|
+
|
30
|
+
expect(strip(query)).to eq expected_query.to_s
|
31
|
+
end
|
32
|
+
|
33
|
+
it "multiple queries" do
|
34
|
+
hash = {address: {street: {like: 'sveav', with_ilike: true}, city: {starts_with: 'New'} }}
|
35
|
+
|
36
|
+
query = HelperClass.new.apply_filters(User.all, hash, include_associations: true).to_sql
|
37
|
+
expected_query = q(
|
38
|
+
"SELECT users.* FROM users",
|
39
|
+
"INNER JOIN addresses ON addresses.user_id = users.id",
|
40
|
+
"WHERE (addresses.street ILIKE '%sveav%')",
|
41
|
+
"AND (addresses.city LIKE 'New%')"
|
42
|
+
)
|
43
|
+
|
44
|
+
expect(strip(query)).to eq expected_query.to_s
|
45
|
+
end
|
46
|
+
|
47
|
+
it "scope" do
|
48
|
+
hash = {address: {scopes: {in_sweden: true} }}
|
49
|
+
|
50
|
+
query = HelperClass.new.apply_filters(User.all, hash, include_associations: true).to_sql
|
51
|
+
expected_query = q(
|
52
|
+
"SELECT users.* FROM users",
|
53
|
+
"INNER JOIN addresses ON addresses.user_id = users.id",
|
54
|
+
"WHERE addresses.country = 'SE'"
|
55
|
+
)
|
56
|
+
|
57
|
+
expect(strip(query)).to eq expected_query.to_s
|
58
|
+
end
|
59
|
+
|
60
|
+
it "null" do
|
61
|
+
hash = {address: {street: {null: false} }}
|
62
|
+
|
63
|
+
query = HelperClass.new.apply_filters(User.all, hash, include_associations: true).to_sql
|
64
|
+
expected_query = q(
|
65
|
+
"SELECT users.* FROM users",
|
66
|
+
"INNER JOIN addresses ON addresses.user_id = users.id",
|
67
|
+
"WHERE (addresses.street IS NOT NULL)"
|
68
|
+
)
|
69
|
+
|
70
|
+
expect(strip(query)).to eq expected_query.to_s
|
71
|
+
end
|
72
|
+
|
73
|
+
it "sorting" do
|
74
|
+
hash = {address: {street: {like: 'svea'}, sort: {property: :street, order: :asc} }}
|
75
|
+
|
76
|
+
query = HelperClass.new.apply_filters(User.all, hash, include_associations: true).to_sql
|
77
|
+
expected_query = q(
|
78
|
+
"SELECT users.* FROM users",
|
79
|
+
"INNER JOIN addresses ON addresses.user_id = users.id",
|
80
|
+
"WHERE (addresses.street LIKE '%svea%')",
|
81
|
+
"ORDER BY addresses.street ASC"
|
82
|
+
)
|
83
|
+
|
84
|
+
expect(strip(query)).to eq expected_query.to_s
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
@@ -0,0 +1,29 @@
|
|
1
|
+
describe ActiveHashRelation do
|
2
|
+
include Helpers
|
3
|
+
|
4
|
+
context 'booleans' do
|
5
|
+
it "one boolean where clause" do
|
6
|
+
hash = {admin: false}
|
7
|
+
|
8
|
+
query = HelperClass.new.apply_filters(User.all, hash).to_sql
|
9
|
+
expected_query = q(
|
10
|
+
"SELECT users.* FROM users",
|
11
|
+
"WHERE users.admin = 'f'"
|
12
|
+
)
|
13
|
+
|
14
|
+
expect(strip(query)).to eq expected_query.to_s
|
15
|
+
end
|
16
|
+
|
17
|
+
it "multi boolean where clauses" do
|
18
|
+
hash = {admin: false, verified: true}
|
19
|
+
|
20
|
+
query = HelperClass.new.apply_filters(User.all, hash).to_sql
|
21
|
+
expected_query = q(
|
22
|
+
"SELECT users.* FROM users",
|
23
|
+
"WHERE users.admin = 'f' AND users.verified = 't'"
|
24
|
+
)
|
25
|
+
|
26
|
+
expect(strip(query)).to eq expected_query.to_s
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
describe ActiveHashRelation do
|
2
|
+
include Helpers
|
3
|
+
|
4
|
+
context 'limit' do
|
5
|
+
it "one where clause" do
|
6
|
+
hash = {microposts_count: 10, limit: 10}
|
7
|
+
|
8
|
+
query = HelperClass.new.apply_filters(User.all, hash).to_sql
|
9
|
+
expected_query = q(
|
10
|
+
"SELECT users.* FROM users WHERE (users.microposts_count = 10) LIMIT 10"
|
11
|
+
)
|
12
|
+
|
13
|
+
expect(strip(query)).to eq expected_query.to_s
|
14
|
+
end
|
15
|
+
|
16
|
+
it "multiple where clause" do
|
17
|
+
hash = {microposts_count: 3, followers_count: 5, limit: 10}
|
18
|
+
|
19
|
+
query = HelperClass.new.apply_filters(User.all, hash).to_sql
|
20
|
+
expected_query = q(
|
21
|
+
"SELECT users.* FROM users",
|
22
|
+
"WHERE (users.microposts_count = 3)",
|
23
|
+
"AND (users.followers_count = 5)",
|
24
|
+
"LIMIT 10"
|
25
|
+
)
|
26
|
+
|
27
|
+
expect(strip(query)).to eq expected_query.to_s
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
describe ActiveHashRelation do
|
2
|
+
include Helpers
|
3
|
+
|
4
|
+
context 'NULL' do
|
5
|
+
context "one where clause" do
|
6
|
+
it "is null" do
|
7
|
+
hash = {admin: {null: true}}
|
8
|
+
|
9
|
+
query = HelperClass.new.apply_filters(User.all, hash).to_sql
|
10
|
+
expected_query = q(
|
11
|
+
"SELECT users.* FROM users",
|
12
|
+
"WHERE (users.admin IS NULL)"
|
13
|
+
)
|
14
|
+
|
15
|
+
expect(strip(query)).to eq expected_query.to_s
|
16
|
+
end
|
17
|
+
|
18
|
+
it "is not null" do
|
19
|
+
hash = {admin: {null: false}}
|
20
|
+
|
21
|
+
query = HelperClass.new.apply_filters(User.all, hash).to_sql
|
22
|
+
expected_query = q(
|
23
|
+
"SELECT users.* FROM users",
|
24
|
+
"WHERE (users.admin IS NOT NULL)"
|
25
|
+
)
|
26
|
+
|
27
|
+
expect(strip(query)).to eq expected_query.to_s
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
it "multi where clauses" do
|
32
|
+
hash = {admin: {null: true}, verified: {null: false}}
|
33
|
+
|
34
|
+
query = HelperClass.new.apply_filters(User.all, hash).to_sql
|
35
|
+
expected_query = q(
|
36
|
+
"SELECT users.* FROM users",
|
37
|
+
"WHERE (users.admin IS NULL)",
|
38
|
+
"AND (users.verified IS NOT NULL)"
|
39
|
+
)
|
40
|
+
|
41
|
+
expect(strip(query)).to eq expected_query.to_s
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,142 @@
|
|
1
|
+
describe ActiveHashRelation do
|
2
|
+
include Helpers
|
3
|
+
|
4
|
+
context 'numbers' do
|
5
|
+
it "one where clause" do
|
6
|
+
hash = {microposts_count: 10}
|
7
|
+
|
8
|
+
query = HelperClass.new.apply_filters(User.all, hash).to_sql
|
9
|
+
expected_query = q(
|
10
|
+
"SELECT users.* FROM users WHERE (users.microposts_count = 10)"
|
11
|
+
)
|
12
|
+
|
13
|
+
expect(strip(query)).to eq expected_query.to_s
|
14
|
+
end
|
15
|
+
|
16
|
+
it "multiple where clause" do
|
17
|
+
hash = {microposts_count: 3, followers_count: 5}
|
18
|
+
|
19
|
+
query = HelperClass.new.apply_filters(User.all, hash).to_sql
|
20
|
+
expected_query = q(
|
21
|
+
"SELECT users.* FROM users",
|
22
|
+
"WHERE (users.microposts_count = 3)",
|
23
|
+
"AND (users.followers_count = 5)"
|
24
|
+
)
|
25
|
+
|
26
|
+
expect(strip(query)).to eq expected_query.to_s
|
27
|
+
end
|
28
|
+
|
29
|
+
it "multiple values" do
|
30
|
+
hash = {microposts_count: [3,4,5]}
|
31
|
+
|
32
|
+
query = HelperClass.new.apply_filters(User.all, hash).to_sql
|
33
|
+
expected_query = q(
|
34
|
+
"SELECT users.* FROM users",
|
35
|
+
"WHERE (users.microposts_count IN (3, 4, 5))",
|
36
|
+
)
|
37
|
+
|
38
|
+
expect(strip(query)).to eq expected_query.to_s
|
39
|
+
end
|
40
|
+
|
41
|
+
context "queries" do
|
42
|
+
it 'eq' do
|
43
|
+
hash = {microposts_count: {eq: 10}}
|
44
|
+
|
45
|
+
query = HelperClass.new.apply_filters(User.all, hash).to_sql
|
46
|
+
expected_query = q(
|
47
|
+
"SELECT users.* FROM users WHERE (users.microposts_count = 10)"
|
48
|
+
)
|
49
|
+
|
50
|
+
expect(strip(query)).to eq expected_query.to_s
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'le' do
|
54
|
+
hash = {microposts_count: {le: 10}}
|
55
|
+
|
56
|
+
query = HelperClass.new.apply_filters(User.all, hash).to_sql
|
57
|
+
expected_query = q(
|
58
|
+
"SELECT users.* FROM users WHERE (users.microposts_count < 10)"
|
59
|
+
)
|
60
|
+
|
61
|
+
expect(strip(query)).to eq expected_query.to_s
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'leq' do
|
65
|
+
hash = {microposts_count: {leq: 10}}
|
66
|
+
|
67
|
+
query = HelperClass.new.apply_filters(User.all, hash).to_sql
|
68
|
+
expected_query = q(
|
69
|
+
"SELECT users.* FROM users WHERE (users.microposts_count <= 10)"
|
70
|
+
)
|
71
|
+
|
72
|
+
expect(strip(query)).to eq expected_query.to_s
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'ge' do
|
76
|
+
hash = {microposts_count: {ge: 10}}
|
77
|
+
|
78
|
+
query = HelperClass.new.apply_filters(User.all, hash).to_sql
|
79
|
+
expected_query = q(
|
80
|
+
"SELECT users.* FROM users WHERE (users.microposts_count > 10)"
|
81
|
+
)
|
82
|
+
|
83
|
+
expect(strip(query)).to eq expected_query.to_s
|
84
|
+
end
|
85
|
+
|
86
|
+
it 'geq' do
|
87
|
+
hash = {microposts_count: {geq: 10}}
|
88
|
+
|
89
|
+
query = HelperClass.new.apply_filters(User.all, hash).to_sql
|
90
|
+
expected_query = q(
|
91
|
+
"SELECT users.* FROM users WHERE (users.microposts_count >= 10)"
|
92
|
+
)
|
93
|
+
|
94
|
+
expect(strip(query)).to eq expected_query.to_s
|
95
|
+
end
|
96
|
+
|
97
|
+
context 'combined' do
|
98
|
+
it 'with a single column' do
|
99
|
+
hash = {microposts_count: {geq: 10, le: 20}}
|
100
|
+
|
101
|
+
query = HelperClass.new.apply_filters(User.all, hash).to_sql
|
102
|
+
expected_query = q(
|
103
|
+
"SELECT users.* FROM users",
|
104
|
+
"WHERE (users.microposts_count < 20)",
|
105
|
+
"AND (users.microposts_count >= 10)"
|
106
|
+
)
|
107
|
+
|
108
|
+
expect(strip(query)).to eq expected_query.to_s
|
109
|
+
end
|
110
|
+
|
111
|
+
it 'with multiple columns' do
|
112
|
+
hash = {
|
113
|
+
microposts_count: {geq: 10, le: 20},
|
114
|
+
followers_count: {leq: 50, ge: 0}
|
115
|
+
}
|
116
|
+
|
117
|
+
query = HelperClass.new.apply_filters(User.all, hash).to_sql
|
118
|
+
expected_query = q(
|
119
|
+
"SELECT users.* FROM users",
|
120
|
+
"WHERE (users.microposts_count < 20)",
|
121
|
+
"AND (users.microposts_count >= 10)",
|
122
|
+
"AND (users.followers_count <= 50)",
|
123
|
+
"AND (users.followers_count > 0)"
|
124
|
+
)
|
125
|
+
|
126
|
+
expect(strip(query)).to eq expected_query.to_s
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
it 'rails enum' do
|
131
|
+
hash = {status: 0}
|
132
|
+
|
133
|
+
query = HelperClass.new.apply_filters(Micropost.all, hash).to_sql
|
134
|
+
expected_query = q(
|
135
|
+
"SELECT microposts.* FROM microposts WHERE (microposts.status = 0)"
|
136
|
+
)
|
137
|
+
|
138
|
+
expect(strip(query)).to eq expected_query.to_s
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
@@ -0,0 +1,98 @@
|
|
1
|
+
describe ActiveHashRelation do
|
2
|
+
include Helpers
|
3
|
+
|
4
|
+
if ActiveRecord::VERSION::MAJOR < 5
|
5
|
+
context 'OR filter' do
|
6
|
+
it "one OR clause" do
|
7
|
+
logger = double('logger')
|
8
|
+
allow(logger).to receive(:warn)
|
9
|
+
allow(Rails).to receive(:logger).and_return(logger)
|
10
|
+
|
11
|
+
hash = {or: [{name: 'Filippos'}, {name: 'Vasilis'}]}
|
12
|
+
|
13
|
+
HelperClass.new.apply_filters(User.all, hash).to_sql
|
14
|
+
expect(logger).to have_received(:warn)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
else
|
19
|
+
context 'OR filter' do
|
20
|
+
it "one OR clause" do
|
21
|
+
hash = {or: [{name: 'Filippos'}, {name: 'Vasilis'}]}
|
22
|
+
|
23
|
+
query = HelperClass.new.apply_filters(User.all, hash).to_sql
|
24
|
+
expected_query = q(
|
25
|
+
"SELECT users.* FROM users",
|
26
|
+
"WHERE ((users.name = 'Filippos') OR (users.name = 'Vasilis'))"
|
27
|
+
)
|
28
|
+
|
29
|
+
expect(strip(query)).to eq expected_query.to_s
|
30
|
+
end
|
31
|
+
|
32
|
+
it "multiple OR clauses" do
|
33
|
+
hash = {or: [{or: [{name: 'Filippos'}, {name: 'Vasilis'}]}, {or: [{id: 1}, {id: 2}]}]}
|
34
|
+
|
35
|
+
query = HelperClass.new.apply_filters(User.all, hash).to_sql
|
36
|
+
expected_query = q(
|
37
|
+
"SELECT users.* FROM users",
|
38
|
+
"WHERE",
|
39
|
+
"(",
|
40
|
+
"((users.name = 'Filippos') OR (users.name = 'Vasilis'))",
|
41
|
+
"OR",
|
42
|
+
"((users.id = 1) OR (users.id = 2))",
|
43
|
+
")"
|
44
|
+
)
|
45
|
+
|
46
|
+
expect(strip(query)).to eq expected_query.to_s
|
47
|
+
end
|
48
|
+
|
49
|
+
it "one complex OR clause" do
|
50
|
+
hash = {or: [{name: 'Filippos', token: '123'}, {name: 'Vasilis'}]}
|
51
|
+
|
52
|
+
query = HelperClass.new.apply_filters(User.all, hash).to_sql
|
53
|
+
expected_query = q(
|
54
|
+
"SELECT users.* FROM users",
|
55
|
+
"WHERE",
|
56
|
+
"(",
|
57
|
+
"(users.name = 'Filippos') AND (users.token = '123')",
|
58
|
+
"OR",
|
59
|
+
"(users.name = 'Vasilis')",
|
60
|
+
")"
|
61
|
+
)
|
62
|
+
|
63
|
+
expect(strip(query)).to eq expected_query.to_s
|
64
|
+
end
|
65
|
+
|
66
|
+
it "nested OR clause" do
|
67
|
+
hash = {or: [{or: [{name: 'Filippos'}, {token: '123'}]}, {name: 'Vasilis'}]}
|
68
|
+
|
69
|
+
query = HelperClass.new.apply_filters(User.all, hash).to_sql
|
70
|
+
expected_query = q(
|
71
|
+
"SELECT users.* FROM users",
|
72
|
+
"WHERE",
|
73
|
+
"(",
|
74
|
+
"((users.name = 'Filippos') OR (users.token = '123'))",
|
75
|
+
"OR",
|
76
|
+
"(users.name = 'Vasilis')",
|
77
|
+
")"
|
78
|
+
)
|
79
|
+
|
80
|
+
expect(strip(query)).to eq expected_query.to_s
|
81
|
+
end
|
82
|
+
|
83
|
+
it "OR clause on associations" do
|
84
|
+
hash = {microposts: {or: [{content: 'Sveavägen 4'}, {id: 1}]}}
|
85
|
+
|
86
|
+
query = HelperClass.new.apply_filters(User.all, hash, include_associations: true).to_sql
|
87
|
+
expected_query = q(
|
88
|
+
"SELECT users.* FROM users",
|
89
|
+
"INNER JOIN microposts ON microposts.user_id = users.id",
|
90
|
+
"WHERE ((microposts.content = 'Sveavägen 4') OR (microposts.id = 1))"
|
91
|
+
)
|
92
|
+
|
93
|
+
expect(strip(query)).to eq expected_query.to_s
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
@@ -0,0 +1,29 @@
|
|
1
|
+
describe ActiveHashRelation do
|
2
|
+
include Helpers
|
3
|
+
|
4
|
+
context 'primary_key' do
|
5
|
+
it "one key" do
|
6
|
+
hash = {id: 1}
|
7
|
+
|
8
|
+
query = HelperClass.new.apply_filters(User.all, hash).to_sql
|
9
|
+
expected_query = q(
|
10
|
+
"SELECT users.* FROM users WHERE (users.id = 1)"
|
11
|
+
)
|
12
|
+
|
13
|
+
expect(strip(query)).to eq expected_query.to_s
|
14
|
+
end
|
15
|
+
|
16
|
+
it "multiple keys" do
|
17
|
+
hash = {id: [1,2,3,4]}
|
18
|
+
|
19
|
+
query = HelperClass.new.apply_filters(User.all, hash).to_sql
|
20
|
+
expected_query = q(
|
21
|
+
"SELECT users.* FROM users WHERE (users.id IN (1, 2, 3, 4))"
|
22
|
+
)
|
23
|
+
|
24
|
+
expect(strip(query)).to eq expected_query.to_s
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
|